Code Duplication    Length = 46-53 lines in 3 locations

src/DataType/FilePath.php 1 location

@@ 9-55 (lines=47) @@
6
use Selami\Entity\Interfaces\DataTypeInterface;
7
use InvalidArgumentException;
8
9
class FilePath 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
     * Slug 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->checkValidOptions($options);
28
        $this->options = array_merge(self::$defaults, $options);
29
    }
30
    
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function assert()
35
    {
36
        if (!preg_match('#^[a-zA-Z0-9/._-]+$#', $this->datum)) {
37
            $this->errorMessageTemplate = self::DATA_TYPE_ERROR;
38
            $this->throwException();
39
        }
40
        return true;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function normalize()
47
    {
48
        try {
49
            $this->assert();
50
            return filter_var($this->datum, FILTER_SANITIZE_URL);
51
        } catch (InvalidArgumentException $e) {
52
            return $this->options['default'];
53
        }
54
    }
55
}
56

src/DataType/Html.php 1 location

@@ 9-61 (lines=53) @@
6
use Selami\Entity\Interfaces\DataTypeInterface;
7
use InvalidArgumentException;
8
9
class Html extends DataTypeAbstract implements DataTypeInterface
10
{
11
    const DATA_TYPE_ERROR   = 'Assertion failed for value "%s" for "%s" : INVALID_TYPE';
12
    const DATA_LENGTH       = 'Assertion failed for value "%s" for "%s" : INVALID_TEXT_LENGTH';
13
14
    protected static $defaults = [
15
        'default'   => null
16
    ];
17
18
    /**
19
     * Text 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
        $this->isString();
38
        return true;
39
    }
40
41
    private function isString()
42
    {
43
        if (!is_string($this->datum)) {
44
            $this->errorMessageTemplate = self::DATA_TYPE_ERROR;
45
            $this->throwException();
46
        }
47
        return true;
48
    }
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function normalize()
53
    {
54
        try {
55
            $this->assert();
56
            return (string) $this->datum;
57
        } catch (InvalidArgumentException $e) {
58
            return $this->options['default'];
59
        }
60
    }
61
}
62

src/DataType/Slug.php 1 location

@@ 9-54 (lines=46) @@
6
use Selami\Entity\Interfaces\DataTypeInterface;
7
use InvalidArgumentException;
8
9
class Slug 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
     * Slug 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 (!preg_match('/^[a-z0-9](-?[a-z0-9]+)*$/i', $this->datum)) {
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