Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[Validator] Refactor Date #5458

Merged
merged 1 commit into from Nov 11, 2013

Conversation

Projects
None yet
3 participants
Contributor

texdc commented Nov 9, 2013

The Date validator converts string, array, and int values into DateTime instances, or tries to, as part of the validation process. But, DateStep also uses a conversion process for helper dates. So, the conversion process was synchronized with Date and refactored out into separate methods to allow extending classes to reuse the functionality and to reduce the complexity of the validation method.

@samsonasik samsonasik commented on the diff Nov 10, 2013

library/Zend/Validator/Date.php
}
- return true;
+ $type = gettype($param);
+ if (!in_array($type, array('string', 'integer', 'array'))) {
+ if ($addErrors) $this->error(self::INVALID);

@samsonasik samsonasik commented on the diff Nov 10, 2013

library/Zend/Validator/Date.php
+ /**
+ * Attempts to convert a string into a DateTime object
+ *
+ * @param string $value
+ * @param bool $addErrors
+ * @return bool|DateTime
+ */
+ protected function convertString($value, $addErrors = true)
+ {
+ $date = DateTime::createFromFormat($this->format, $value);
+
+ // Invalid dates can show up as warnings (ie. "2007-02-99")
+ // and still return a DateTime object.
+ $errors = DateTime::getLastErrors();
+ if ($errors['warning_count'] > 0) {
+ if ($addErrors) $this->error(self::FALSEFORMAT);

@samsonasik samsonasik commented on the diff Nov 10, 2013

library/Zend/Validator/DateStep.php
}
- if (!($dateObj instanceof DateTime)) {
- throw new Exception\InvalidArgumentException('Invalid date param given');
+
+ // Invalid dates can show up as warnings (ie. "2007-02-99")
+ // and still return a DateTime object.
+ $errors = DateTime::getLastErrors();
+ if ($errors['warning_count'] > 0) {
+ if ($addErrors) $this->error(self::FALSE_FORMAT);

@weierophinney weierophinney added a commit that referenced this pull request Nov 11, 2013

@weierophinney weierophinney Merge pull request #5458 from texdc/Refactor-Date-Validator
[Validator] Refactor Date
26d7817

@weierophinney weierophinney added a commit that referenced this pull request Nov 11, 2013

@weierophinney weierophinney [#5458] CS fixes
- Added blocks where necessary
- Conditional structures
- constant docblocks
a3c8f36

@weierophinney weierophinney merged commit 8df9023 into zendframework:develop Nov 11, 2013

1 check passed

default The Travis CI build passed
Details

@ghost ghost assigned weierophinney Nov 11, 2013

@texdc texdc deleted the unknown repository branch Nov 12, 2013

@weierophinney weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015

@weierophinney weierophinney Merge pull request zendframework/zendframework#5458 from texdc/Refact…
…or-Date-Validator

[Validator] Refactor Date
b6fff8e

@weierophinney weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015

@weierophinney weierophinney [zendframework/zendframework#5458] CS fixes
- Added blocks where necessary
- Conditional structures
- constant docblocks
55a8aa1

@weierophinney weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015

@weierophinney weierophinney Merge branch 'feature/5458' into develop 8ee395c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment