1 | <?php |
||
19 | abstract class Scalar extends AbstractField |
||
20 | { |
||
21 | const ENDIAN_BIG = 1; |
||
22 | const ENDIAN_LITTLE = 2; |
||
23 | |||
24 | /** |
||
25 | * @var int|string|callable Size of field in bits/bytes, path to value in DataSet or callback. |
||
26 | */ |
||
27 | protected $size; |
||
28 | |||
29 | /** |
||
30 | * @var callable Callback that changes value of the field. |
||
31 | */ |
||
32 | protected $formatter; |
||
33 | |||
34 | /** |
||
35 | * @var int Field endian. |
||
36 | * @since 1.0 |
||
37 | */ |
||
38 | protected $endian; |
||
39 | |||
40 | /** |
||
41 | * @var array Human names of some common used sizes. |
||
42 | */ |
||
43 | private $sizes = [ |
||
44 | 'BIT' => 1, |
||
45 | 'SEMI_NIBBLE' => 2, |
||
46 | 'NIBBLE' => 4, |
||
47 | 'BYTE' => 8, |
||
48 | 'SHORT' => 16, |
||
49 | 'WORD' => 32, |
||
50 | 'DWORD' => 64, |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Read part of data from source and return value in necessary format. |
||
55 | * |
||
56 | * This is abstract method, so each implementation should return it's own |
||
57 | * type of value. |
||
58 | * |
||
59 | * @param AbstractStream $stream Stream from which read. |
||
60 | * @return int|string|null Value type depend by implementation. |
||
61 | */ |
||
62 | abstract public function read(AbstractStream $stream); |
||
63 | |||
64 | |||
65 | 50 | public function __construct($size, $options = []) |
|
74 | |||
75 | /** |
||
76 | * Return final value of size. |
||
77 | * |
||
78 | * If size was set as DataSet path or callback, it will be processed here. |
||
79 | * |
||
80 | * @return int Final value of size. |
||
81 | * @throws ConfigurationException If the value was less than zero. |
||
82 | */ |
||
83 | 30 | public function getSize() |
|
93 | |||
94 | /** |
||
95 | * Process and sets size. |
||
96 | * |
||
97 | * Size can be represented as a string containing on of size key words {@see $sizes}. |
||
98 | * Also you can set path to already parsed value in DataSet. |
||
99 | * |
||
100 | * @param int|string $size Size in bits/bytes or DataSet path. |
||
101 | * @return static For chaining. |
||
102 | */ |
||
103 | 50 | public function setSize($size) |
|
112 | |||
113 | /** |
||
114 | * Getter for the value callback. |
||
115 | * |
||
116 | * @return callable |
||
117 | */ |
||
118 | 1 | public function getFormatter() |
|
122 | |||
123 | /** |
||
124 | * Setter for the value callback. |
||
125 | * |
||
126 | * @param callable $formatter |
||
127 | * @return $this |
||
128 | */ |
||
129 | 3 | public function setFormatter($formatter) |
|
134 | |||
135 | /** |
||
136 | * @return int |
||
137 | * @since 1.0 |
||
138 | */ |
||
139 | 25 | public function getEndian() |
|
143 | |||
144 | /** |
||
145 | * @param int $endian |
||
146 | * @since 1.0 |
||
147 | * @return $this |
||
148 | */ |
||
149 | 4 | public function setEndian($endian) |
|
159 | |||
160 | /** |
||
161 | * Reads and process value from Stream. |
||
162 | * |
||
163 | * @api |
||
164 | * @param AbstractStream $stream Stream from which read. |
||
165 | * @return mixed The final value. |
||
166 | */ |
||
167 | 16 | public function parse(AbstractStream $stream) |
|
173 | |||
174 | /** |
||
175 | * Applies value formatter to read value. |
||
176 | * |
||
177 | * @param int|string|null $value Read value. |
||
178 | * @return mixed Processed value. |
||
179 | */ |
||
180 | 16 | private function format($value) |
|
187 | |||
188 | /** |
||
189 | * Process given string and return appropriate size value. |
||
190 | * |
||
191 | * @param $word |
||
192 | * @return int |
||
193 | */ |
||
194 | 15 | private function parseSizeWord($word) |
|
204 | } |