Completed
Push — master ( 5a1f4e...aa9408 )
by ignace nyamagana
12:29
created

AbstractComponent::__toString()   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 5
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 5
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 1426
    public function __construct($data = null)
48
    {
49 1426
        $this->data = $this->validate($data);
50 1330
    }
51
52
    /**
53
     * Validate the component string
54
     *
55
     * @param mixed $data
56
     *
57
     * @throws InvalidArgumentException if the component is no valid
58
     *
59
     * @return mixed
60
     */
61 1090
    protected function validate($data)
62
    {
63 1090
        if (null === $data) {
64 777
            return $data;
65
        }
66
67 691
        return $this->decodeComponent($this->validateString($data));
68
    }
69
70
71
    /**
72
     * The component raw data
73
     *
74
     * @return mixed
75
     */
76 834
    public function getContent()
77
    {
78 834
        return $this->data;
79
    }
80
81
    /**
82
     * Returns the instance string representation; If the
83
     * instance is not defined an empty string is returned
84
     *
85
     * @return string
86
     */
87 1224
    public function __toString()
88
    {
89 1224
        return (string) $this->getContent();
90
    }
91
92
    /**
93
     * Returns the instance string representation
94
     * with its optional URI delimiters
95
     *
96
     * @return string
97
     */
98 183
    public function getUriComponent()
99
    {
100 183
        return $this->__toString();
101
    }
102
103
    /**
104
     * Returns an instance with the specified string
105
     *
106
     * This method MUST retain the state of the current instance, and return
107
     * an instance that contains the modified data
108
     *
109
     * @param string $value
110
     *
111
     * @return static
112
     */
113 435
    public function withContent($value = null)
114
    {
115 435
        if ($value === $this->getContent()) {
116 306
            return $this;
117
        }
118
119 366
        return new static($value);
120
    }
121
122
    /**
123
     * DEPRECATION WARNING! This method will be removed in the next major point release
124
     *
125
     * @deprecated deprecated since version 4.2
126
     *
127
     * @see withContent
128
     *
129
     * Returns an instance with the specified string
130
     *
131
     * This method MUST retain the state of the current instance, and return
132
     * an instance that contains the modified data
133
     *
134
     * @param string $value
135
     *
136
     * @return static
137
     */
138
    public function modify($value)
139
    {
140
        return $this->withContent($value);
141
    }
142
}
143