-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
262 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace C0ntax\ParsleyBundle\Directive\Field\Constraint; | ||
|
||
/** | ||
* Class Range | ||
* | ||
* @package C0ntax\ParsleyBundle\Directive\Field\Constraint | ||
*/ | ||
class Range extends AbstractConstraint | ||
{ | ||
/** @var int|float|\DateTime|\DateTimeImmutable */ | ||
private $min; | ||
|
||
/** @var int|float|\DateTime|\DateTimeImmutable */ | ||
private $max; | ||
|
||
/** | ||
* Range constructor. | ||
* | ||
* @param int|float|\DateTime|\DateTimeImmutable $min | ||
* @param int|float|\DateTime|\DateTimeImmutable $max | ||
* @param null|string $errorMessageString | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct($min, $max, ?string $errorMessageString = null) | ||
{ | ||
$this->setMin($min); | ||
$this->setMax($max); | ||
parent::__construct($errorMessageString); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getConstraintId(): string | ||
{ | ||
return 'range'; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getViewAttr(): array | ||
{ | ||
$minValue = $this->createStringValue($this->getMin()); | ||
$maxValue = $this->createStringValue($this->getMax()); | ||
|
||
return $this->getMergedViewAttr('["'.$minValue.'", "'.$maxValue.'"]'); | ||
} | ||
|
||
/** | ||
* @param $value | ||
* @return string | ||
*/ | ||
private function createStringValue($value): string | ||
{ | ||
if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) { | ||
return $value->format('Y-m-d H:i:s'); | ||
} else { | ||
return (string) $value; | ||
} | ||
} | ||
|
||
/** | ||
* @return \DateTime|\DateTimeImmutable|float|int | ||
*/ | ||
private function getMin() | ||
{ | ||
return $this->min; | ||
} | ||
|
||
/** | ||
* @param \DateTime|\DateTimeImmutable|float|int $min | ||
* @return Range | ||
*/ | ||
private function setMin($min): Range | ||
{ | ||
$this->min = $min; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return \DateTime|\DateTimeImmutable|float|int | ||
*/ | ||
private function getMax() | ||
{ | ||
return $this->max; | ||
} | ||
|
||
/** | ||
* @param \DateTime|\DateTimeImmutable|float|int $max | ||
* @return Range | ||
*/ | ||
private function setMax($max): Range | ||
{ | ||
$this->max = $max; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace C0ntax\ParsleyBundle\Tests\Directive\Field\Constraint; | ||
|
||
use C0ntax\ParsleyBundle\Directive\Field\Constraint\Range; | ||
|
||
class RangeTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testGetConstraintId() | ||
{ | ||
self::assertEquals('range', Range::getConstraintId()); | ||
} | ||
|
||
public function testGetViewAttr() | ||
{ | ||
$pattern = new Range(1, 10); | ||
self::assertEquals(['data-parsley-range' => '["1", "10"]'], $pattern->getViewAttr()); | ||
|
||
$pattern = new Range(1, 10, 'Error message'); | ||
self::assertEquals(['data-parsley-range' => '["1", "10"]', 'data-parsley-range-message' => 'Error message'], $pattern->getViewAttr()); | ||
|
||
$minDate = (new \DateTime('now'))->sub(new \DateInterval('P2W')); | ||
$maxDate = (new \DateTime('now'))->add(new \DateInterval('P2W')); | ||
|
||
$minDateString = $minDate->format('Y-m-d H:i:s'); | ||
$maxDateString = $maxDate->format('Y-m-d H:i:s'); | ||
|
||
$pattern = new Range($minDate, $maxDate, 'Error message'); | ||
self::assertEquals(['data-parsley-range' => '["'.$minDateString.'", "'.$maxDateString.'"]', 'data-parsley-range-message' => 'Error message'], $pattern->getViewAttr()); | ||
} | ||
} |
Oops, something went wrong.