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\Meditation\Constraints;
13:
14: use Chromabits\Nucleus\Meditation\Exceptions\UnknownTypeException;
15: use Chromabits\Nucleus\Meditation\TypeHound;
16:
17: /**
18: * Class PrimitiveTypeConstraint.
19: *
20: * @author Eduardo Trujillo <ed@chromabits.com>
21: * @package Chromabits\Nucleus\Meditation\Constraints
22: */
23: class PrimitiveTypeConstraint extends AbstractTypeConstraint
24: {
25: protected $expectedType;
26:
27: /**
28: * Construct an instance of a PrimitiveConstant.
29: *
30: * @param string $typeName
31: */
32: public function __construct($typeName)
33: {
34: parent::__construct();
35:
36: // TODO: Validate the typeName field
37:
38: $this->expectedType = $typeName;
39: }
40:
41: /**
42: * Construct an instance of a PrimitiveConstant.
43: *
44: * @param string $typeName
45: *
46: * @return static
47: */
48: public static function forType($typeName)
49: {
50: return new static($typeName);
51: }
52:
53: /**
54: * Check if the constraint is met.
55: *
56: * @param mixed $value
57: * @param array $context
58: *
59: * @throws UnknownTypeException
60: * @return mixed
61: */
62: public function check($value, array $context = [])
63: {
64: $hound = new TypeHound($value);
65:
66: return $this->expectedType === $hound->resolve();
67: }
68:
69: /**
70: * Get string representation of this constraint.
71: *
72: * @return mixed
73: */
74: public function toString()
75: {
76: return $this->expectedType;
77: }
78: }
79: