1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Validation\Constraints;
13:
14: use Chromabits\Nucleus\Meditation\Constraints\AbstractConstraint;
15:
16: /**
17: * Class RegexConstraint.
18: *
19: * @author Eduardo Trujillo <ed@chromabits.com>
20: * @package Chromabits\Nucleus\Validation\Constraints
21: */
22: class RegexConstraint extends AbstractConstraint
23: {
24: /**
25: * @var string
26: */
27: private $pattern;
28:
29: /**
30: * Construct an instance of a RegexConstraint.
31: *
32: * @param string $pattern
33: */
34: public function __construct($pattern)
35: {
36: parent::__construct();
37:
38: $this->pattern = $pattern;
39: }
40:
41: /**
42: * Check if the constraint is met.
43: *
44: * @param mixed $value
45: * @param array $context
46: *
47: * @return mixed
48: */
49: public function check($value, array $context = [])
50: {
51: return preg_match($this->pattern, $value) === 1;
52: }
53:
54: /**
55: * Get string representation of this constraint.
56: *
57: * @return mixed
58: */
59: public function toString()
60: {
61: return '{regex}';
62: }
63: }
64: