Code Duplication    Length = 44-48 lines in 4 locations

src/DataType/Boolean.php 1 location

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

src/DataType/Double.php 1 location

@@ 9-52 (lines=44) @@
6
use Selami\Entity\Interfaces\DataTypeInterface;
7
use InvalidArgumentException;
8
9
class Double extends DataTypeAbstract implements DataTypeInterface
10
{
11
    const DATA_TYPE_ERROR   = 'Assertion failed for value "%s" for "%s" : INVALID_TYPE';
12
13
    protected static $defaults = [
14
        'default' => 0.0
15
    ];
16
17
    /**
18
     * Double 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
        if (!is_float($this->datum)) {
35
            $this->errorMessageTemplate = self::DATA_TYPE_ERROR;
36
            $this->throwException();
37
        }
38
        return true;
39
    }
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function normalize()
44
    {
45
        try {
46
            $this->assert();
47
            return $this->datum;
48
        } catch (InvalidArgumentException $e) {
49
            return $this->options['default'];
50
        }
51
    }
52
}
53

src/DataType/Email.php 1 location

@@ 9-56 (lines=48) @@
6
use Selami\Entity\Interfaces\DataTypeInterface;
7
use InvalidArgumentException;
8
9
class Email extends DataTypeAbstract implements DataTypeInterface
10
{
11
    const DATA_FORMAT_ERROR   = 'Assertion failed for value "%s" for "%s" : INVALID_MAIL_ADDRESS_FORMAT';
12
13
    protected static $defaults = [
14
        'default'   => ''
15
    ];
16
17
18
    /**
19
     * Email constructor.
20
     * @param string $key
21
     * @param mixed $datum
22
     * @param array $options
23
     * @throws validArgumentException
24
     */
25
    public function __construct(string $key, $datum, array $options = [])
26
    {
27
        $this->key = $key;
28
        $this->datum = $datum;
29
        $this->checkValidOptions($options);
30
        $this->options = array_merge(self::$defaults, $options);
31
    }
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function assert()
36
    {
37
        if (filter_var($this->datum, FILTER_VALIDATE_EMAIL) === false) {
38
            $this->errorMessageTemplate = self::DATA_FORMAT_ERROR;
39
            $this->throwException();
40
        }
41
        return true;
42
    }
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

src/DataType/Integer.php 1 location

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