1
2/*
3 +------------------------------------------------------------------------+
4 | Phalcon Framework                                                      |
5 +------------------------------------------------------------------------+
6 | Copyright (c) 2011-2017 Phalcon Team (https://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\Message;
24use Phalcon\Validation\Validator;
25
26/**
27 * Phalcon\Validation\Validator\Digit
28 *
29 * Check for numeric character(s)
30 *
31 * <code>
32 * use Phalcon\Validation;
33 * use Phalcon\Validation\Validator\Digit as DigitValidator;
34 *
35 * $validator = new Validation();
36 *
37 * $validator->add(
38 *     "height",
39 *     new DigitValidator(
40 *         [
41 *             "message" => ":field must be numeric",
42 *         ]
43 *     )
44 * );
45 *
46 * $validator->add(
47 *     [
48 *         "height",
49 *         "width",
50 *     ],
51 *     new DigitValidator(
52 *         [
53 *             "message" => [
54 *                 "height" => "height must be numeric",
55 *                 "width"  => "width must be numeric",
56 *             ],
57 *         ]
58 *     )
59 * );
60 * </code>
61 */
62class Digit extends Validator
63{
64
65	/**
66	 * Executes the validation
67	 */
68	public function validate(<Validation> validation, string! field) -> boolean
69	{
70		var value, message, label, replacePairs, code;
71
72		let value = validation->getValue(field);
73
74		if is_int(value) || ctype_digit(value) {
75			return true;
76		}
77
78		let label = this->prepareLabel(validation, field),
79			message = this->prepareMessage(validation, field, "Digit"),
80			code = this->prepareCode(field);
81
82		let replacePairs = [":field": label];
83
84		validation->appendMessage(
85			new Message(
86				strtr(message, replacePairs),
87				field,
88				"Digit",
89				code
90			)
91		);
92
93		return false;
94	}
95}
96