1
2/*
3 +------------------------------------------------------------------------+
4 | Phalcon Framework                                                      |
5 +------------------------------------------------------------------------+
6 | Copyright (c) 2011-2017 Phalcon Team (http://www.phalconphp.com)       |
7 +------------------------------------------------------------------------+
8 | This source file is subject to the New BSD License that is bundled     |
9 | with this package in the file LICENSE.txt.                             |
10 |                                                                        |
11 | If you did not receive a copy of the license and are unable to         |
12 | obtain it through the world-wide-web, please send an email             |
13 | to license@phalconphp.com so we can send you a copy immediately.       |
14 +------------------------------------------------------------------------+
15 | Authors: Andres Gutierrez <andres@phalconphp.com>                      |
16 |          Eduar Carvajal <eduar@phalconphp.com>                         |
17 +------------------------------------------------------------------------+
18 */
19
20namespace Phalcon\Validation\Validator;
21
22use Phalcon\Validation;
23use Phalcon\Validation\Validator;
24use Phalcon\Validation\Message;
25
26/**
27 * Phalcon\Validation\Validator\Date
28 *
29 * Checks if a value is a valid date
30 *
31 * <code>
32 * use Phalcon\Validation;
33 * use Phalcon\Validation\Validator\Date as DateValidator;
34 *
35 * $validator = new Validation();
36 *
37 * $validator->add(
38 *     "date",
39 *     new DateValidator(
40 *         [
41 *             "format"  => "d-m-Y",
42 *             "message" => "The date is invalid",
43 *         ]
44 *     )
45 * );
46 *
47 * $validator->add(
48 *     [
49 *         "date",
50 *         "anotherDate",
51 *     ],
52 *     new DateValidator(
53 *         [
54 *             "format" => [
55 *                 "date"        => "d-m-Y",
56 *                 "anotherDate" => "Y-m-d",
57 *             ],
58 *             "message" => [
59 *                 "date"        => "The date is invalid",
60 *                 "anotherDate" => "The another date is invalid",
61 *             ],
62 *         ]
63 *     )
64 * );
65 * </code>
66 */
67class Date extends Validator
68{
69	/**
70	 * Executes the validation
71	 */
72	public function validate(<Validation> validation, string! field) -> boolean
73	{
74		var value, format, label, message, replacePairs, code;
75
76		let value = validation->getValue(field);
77		let format = this->getOption("format");
78
79		if typeof format == "array" {
80			let format = format[field];
81		}
82
83		if empty format {
84			let format = "Y-m-d";
85		}
86
87		if !this->checkDate(value, format) {
88			let label = this->prepareLabel(validation, field),
89				message = this->prepareMessage(validation, field, "Date"),
90				code = this->prepareCode(field);
91
92			let replacePairs = [":field": label];
93
94			validation->appendMessage(
95				new Message(
96					strtr(message, replacePairs),
97					field,
98					"Date",
99					code
100				)
101			);
102
103			return false;
104		}
105
106		return true;
107	}
108
109	private function checkDate(value, format) -> boolean
110	{
111		var date, errors;
112
113		if !is_string(value) {
114			return false;
115		}
116
117		let date = \DateTime::createFromFormat(format, value);
118		let errors = \DateTime::getLastErrors();
119
120		if errors["warning_count"] > 0 || errors["error_count"] > 0 {
121			return false;
122		}
123
124		return true;
125	}
126}
127