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

Fragment   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 1 Features 0
Metric Value
wmc 14
c 10
b 1
f 0
lcom 1
cbo 1
dl 0
loc 105
ccs 29
cts 29
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 8 2
A __set_state() 0 7 1
A __debugInfo() 0 4 1
A getContent() 0 8 3
A getUriComponent() 0 9 2
A modify() 0 12 4
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\Interfaces\Fragment as FragmentInterface;
15
16
/**
17
 * Value object representing a URI fragment component.
18
 *
19
 * @package League.uri
20
 * @author  Ignace Nyamagana Butera <[email protected]>
21
 * @since   1.0.0
22
 */
23
class Fragment extends AbstractComponent implements FragmentInterface
24
{
25
26
    /**
27
     * Preserve the delimiter
28
     *
29
     * @var bool
30
     */
31
    protected $preserveDelimiter = false;
32
33
    /**
34
     * new instance
35
     *
36
     * @param string|null $data the component value
37
     */
38 760
    public function __construct($data = null)
39
    {
40 760
        $this->data = $this->validate($data);
41 748
        $this->preserveDelimiter = null !== $data;
42 748
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 760
    protected function validate($data)
48
    {
49 760
        if (null === $data) {
50 438
            return $data;
51
        }
52
53 409
        return $this->decodeComponent($this->validateString($data));
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 12
    public static function __set_state(array $properties)
60
    {
61 12
        $component = new static($properties['data']);
62 12
        $component->preserveDelimiter = $properties['preserveDelimiter'];
63
64 12
        return $component;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 2
    public function __debugInfo()
71
    {
72 2
        return ['fragment' => $this->__toString()];
73
    }
74
75
    /**
76
     * Returns the component literal value
77
     *
78
     * @return string|null
79
     */
80 766
    public function getContent()
81
    {
82 766
        if (null === $this->data && false === $this->preserveDelimiter) {
83 468
            return null;
84
        }
85
86 397
        return $this->encodeQueryFragment($this->data);
87
    }
88
89
    /**
90
     * Returns the instance string representation
91
     * with its optional URI delimiters
92
     *
93
     * @return string
94
     */
95 761
    public function getUriComponent()
96
    {
97 761
        $component = $this->__toString();
98 761
        if ($this->preserveDelimiter) {
99 395
            return FragmentInterface::DELIMITER.$component;
100
        }
101
102 465
        return $component;
103
    }
104
105
    /**
106
     * Returns an instance with the specified string
107
     *
108
     * This method MUST retain the state of the current instance, and return
109
     * an instance that contains the modified data
110
     *
111
     * @param string $value
112
     *
113
     * @return static
114
     */
115 9
    public function modify($value)
116
    {
117 9
        if (null === $value && $value === $this->getContent()) {
118 3
            return $this;
119
        }
120
121 6
        if ($value === $this->__toString()) {
122 3
            return $this;
123
        }
124
125 3
        return new static($value);
126
    }
127
}
128