Failed Conditions
Pull Request — master (#67)
by lee
02:56
created

Fragment::doInitialize()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 0
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Purl;
6
7
use function array_merge;
8
use function parse_url;
9
use function sprintf;
10
11
/**
12
 * Fragment represents the part of a Url after the hashmark (#).
13
 *
14
 * @property Path|string $path
15
 * @property Query|string $query
16
 */
17
class Fragment extends AbstractPart
18
{
19
    /** @var string|null The original fragment string. */
20
    private $fragment;
21
22
    /** @var mixed[] */
23
    protected $data = [
24
        'path'  => null,
25
        'query' => null,
26
    ];
27
28
    /** @var string[] */
29
    protected $partClassMap = [
30
        'path' => 'Purl\Path',
31
        'query' => 'Purl\Query',
32
    ];
33
34
    /**
35
     * @param string|Path|null $fragment
36
     */
37 70
    public function __construct($fragment = null, ?Query $query = null)
38
    {
39 70
        if ($fragment instanceof Path) {
40 3
            $this->initialized  = true;
41 3
            $this->data['path'] = $fragment;
42
        } else {
43 69
            $this->fragment = $fragment;
44
        }
45
46 70
        $this->data['query'] = $query;
47 70
    }
48
49
    /**
50
     * @param mixed $value
51
     */
52 2
    public function set(string $key, $value) : AbstractPart
53
    {
54 2
        $this->initialize();
55 2
        $this->data[$key] = $this->preparePartValue($key, $value);
56
57 2
        return $this;
58
    }
59
60 24
    public function getFragment() : string
61
    {
62 24
        $this->initialize();
63
64 24
        return sprintf('%s%s', $this->path, (string) $this->query !== '' ? '?' . (string) $this->query : '');
65
    }
66
67 1
    public function setFragment(string $fragment) : AbstractPart
68
    {
69 1
        $this->initialized = false;
70 1
        $this->data        = [];
71 1
        $this->fragment    = $fragment;
72
73 1
        return $this;
74
    }
75
76 1
    public function setPath(Path $path) : AbstractPart
77
    {
78 1
        $this->data['path'] = $path;
79
80 1
        return $this;
81
    }
82
83 1
    public function getPath() : Path
84
    {
85 1
        $this->initialize();
86
87 1
        return $this->data['path'];
88
    }
89
90 1
    public function setQuery(Query $query) : AbstractPart
91
    {
92 1
        $this->data['query'] = $query;
93
94 1
        return $this;
95
    }
96
97 1
    public function getQuery() : Query
98
    {
99 1
        $this->initialize();
100
101 1
        return $this->data['query'];
102
    }
103
104 22
    public function __toString() : string
105
    {
106 22
        return $this->getFragment();
107
    }
108
109 29
    protected function doInitialize() : void
110
    {
111 29
        if ($this->fragment !== null) {
112 14
            $data = parse_url($this->fragment);
113 14
            if ($data === false) {
114
                $data = ['path' => $this->fragment];
115
            }
116
117 14
            $this->data = array_merge($this->data, $data);
118
        }
119
120 29
        foreach ($this->data as $key => $value) {
121 29
            $this->data[$key] = $this->preparePartValue($key, $value);
122
        }
123 29
    }
124
}
125