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\Url
28 *
29 * Checks if a value has a url format
30 *
31 * <code>
32 * use Phalcon\Validation;
33 * use Phalcon\Validation\Validator\Url as UrlValidator;
34 *
35 * $validator = new Validation();
36 *
37 * $validator->add(
38 *     "url",
39 *     new UrlValidator(
40 *         [
41 *             "message" => ":field must be a url",
42 *         ]
43 *     )
44 * );
45 *
46 * $validator->add(
47 *     [
48 *         "url",
49 *         "homepage",
50 *     ],
51 *     new UrlValidator(
52 *         [
53 *             "message" => [
54 *                 "url"      => "url must be a url",
55 *                 "homepage" => "homepage must be a url",
56 *             ]
57 *         ]
58 *     )
59 * );
60 * </code>
61 */
62class Url 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 !filter_var(value, FILTER_VALIDATE_URL) {
75			let label = this->prepareLabel(validation, field),
76				message = this->prepareMessage(validation, field, "Url"),
77				code = this->prepareCode(field);
78
79			let replacePairs = [":field": label];
80
81			validation->appendMessage(
82				new Message(
83					strtr(message, replacePairs),
84					field,
85					"Url",
86					code
87				)
88			);
89
90			return false;
91		}
92
93		return true;
94	}
95}
96