1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
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: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Validates a value using a regular expression.
16: *
17: * @category Jyxo
18: * @package Jyxo_Input
19: * @subpackage Validator
20: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
21: * @license https://github.com/jyxo/php/blob/master/license.txt
22: * @author Jan Pěček
23: */
24: class Jyxo_Input_Validator_Regex extends Jyxo_Input_Validator_AbstractValidator
25: {
26:
27: /**
28: * Regular expression.
29: *
30: * @var string
31: */
32: protected $pattern;
33:
34:
35: /**
36: * Constructor.
37: *
38: * @param string $pattern Regular expression
39: */
40: public function __construct($pattern)
41: {
42: $this->setPattern($pattern);
43: }
44:
45: /**
46: * Sets the validation regular expression.
47: *
48: * @param string $pattern Regular expression
49: * @return Jyxo_Input_Validator_Regex
50: * @throws Jyxo_Input_Validator_Exception On empty regular expression
51: */
52: public function setPattern($pattern)
53: {
54: if (empty($pattern)) {
55: throw new Jyxo_Input_Validator_Exception('Pattern could not be empty');
56: }
57: $this->pattern = (string) $pattern;
58:
59: return $this;
60: }
61:
62: /**
63: * Returns the validation regular expression.
64: *
65: * @return string
66: */
67: public function getPattern()
68: {
69: return $this->pattern;
70: }
71:
72: /**
73: * Validates a value.
74: *
75: * @param mixed $value Input value
76: * @return boolean
77: */
78: public function isValid($value)
79: {
80: if (!preg_match($this->getPattern(), (string) $value)) {
81: return false;
82: }
83:
84: return true;
85: }
86:
87: }
88: