-
Notifications
You must be signed in to change notification settings - Fork 87
resolve #158 #159
resolve #158 #159
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
|
||
class Date extends DateTimeElement | ||
{ | ||
|
||
const DATETIME_FORMAT = 'Y-m-d'; | ||
|
||
/** | ||
* Seed attributes | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
use DateInterval; | ||
use DateTime as PhpDateTime; | ||
use Zend\Form\Element; | ||
use Zend\Form\Exception\InvalidArgumentException; | ||
use Zend\InputFilter\InputProviderInterface; | ||
use Zend\Validator\Date as DateValidator; | ||
use Zend\Validator\DateStep as DateStepValidator; | ||
|
@@ -120,17 +121,60 @@ protected function getValidators() | |
$validators = []; | ||
$validators[] = $this->getDateValidator(); | ||
|
||
if (isset($this->attributes['min'])) { | ||
$validators[] = new GreaterThanValidator([ | ||
'min' => $this->attributes['min'], | ||
'inclusive' => true, | ||
]); | ||
if (isset($this->attributes['min']) && | ||
\DateTime::createFromFormat( | ||
static::DATETIME_FORMAT, | ||
$this->attributes['min'] | ||
) instanceof \DateTimeInterface | ||
) { | ||
$validators[] = new GreaterThanValidator( | ||
[ | ||
'min' => $this->attributes['min'], | ||
'inclusive' => true, | ||
] | ||
); | ||
} elseif (isset($this->attributes['min']) && | ||
! \DateTime::createFromFormat( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. …and Please simplify these if statements. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is correct and intended to be this way. No further simplifications are necessary. Are you proposing creating and referencing unnecessary variables simply for the sake of reducing the length of the evaluation? |
||
static::DATETIME_FORMAT, | ||
$this->attributes['min'] | ||
) instanceof \DateTimeInterface | ||
) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'%1$s expects "min" to conform to %2$s; received "%3$s"', | ||
__METHOD__, | ||
static::DATETIME_FORMAT, | ||
$this->attributes['min'] | ||
) | ||
); | ||
} | ||
if (isset($this->attributes['max'])) { | ||
$validators[] = new LessThanValidator([ | ||
'max' => $this->attributes['max'], | ||
|
||
if (isset($this->attributes['max']) && | ||
\DateTime::createFromFormat( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here… There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same response as above -- this logic is intended to be this way and has backing unit tests added. |
||
static::DATETIME_FORMAT, | ||
$this->attributes['max'] | ||
) instanceof \DateTimeInterface | ||
) { | ||
$validators[] = new LessThanValidator( | ||
[ | ||
'max' => $this->attributes['max'], | ||
'inclusive' => true, | ||
]); | ||
] | ||
); | ||
} elseif (isset($this->attributes['max']) && | ||
! \DateTime::createFromFormat( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. …and here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See other responses. Again, intentional :-P |
||
static::DATETIME_FORMAT, | ||
$this->attributes['max'] | ||
) instanceof \DateTimeInterface | ||
) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'%1$s expects "max" to conform to %2$s; received "%3$s"', | ||
__METHOD__, | ||
static::DATETIME_FORMAT, | ||
$this->attributes['max'] | ||
) | ||
); | ||
} | ||
if (! isset($this->attributes['step']) | ||
|| 'any' !== $this->attributes['step'] | ||
|
@@ -145,7 +189,7 @@ protected function getValidators() | |
/** | ||
* Retrieves a Date Validator configured for a DateTime Input type | ||
* | ||
* @return DateTime | ||
* @return DateValidator | ||
*/ | ||
protected function getDateValidator() | ||
{ | ||
|
@@ -155,7 +199,7 @@ protected function getDateValidator() | |
/** | ||
* Retrieves a DateStep Validator configured for a DateTime Input type | ||
* | ||
* @return DateTime | ||
* @return DateStepValidator | ||
*/ | ||
protected function getStepValidator() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
|
||
class Month extends DateTime | ||
{ | ||
|
||
const DATETIME_FORMAT = 'Y-m'; | ||
|
||
/** | ||
* Seed attributes | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ | |
|
||
class Time extends DateTime | ||
{ | ||
|
||
const DATETIME_FORMAT = 'H:i:s'; | ||
|
||
/** | ||
* Seed attributes | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
use PHPUnit\Framework\TestCase; | ||
use Zend\Form\Element\DateTimeLocal as DateTimeLocalElement; | ||
use Zend\Form\Exception\InvalidArgumentException; | ||
|
||
class DateTimeLocalTest extends TestCase | ||
{ | ||
|
@@ -19,8 +20,8 @@ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttri | |
$element = new DateTimeLocalElement('foo'); | ||
$element->setAttributes([ | ||
'inclusive' => true, | ||
'min' => '2000-01-01T00:00Z', | ||
'max' => '2001-01-01T00:00Z', | ||
'min' => '2000-01-01T00:00', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same story -- this is the format that the createFromFormat expects. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test was updated to be consistent w/ the documentation. Additional test added when an invalid min/max specification like the one above is added that would result in an InvalidArgumentException. |
||
'max' => '2001-01-01T00:00', | ||
'step' => '1', | ||
]); | ||
|
||
|
@@ -40,11 +41,11 @@ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttri | |
switch ($class) { | ||
case 'Zend\Validator\GreaterThan': | ||
$this->assertTrue($validator->getInclusive()); | ||
$this->assertEquals('2000-01-01T00:00Z', $validator->getMin()); | ||
$this->assertEquals('2000-01-01T00:00', $validator->getMin()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the format used in both the documentation and what conforms to the requested \DateTime::createFromFormat string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test was updated to be consistent w/ the documentation. Additional test added when an invalid min/max specification like the one above is added that would result in an InvalidArgumentException. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the test changes make sense. The originals were based on an invalid assumption. |
||
break; | ||
case 'Zend\Validator\LessThan': | ||
$this->assertTrue($validator->getInclusive()); | ||
$this->assertEquals('2001-01-01T00:00Z', $validator->getMax()); | ||
$this->assertEquals('2001-01-01T00:00', $validator->getMax()); | ||
break; | ||
case 'Zend\Validator\DateStep': | ||
$dateInterval = new \DateInterval('PT1M'); | ||
|
@@ -55,4 +56,33 @@ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttri | |
} | ||
} | ||
} | ||
|
||
public function testFailsWithInvalidMinSpecification() | ||
{ | ||
$element = new DateTimeLocalElement('foo'); | ||
$element->setAttributes( | ||
[ | ||
'inclusive' => true, | ||
'min' => '2001-01-01T00:00Z', | ||
'step' => '1', | ||
] | ||
); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$element->getInputSpecification(); | ||
} | ||
|
||
public function testFailsWithInvalidMaxSpecification() | ||
{ | ||
$element = new DateTimeLocalElement('foo'); | ||
$element->setAttributes( | ||
[ | ||
'inclusive' => true, | ||
'max' => '2001-01-01T00:00Z', | ||
'step' => '1', | ||
] | ||
); | ||
$this->expectException(InvalidArgumentException::class); | ||
$element->getInputSpecification(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createFromFormat
here…There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is correct and intended to be this way. No further simplifications are necessary. Are you proposing creating and referencing unnecessary variables simply for the sake of reducing the length of the evaluation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the
DateTime::createFromFormat()
from the condition and theelseif
can be eliminated.At the moment the entire block is hard to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please simplify the if statements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I agree with @froschdesign here. The same multiline statement is used in multiple conditions, making those conditions really hard to read. Capturing to a variable makes the conditions easier to read.
I can do that on merge.