Completed
Pull Request — master (#58)
by ignace nyamagana
03:41
created

AbstractComponent::modify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2013-2015 Ignace Nyamagana Butera
8
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
9
 * @version   4.2.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Components;
13
14
use League\Uri\Types\ImmutableComponentTrait;
15
16
/**
17
 * An abstract class to ease component manipulation
18
 *
19
 * @package League.uri
20
 * @author  Ignace Nyamagana Butera <[email protected]>
21
 * @since   4.0.0
22
 */
23
abstract class AbstractComponent
24
{
25
    use ImmutableComponentTrait;
26
27
    /**
28
     * The component data
29
     *
30
     * @var int|string
31
     */
32
    protected $data;
33
34
    /**
35
     * @inheritdoc
36
     */
37 12
    public static function __set_state(array $properties)
38
    {
39 12
        return new static($properties['data']);
40
    }
41
42
    /**
43
     * new instance
44
     *
45
     * @param string|null $data the component value
46
     */
47 1063
    public function __construct($data = null)
48
    {
49 1063
        if ($data !== null) {
50 1021
            $this->init($data);
51 916
        }
52 958
    }
53
54
    /**
55
     * Set data.
56
     *
57
     * @param mixed $data The data to set.
58
     */
59 1006
    protected function init($data)
60
    {
61 1006
        $data = $this->validateString($data);
62 958
        $this->data = $this->validate($data);
63 916
    }
64
65
    /**
66
     * validate the incoming data
67
     *
68
     * @param string $data
69
     *
70
     * @return string
71
     */
72 515
    protected function validate($data)
73
    {
74 515
        $data = filter_var($data, FILTER_UNSAFE_RAW, ['flags' => FILTER_FLAG_STRIP_LOW]);
75 515
        $this->assertValidComponent($data);
76
77 488
        return rawurldecode(trim($data));
78
    }
79
80
    /**
81
     * Returns the instance string representation; If the
82
     * instance is not defined an empty string is returned
83
     *
84
     * @return string
85
     */
86 751
    public function __toString()
87
    {
88 751
        return $this->encode($this->data);
89
    }
90
91
    /**
92
     * Returns the instance string representation
93
     * with its optional URI delimiters
94
     *
95
     * @return string
96
     */
97 78
    public function getUriComponent()
98
    {
99 78
        return $this->__toString();
100
    }
101
102
    /**
103
     * Returns an instance with the specified string
104
     *
105
     * This method MUST retain the state of the current instance, and return
106
     * an instance that contains the modified data
107
     *
108
     * @param string $value
109
     *
110
     * @return static
111
     */
112 291
    public function modify($value)
113
    {
114 291
        if ($value == $this->__toString()) {
115 165
            return $this;
116
        }
117
118 237
        return new static($value);
119
    }
120
}
121