Code Duplication    Length = 46-48 lines in 2 locations

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 filter_var($this->datum, FILTER_SANITIZE_EMAIL);
52
        } catch (InvalidArgumentException $e) {
53
            return filter_var($this->options['default'], FILTER_SANITIZE_EMAIL);
54
        }
55
    }
56
}
57

src/DataType/Url.php 1 location

@@ 9-54 (lines=46) @@
6
use Selami\Entity\Interfaces\DataTypeInterface;
7
use InvalidArgumentException;
8
9
class Url 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' => null
15
    ];
16
17
18
    /**
19
     * Url 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->checkValidOptions($options);
29
        $this->options = array_merge(self::$defaults, $options);
30
    }
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function assert()
35
    {
36
        if (!filter_var($this->datum, FILTER_VALIDATE_URL)) {
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 filter_var($this->datum, FILTER_SANITIZE_URL);
50
        } catch (InvalidArgumentException $e) {
51
            return $this->options['default'];
52
        }
53
    }
54
}
55