@@ 11-57 (lines=47) @@ | ||
8 | use Assert\AssertionFailedException; |
|
9 | use InvalidArgumentException; |
|
10 | ||
11 | class Boolean extends DataTypeAbstract implements DataTypeInterface |
|
12 | { |
|
13 | const DATA_TYPE_ERROR = 'Assertion failed for value "%s" for "%s" : INVALID_TYPE'; |
|
14 | ||
15 | protected static $defaults = [ |
|
16 | 'default' => false |
|
17 | ]; |
|
18 | ||
19 | ||
20 | /** |
|
21 | * Boolean constructor. |
|
22 | * @param string $key |
|
23 | * @param mixed $datum |
|
24 | * @param array $options |
|
25 | */ |
|
26 | public function __construct(string $key, $datum, array $options = []) |
|
27 | { |
|
28 | $this->key = $key; |
|
29 | $this->datum = $datum; |
|
30 | $this->options = array_merge(self::$defaults, $options); |
|
31 | } |
|
32 | /** |
|
33 | * {@inheritdoc} |
|
34 | */ |
|
35 | public function assert() |
|
36 | { |
|
37 | try { |
|
38 | Assertion::boolean($this->datum); |
|
39 | } catch (AssertionFailedException $e) { |
|
40 | $this->errorMessageTemplate = self::DATA_TYPE_ERROR; |
|
41 | $this->throwException(); |
|
42 | } |
|
43 | return true; |
|
44 | } |
|
45 | /** |
|
46 | * {@inheritdoc} |
|
47 | */ |
|
48 | public function normalize() |
|
49 | { |
|
50 | try { |
|
51 | $this->assert(); |
|
52 | return $this->datum; |
|
53 | } catch (InvalidArgumentException $e) { |
|
54 | return $this->options['default']; |
|
55 | } |
|
56 | } |
|
57 | } |
|
58 |
@@ 11-56 (lines=46) @@ | ||
8 | use Assert\AssertionFailedException; |
|
9 | use InvalidArgumentException; |
|
10 | ||
11 | class Double extends DataTypeAbstract implements DataTypeInterface |
|
12 | { |
|
13 | const DATA_TYPE_ERROR = 'Assertion failed for value "%s" for "%s" : INVALID_TYPE'; |
|
14 | ||
15 | protected static $defaults = [ |
|
16 | 'default' => 0.0 |
|
17 | ]; |
|
18 | ||
19 | /** |
|
20 | * Boolean constructor. |
|
21 | * @param string $key |
|
22 | * @param mixed $datum |
|
23 | * @param array $options |
|
24 | */ |
|
25 | public function __construct(string $key, $datum, array $options = []) |
|
26 | { |
|
27 | $this->key = $key; |
|
28 | $this->datum = $datum; |
|
29 | $this->options = array_merge(self::$defaults, $options); |
|
30 | } |
|
31 | /** |
|
32 | * {@inheritdoc} |
|
33 | */ |
|
34 | public function assert() |
|
35 | { |
|
36 | try { |
|
37 | Assertion::float($this->datum); |
|
38 | } catch (AssertionFailedException $e) { |
|
39 | $this->errorMessageTemplate = self::DATA_TYPE_ERROR; |
|
40 | $this->throwException(); |
|
41 | } |
|
42 | return true; |
|
43 | } |
|
44 | /** |
|
45 | * {@inheritdoc} |
|
46 | */ |
|
47 | public function normalize() |
|
48 | { |
|
49 | try { |
|
50 | $this->assert(); |
|
51 | return $this->datum; |
|
52 | } catch (InvalidArgumentException $e) { |
|
53 | return $this->options['default']; |
|
54 | } |
|
55 | } |
|
56 | } |
|
57 |
@@ 11-57 (lines=47) @@ | ||
8 | use Assert\AssertionFailedException; |
|
9 | use InvalidArgumentException; |
|
10 | ||
11 | class Integer extends DataTypeAbstract implements DataTypeInterface |
|
12 | { |
|
13 | const DATA_TYPE_ERROR = 'Assertion failed for value "%s" for "%s" : INVALID_TYPE'; |
|
14 | ||
15 | protected static $defaults = [ |
|
16 | 'default' => 0 |
|
17 | ]; |
|
18 | ||
19 | ||
20 | /** |
|
21 | * Boolean constructor. |
|
22 | * @param string $key |
|
23 | * @param mixed $datum |
|
24 | * @param array $options |
|
25 | */ |
|
26 | public function __construct(string $key, $datum, array $options = []) |
|
27 | { |
|
28 | $this->key = $key; |
|
29 | $this->datum = $datum; |
|
30 | $this->options = array_merge(self::$defaults, $options); |
|
31 | } |
|
32 | /** |
|
33 | * {@inheritdoc} |
|
34 | */ |
|
35 | public function assert() |
|
36 | { |
|
37 | try { |
|
38 | Assertion::integer($this->datum); |
|
39 | } catch (AssertionFailedException $e) { |
|
40 | $this->errorMessageTemplate = self::DATA_TYPE_ERROR; |
|
41 | $this->throwException(); |
|
42 | } |
|
43 | return true; |
|
44 | } |
|
45 | /** |
|
46 | * {@inheritdoc} |
|
47 | */ |
|
48 | public function normalize() |
|
49 | { |
|
50 | try { |
|
51 | $this->assert(); |
|
52 | return $this->datum; |
|
53 | } catch (InvalidArgumentException $e) { |
|
54 | return $this->options['default']; |
|
55 | } |
|
56 | } |
|
57 | } |
|
58 |