OptionRegex   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setRegex() 0 4 1
A setMessage() 0 4 1
A setValue() 0 10 2
1
<?php
2
3
/*
4
 * A PSR7 aware cURL client (https://github.com/juliangut/spiral).
5
 *
6
 * @license BSD-3-Clause
7
 * @link https://github.com/juliangut/spiral
8
 * @author Julián Gutiérrez <[email protected]>
9
 */
10
11
namespace Jgut\Spiral\Option;
12
13
use Jgut\Spiral\Exception\OptionException;
14
15
/**
16
 * Regex cURL option wrapper.
17
 */
18
class OptionRegex extends DefaultOption
19
{
20
    /**
21
     * Regex to check.
22
     *
23
     * @var string
24
     */
25
    protected $regex = '/^$/';
26
27
    /**
28
     * Error message.
29
     *
30
     * @var string
31
     */
32
    protected $message = '"%s" is not a valid value';
33
34
    /**
35
     * Create regex cURL option.
36
     *
37
     * @param int $option
38
     */
39
    public function __construct($option)
40
    {
41
        parent::__construct($option);
42
43
        $this->value = '';
44
    }
45
46
    /**
47
     * Set regex expression.
48
     *
49
     * @param $regex
50
     */
51
    public function setRegex($regex)
52
    {
53
        $this->regex = $regex;
54
    }
55
56
    /**
57
     * Set fail message.
58
     *
59
     * @param $message
60
     */
61
    public function setMessage($message)
62
    {
63
        $this->message = $message;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     *
69
     * @param string $value
70
     *
71
     * @throws OptionException
72
     */
73
    public function setValue($value)
74
    {
75
        $value = trim($value);
76
77
        if (!preg_match($this->regex, $value)) {
78
            throw new OptionException(sprintf($this->message, $value));
79
        }
80
81
        $this->value = $value;
82
    }
83
}
84