1: <?php
2: /**
3: * Wei Framework
4: *
5: * @copyright Copyright (c) 2008-2015 Twin Huang
6: * @license http://opensource.org/licenses/mit-license.php MIT License
7: */
8:
9: namespace Wei\Validator;
10:
11: /**
12: * Check if the input is valid URL address
13: *
14: * @author Twin Huang <twinhuang@qq.com>
15: */
16: class Url extends BaseValidator
17: {
18: protected $invalidMessage = '%name% must be valid URL';
19:
20: protected $negativeMessage = '%name% must not be URL';
21:
22: /**
23: * Requires the URL to contain a path part (like http://www.example.com/path/part)
24: *
25: * @var bool
26: */
27: protected $path = false;
28:
29: /**
30: * Requires URL to have a query string (like http://www.example.com/?query=string)
31: *
32: * @var bool
33: */
34: protected $query = false;
35:
36: /**
37: * Check if the input is valid URL address, options could be "path" and "query"
38: *
39: * @param mixed $input
40: * @param array $options
41: * @return string|bool
42: */
43: public function __invoke($input, $options = array())
44: {
45: $options && $this->storeOption($options);
46:
47: return $this->isValid($input);
48: }
49:
50: /**
51: * {@inheritdoc}
52: */
53: protected function doValidate($input)
54: {
55: $flag = 0;
56: if ($this->path) {
57: $flag = $flag | FILTER_FLAG_PATH_REQUIRED;
58: }
59: if ($this->query) {
60: $flag = $flag | FILTER_FLAG_QUERY_REQUIRED;
61: }
62:
63: if (!filter_var($input, FILTER_VALIDATE_URL, $flag)) {
64: $this->addError('invalid');
65: return false;
66: }
67:
68: return true;
69: }
70: }
71: