Completed
Push — master ( 4de594...21f32b )
by ignace nyamagana
04:46
created

AbstractComponent::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
AbstractComponent::validate() 0 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 1205
    public function __construct($data = null)
48
    {
49 1205
        $this->data = $this->validate($data);
50 1121
    }
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
    abstract protected function validate($data);
62
63
    /**
64
     * The component raw data
65
     *
66
     * @return string|null
67
     */
68 859
    public function getContent()
69
    {
70 859
        return $this->data;
71
    }
72
73
    /**
74
     * Returns the instance string representation; If the
75
     * instance is not defined an empty string is returned
76
     *
77
     * @return string
78
     */
79 832
    public function __toString()
80
    {
81 832
        return (string) $this->getContent();
82
    }
83
84
    /**
85
     * Returns the instance string representation
86
     * with its optional URI delimiters
87
     *
88
     * @return string
89
     */
90 180
    public function getUriComponent()
91
    {
92 180
        return $this->__toString();
93
    }
94
95
    /**
96
     * Returns an instance with the specified string
97
     *
98
     * This method MUST retain the state of the current instance, and return
99
     * an instance that contains the modified data
100
     *
101
     * @param string $value
102
     *
103
     * @return static
104
     */
105 387
    public function modify($value)
106
    {
107 387
        if ($value === $this->getContent()) {
108 258
            return $this;
109
        }
110
111 339
        return new static($value);
112
    }
113
}
114