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\Mvc\Model\Validator;
21
22use Phalcon\Mvc\EntityInterface;
23use Phalcon\Mvc\Model\Exception;
24use Phalcon\Mvc\Model\Validator;
25
26/**
27 * Phalcon\Mvc\Model\Validator\Url
28 *
29 * Allows to validate if a field has a url format
30 *
31 * This validator is only for use with Phalcon\Mvc\Collection. If you are using
32 * Phalcon\Mvc\Model, please use the validators provided by Phalcon\Validation.
33 *
34 *<code>
35 * use Phalcon\Mvc\Model\Validator\Url as UrlValidator;
36 *
37 * class Posts extends \Phalcon\Mvc\Collection
38 * {
39 *     public function validation()
40 *     {
41 *         $this->validate(
42 *             new UrlValidator(
43 *                 [
44 *                     "field" => "source_url",
45 *                 ]
46 *             )
47 *         );
48 *
49 *         if ($this->validationHasFailed() === true) {
50 *             return false;
51 *         }
52 *     }
53 * }
54 *</code>
55 *
56 * @deprecated 3.1.0
57 * @see Phalcon\Validation\Validator\Url
58 */
59class Url extends Validator
60{
61	/**
62	 * Executes the validator
63	 */
64	public function validate(<EntityInterface> record) -> boolean
65	{
66		var field, value, message;
67
68		let field = this->getOption("field");
69		if typeof field != "string" {
70			throw new Exception("Field name must be a string");
71		}
72
73		let value = record->readAttribute(field);
74		if this->isSetOption("allowEmpty") && empty value {
75			return true;
76		}
77
78		/**
79		 * Filters the format using FILTER_VALIDATE_URL
80		 */
81		if !filter_var(value, FILTER_VALIDATE_URL) {
82
83			/**
84			 * Check if the developer has defined a custom message
85			 */
86			let message = this->getOption("message");
87			if empty message {
88				let message = ":field does not have a valid url format";
89			}
90
91			this->appendMessage(strtr(message, [":field": field]), field, "Url");
92			return false;
93		}
94
95		return true;
96	}
97}
98