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