Completed
Pull Request — master (#40)
by ignace nyamagana
11:26
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 3
Bugs 0 Features 0
Metric Value
c 3
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
<?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.1.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
     * new instance
36
     *
37
     * @param string|null $data the component value
38
     */
39 704
    public function __construct($data = null)
40
    {
41 704
        if ($data !== null) {
42 674
            $this->init($data);
43 596
        }
44 626
    }
45
46
    /**
47
     * Set data.
48
     *
49
     * @param mixed $data The data to set.
50
     */
51 644
    protected function init($data)
52
    {
53 644
        $data = $this->validateString($data);
54 612
        $this->data = $this->validate($data);
55 584
    }
56
57
    /**
58
     * validate the incoming data
59
     *
60
     * @param string $data
61
     *
62
     * @return string
63
     */
64 322
    protected function validate($data)
65
    {
66 322
        $data = filter_var($data, FILTER_UNSAFE_RAW, ['flags' => FILTER_FLAG_STRIP_LOW]);
67 322
        $this->assertValidComponent($data);
68
69 304
        return rawurldecode(trim($data));
70
    }
71
72
    /**
73
     * Returns the instance string representation; If the
74
     * instance is not defined an empty string is returned
75 474
     *
76
     * @return string
77 474
     */
78
    public function __toString()
79
    {
80
        return static::encode((string) $this->data);
81
    }
82
83 52
    /**
84
     * Returns the instance string representation
85 52
     * with its optional URI delimiters
86
     *
87
     * @return string
88
     */
89
    public function getUriComponent()
90
    {
91
        return $this->__toString();
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public static function __set_state(array $properties)
98
    {
99
        return new static($properties['data']);
100
    }
101
}
102