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\Boa;
15: use Chromabits\Nucleus\Meditation\Constraints\AbstractConstraint;
16: use Chromabits\Nucleus\Support\Std;
17:
18: /**
19: * Class DomainConstraint.
20: *
21: * @author Eduardo Trujillo <ed@chromabits.com>
22: * @package Chromabits\Nucleus\Validation\Constraints
23: */
24: class DomainConstraint extends AbstractConstraint
25: {
26: /**
27: * Check if the constraint is met.
28: *
29: * @param mixed $value
30: * @param array $context
31: *
32: * @return mixed
33: */
34: public function check($value, array $context = [])
35: {
36: return Std::falsy(
37: Boa::string()->check($value, $context),
38: (new RegexConstraint(
39: '/^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$/'
40: ))->check($value, $context)
41: );
42: }
43:
44: /**
45: * Get string representation of this constraint.
46: *
47: * @return mixed
48: */
49: public function toString()
50: {
51: return '{domain}';
52: }
53:
54: /**
55: * Get the description of the constraint.
56: *
57: * @return string
58: */
59: public function getDescription()
60: {
61: return 'The value is expected to be a valid domain name.';
62: }
63: }
64: