Completed
Push — master ( 1a3db9...c5a357 )
by Mehmet
02:19
created

Regex   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
rs 10
wmc 3
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami\Entity\DataType;
5
6
use Selami\Entity\Interfaces\DataTypeInterface;
7
use InvalidArgumentException;
8
9
class Regex implements DataTypeInterface
10
{
11
    use DataTypeRegexTrait;
12
13
    const DATA_FORMAT_ERROR   = 'Assertion failed for value "%s" for "%s" : INVALID_FORMAT';
14
15
    protected $regex;
16
    protected $sanitizeFlags;
17
    protected static $defaults = [
18
        'default'       => null,
19
        'regex'         => null,
20
        'sanitize_flag' => null
21
    ];
22
23
    /**
24
     * Regex constructor.
25
     * @param string $key
26
     * @param mixed $datum
27
     * @param array $options
28
     * @throws InvalidArgumentException
29
     */
30
    public function __construct(string $key, $datum, array $options = [])
31
    {
32
        if (!isset($options['regex']) || empty($options['regex'])) {
33
            throw new InvalidArgumentException('$options[regex] can\'t be empty and must be a valid regex');
34
        }
35
        $this->key = $key;
36
        $this->datum = $datum;
37
        $this->regex = $options['regex'];
38
        $this->sanitizeFlags = $options['sanitize_flag'] ?? null;
39
        $this->checkValidOptions($options);
40
        $this->options = array_merge(self::$defaults, $options);
41
    }
42
}
43