Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit b520f1a

Browse files
committed
Unit tests updated to reflect changes.
Throws an InvalidArgumentException when one attempts to create a DateTime element with a string that does not conform to the expected \DateTime format. Week updated because one cannot construct a \DateTime from 'Y-\WW' format
1 parent b0d470c commit b520f1a

File tree

6 files changed

+115
-27
lines changed

6 files changed

+115
-27
lines changed

src/Element/DateTime.php

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use DateInterval;
1313
use DateTime as PhpDateTime;
1414
use Zend\Form\Element;
15+
use Zend\Form\Exception\InvalidArgumentException;
1516
use Zend\InputFilter\InputProviderInterface;
1617
use Zend\Validator\Date as DateValidator;
1718
use Zend\Validator\DateStep as DateStepValidator;
@@ -53,7 +54,6 @@ class DateTime extends Element implements InputProviderInterface
5354
public function setOptions($options)
5455
{
5556
parent::setOptions($options);
56-
5757
if (isset($this->options['format'])) {
5858
$this->setFormat($this->options['format']);
5959
}
@@ -81,6 +81,7 @@ public function getValue($returnFormattedValue = true)
8181
return $value;
8282
}
8383
$format = $this->getFormat();
84+
8485
return $value->format($format);
8586
}
8687

@@ -92,7 +93,8 @@ public function getValue($returnFormattedValue = true)
9293
*/
9394
public function setFormat($format)
9495
{
95-
$this->format = (string) $format;
96+
$this->format = (string)$format;
97+
9698
return $this;
9799
}
98100

@@ -120,32 +122,78 @@ protected function getValidators()
120122
$validators = [];
121123
$validators[] = $this->getDateValidator();
122124

123-
if (isset($this->attributes['min'])) {
124-
$validators[] = new GreaterThanValidator([
125-
'min' => $this->attributes['min'],
126-
'inclusive' => true,
127-
]);
125+
if (isset($this->attributes['min']) &&
126+
\DateTime::createFromFormat(
127+
$this->format,
128+
$this->attributes['min']
129+
) instanceof \DateTimeInterface
130+
) {
131+
$validators[] = new GreaterThanValidator(
132+
[
133+
'min' => $this->attributes['min'],
134+
'inclusive' => true,
135+
]
136+
);
137+
} elseif (isset($this->attributes['min']) &&
138+
! \DateTime::createFromFormat(
139+
$this->format,
140+
$this->attributes['min']
141+
) instanceof \DateTimeInterface
142+
) {
143+
throw new InvalidArgumentException(
144+
sprintf(
145+
'%1$s expects "min" to conform to %2$s; received "%3$s"',
146+
__METHOD__,
147+
$this->format,
148+
$this->attributes['min']
149+
)
150+
);
128151
}
129-
if (isset($this->attributes['max'])) {
130-
$validators[] = new LessThanValidator([
131-
'max' => $this->attributes['max'],
152+
153+
if (isset($this->attributes['max']) &&
154+
\DateTime::createFromFormat(
155+
$this->format,
156+
$this->attributes['max']
157+
) instanceof \DateTimeInterface
158+
) {
159+
$validators[] = new LessThanValidator(
160+
[
161+
'max' => $this->attributes['max'],
132162
'inclusive' => true,
133-
]);
163+
]
164+
);
165+
} elseif (isset($this->attributes['max']) &&
166+
! \DateTime::createFromFormat(
167+
$this->format,
168+
$this->attributes['max']
169+
) instanceof \DateTimeInterface
170+
) {
171+
throw new InvalidArgumentException(
172+
sprintf(
173+
'%1$s expects "max" to conform to %2$s; received "%3$s"',
174+
__METHOD__,
175+
$this->format,
176+
$this->attributes['max']
177+
)
178+
);
134179
}
180+
181+
135182
if (! isset($this->attributes['step'])
136183
|| 'any' !== $this->attributes['step']
137184
) {
138185
$validators[] = $this->getStepValidator();
139186
}
140187

141188
$this->validators = $validators;
189+
142190
return $this->validators;
143191
}
144192

145193
/**
146194
* Retrieves a Date Validator configured for a DateTime Input type
147195
*
148-
* @return DateTime
196+
* @return DateValidator
149197
*/
150198
protected function getDateValidator()
151199
{
@@ -155,22 +203,23 @@ protected function getDateValidator()
155203
/**
156204
* Retrieves a DateStep Validator configured for a DateTime Input type
157205
*
158-
* @return DateTime
206+
* @return DateStepValidator
159207
*/
160208
protected function getStepValidator()
161209
{
162-
$format = $this->getFormat();
210+
$format = $this->getFormat();
163211
$stepValue = (isset($this->attributes['step']))
164-
? $this->attributes['step'] : 1; // Minutes
165-
212+
? $this->attributes['step'] : 1; // Minutes
166213
$baseValue = (isset($this->attributes['min']))
167-
? $this->attributes['min'] : date($format, 0);
214+
? $this->attributes['min'] : date($format, 0);
168215

169-
return new DateStepValidator([
170-
'format' => $format,
216+
return new DateStepValidator(
217+
[
218+
'format' => $format,
171219
'baseValue' => $baseValue,
172-
'step' => new DateInterval("PT{$stepValue}M"),
173-
]);
220+
'step' => new DateInterval("PT{$stepValue}M"),
221+
]
222+
);
174223
}
175224

176225
/**

src/Element/Week.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use Zend\Validator\DateStep as DateStepValidator;
1313
use Zend\Validator\Regex as RegexValidator;
14+
use Zend\Validator\GreaterThan as GreaterThanValidator;
15+
use Zend\Validator\LessThan as LessThanValidator;
1416

1517
class Week extends DateTime
1618
{
@@ -52,4 +54,36 @@ protected function getStepValidator()
5254
'step' => new \DateInterval("P{$stepValue}W"),
5355
]);
5456
}
57+
58+
/**
59+
* @see https://bugs.php.net/bug.php?id=74511
60+
* @return array
61+
*/
62+
protected function getValidators()
63+
{
64+
if ($this->validators) {
65+
return $this->validators;
66+
}
67+
$validators = [];
68+
$validators[] = $this->getDateValidator();
69+
if (isset($this->attributes['min'])) {
70+
$validators[] = new GreaterThanValidator([
71+
'min' => $this->attributes['min'],
72+
'inclusive' => true,
73+
]);
74+
}
75+
if (isset($this->attributes['max'])) {
76+
$validators[] = new LessThanValidator([
77+
'max' => $this->attributes['max'],
78+
'inclusive' => true,
79+
]);
80+
}
81+
if (! isset($this->attributes['step'])
82+
|| 'any' !== $this->attributes['step']
83+
) {
84+
$validators[] = $this->getStepValidator();
85+
}
86+
$this->validators = $validators;
87+
return $this->validators;
88+
}
5589
}

test/Element/DateTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public function testCorrectFormatPassedToDateValidator()
124124
{
125125
$element = new DateElement('foo');
126126
$element->setAttributes([
127-
'min' => '2012-01-01',
128-
'max' => '2012-12-31',
127+
'min' => '01-01-2012',
128+
'max' => '12-31-2012',
129129
]);
130130
$element->setFormat('d-m-Y');
131131

test/Element/DateTimeLocalTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttri
1919
$element = new DateTimeLocalElement('foo');
2020
$element->setAttributes([
2121
'inclusive' => true,
22-
'min' => '2000-01-01T00:00Z',
23-
'max' => '2001-01-01T00:00Z',
22+
'min' => '2000-01-01T00:00',
23+
'max' => '2001-01-01T00:00',
2424
'step' => '1',
2525
]);
2626

@@ -40,11 +40,11 @@ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttri
4040
switch ($class) {
4141
case 'Zend\Validator\GreaterThan':
4242
$this->assertTrue($validator->getInclusive());
43-
$this->assertEquals('2000-01-01T00:00Z', $validator->getMin());
43+
$this->assertEquals('2000-01-01T00:00', $validator->getMin());
4444
break;
4545
case 'Zend\Validator\LessThan':
4646
$this->assertTrue($validator->getInclusive());
47-
$this->assertEquals('2001-01-01T00:00Z', $validator->getMax());
47+
$this->assertEquals('2001-01-01T00:00', $validator->getMax());
4848
break;
4949
case 'Zend\Validator\DateStep':
5050
$dateInterval = new \DateInterval('PT1M');

test/Element/MonthTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttri
2424
'step' => '1',
2525
]);
2626

27+
$element->setFormat('Y-m');
28+
2729
$inputSpec = $element->getInputSpecification();
2830
$this->assertArrayHasKey('validators', $inputSpec);
2931
$this->assertInternalType('array', $inputSpec['validators']);

test/Element/WeekTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
class WeekTest extends TestCase
1616
{
17+
1718
public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttributes()
1819
{
1920
$element = new WeekElement('foo');
@@ -24,6 +25,8 @@ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttri
2425
'step' => '1',
2526
]);
2627

28+
$element->setFormat('Y-\WW');
29+
2730
$inputSpec = $element->getInputSpecification();
2831
$this->assertArrayHasKey('validators', $inputSpec);
2932
$this->assertInternalType('array', $inputSpec['validators']);

0 commit comments

Comments
 (0)