1: <?php
2: /**
3: * Hard type support
4: * For when you absolutely want to know what you are getting
5: *
6: * @author Ashley Kitson <akitson@zf4.biz>
7: * @copyright Ashley Kitson, UK, 2014
8: * @licence GPL V3 or later : http://www.gnu.org/licenses/gpl.html
9: */
10:
11: namespace chippyash\Type;
12:
13: use chippyash\Type\Interfaces\TypeInterface;
14:
15: /**
16: * An abstract PHP type as an object
17: */
18: abstract class AbstractType implements TypeInterface
19: {
20:
21: /**
22: * Value of the type
23: *
24: * @var mixed
25: */
26: protected $value;
27:
28: /**
29: * Constructor
30: *
31: * @param mixed $value
32: */
33: public function __construct($value)
34: {
35: $this->set($value);
36: }
37:
38: /**
39: * Set the object value.
40: * Forces type
41: *
42: * @param mixed $value
43: *
44: * @return \chippyash\Type\AbstractType Fluent Interface
45: *
46: * @see typeOf
47: */
48: public function set($value)
49: {
50: $this->value = $this->typeOf($value);
51:
52: return $this;
53: }
54:
55: /**
56: * Get the value of the object typed properly
57: *
58: * @return mixed
59: */
60: public function get()
61: {
62: return $this->value;
63: }
64:
65: /**
66: * Magic invoke method
67: * Proxy to get()
68: *
69: * @see get
70: *
71: * @return mixed
72: */
73: public function __invoke()
74: {
75: return $this->get();
76: }
77:
78: /**
79: * Magic method - convert to string
80: *
81: * @return string
82: */
83: public function __toString()
84: {
85: $tmp = $this->get();
86: return (\is_string($tmp) ? $tmp : (string) $tmp);
87: }
88:
89: /**
90: * Return correctly typed value for this type
91: *
92: * @param mixed $value
93: *
94: * @return mixed
95: */
96: abstract protected function typeOf($value);
97: }
98: