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

Regex::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 12
rs 9.4285
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