Completed
Push — master ( 3089f7...b04f5d )
by ignace nyamagana
10s
created

AbstractComponent::__set_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 component literal value. The return type can be
82
     * <ul>
83
     * <li> null: If the component is not defined
84
     * <li> int: If the component is a defined port
85
     * <li> string: Otherwise
86
     * </ul>
87
     *
88
     * @return string|int|null
89
     */
90 719
    public function getContent()
91
    {
92 719
        if (null === $this->data) {
93 516
            return null;
94
        }
95
96 668
        return $this->encode($this->data);
97
    }
98
99
    /**
100
     * Returns the instance string representation; If the
101
     * instance is not defined an empty string is returned
102
     *
103
     * @return string
104
     */
105 957
    public function __toString()
106
    {
107 957
        return (string) $this->getContent();
108
    }
109
110
    /**
111
     * Returns the instance string representation
112
     * with its optional URI delimiters
113
     *
114
     * @return string
115
     */
116 135
    public function getUriComponent()
117
    {
118 135
        return $this->__toString();
119
    }
120
121
    /**
122
     * Returns an instance with the specified string
123
     *
124
     * This method MUST retain the state of the current instance, and return
125
     * an instance that contains the modified data
126
     *
127
     * @param string $value
128
     *
129
     * @return static
130
     */
131 291
    public function modify($value)
132
    {
133 291
        if ($value === $this->getContent()) {
134 165
            return $this;
135
        }
136
137 237
        return new static($value);
138
    }
139
}
140