Completed
Push — master ( fb692e...c26f50 )
by ignace nyamagana
03:48
created

Fragment::getValue()   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 0
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\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 784
    public function __construct($data = null)
39
    {
40 784
        $this->data = $this->validate($data);
41 772
        $this->preserveDelimiter = null !== $data;
42 772
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 12
    public static function __set_state(array $properties)
48
    {
49 12
        $component = new static($properties['data']);
50 12
        $component->preserveDelimiter = $properties['preserveDelimiter'];
51
52 12
        return $component;
53
    }
54
55
    /**
56
     * Returns the component literal value
57
     *
58
     * @return string|null
59
     */
60 760
    public function getContent()
61
    {
62 760
        if (null === $this->data && false === $this->preserveDelimiter) {
63 456
            return null;
64
        }
65
66 391
        return $this->encodeQueryFragment($this->data);
67
    }
68
69
    /**
70
     * Return the decoded string representation of the component
71
     *
72
     * @return string
73
     */
74 30
    public function getValue()
75
    {
76 30
        return (string) $this->data;
77
    }
78
79
    /**
80
     * Returns the instance string representation
81
     * with its optional URI delimiters
82
     *
83
     * @return string
84
     */
85 725
    public function getUriComponent()
86
    {
87 725
        $component = $this->__toString();
88 725
        if ($this->preserveDelimiter) {
89 383
            return FragmentInterface::DELIMITER.$component;
90
        }
91
92 429
        return $component;
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 9
    public function modify($value)
106
    {
107 9
        if (null === $value && $value === $this->getContent()) {
108 3
            return $this;
109
        }
110
111 6
        if ($value === $this->__toString()) {
112 3
            return $this;
113
        }
114
115 3
        return new static($value);
116
    }
117
}
118