Code Duplication    Length = 44-44 lines in 2 locations

src/DataType/Boolean.php 1 location

@@ 11-54 (lines=44) @@
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
17
    /**
18
     * Boolean constructor.
19
     * @param string $key
20
     * @param mixed $datum
21
     * @param array $options
22
     */
23
    public function __construct(string $key, $datum, array $options = [])
24
    {
25
        $this->key = $key;
26
        $this->datum = $datum;
27
        $this->options = array_merge(self::$defaults, $options);
28
    }
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function assert()
33
    {
34
        try {
35
            Assertion::boolean($this->datum);
36
        } catch (AssertionFailedException $e) {
37
            $this->errorMessageTemplate = self::DATA_TYPE_ERROR;
38
            $this->throwException();
39
        }
40
        return true;
41
    }
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function normalize()
46
    {
47
        try {
48
            $this->assert();
49
            return $this->datum;
50
        } catch (InvalidArgumentException $e) {
51
            return $this->options['default'];
52
        }
53
    }
54
}
55

src/DataType/Integer.php 1 location

@@ 11-54 (lines=44) @@
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
17
    /**
18
     * Boolean constructor.
19
     * @param string $key
20
     * @param mixed $datum
21
     * @param array $options
22
     */
23
    public function __construct(string $key, $datum, array $options = [])
24
    {
25
        $this->key = $key;
26
        $this->datum = $datum;
27
        $this->options = array_merge(self::$defaults, $options);
28
    }
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function assert()
33
    {
34
        try {
35
            Assertion::integer($this->datum);
36
        } catch (AssertionFailedException $e) {
37
            $this->errorMessageTemplate = self::DATA_TYPE_ERROR;
38
            $this->throwException();
39
        }
40
        return true;
41
    }
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function normalize()
46
    {
47
        try {
48
            $this->assert();
49
            return $this->datum;
50
        } catch (InvalidArgumentException $e) {
51
            return $this->options['default'];
52
        }
53
    }
54
}
55