File: hierarchical-path.md

package info (click to toggle)
php-league-uri-src 7.5.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,712 kB
  • sloc: php: 16,698; javascript: 127; makefile: 43; xml: 36
file content (302 lines) | stat: -rw-r--r-- 10,498 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
---
layout: default
title: The Hierarchical Path component
---

# The Hierarchical Path component

The library provides a `HierarchicalPath` class to ease HTTP like path creation and manipulation. This URI component object exposes :

- the [package common API](/components/2.0/api/)
- the [path common API](/components/2.0/path)

It also provides specific methods to work with segments-type URI path components.

<p class="message-notice">If the modifications do not change the current object, it is returned as is, otherwise, a new modified object is returned.</p>

<p class="message-warning">When a modification fails a <code>League\Uri\Contracts\UriException</code> exception is thrown.</p>

## Instantiation

### Using the default constructor

~~~php
<?php
public HierarchicalPath::__construct(?string $content = null): void
~~~

<p class="message-notice">submitted string is normalized to be <code>RFC3986</code> compliant.</p>

<p class="message-warning">If the submitted value is not valid a <code>League\Uri\Components\Exception</code> exception is thrown.</p>

### Using a string

A string or an PHP object exposing a `__toString` method can be used to instantiate a new object with the following named constructor.

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path = HierarchicalPath::createFromString('path/to/the/sky');
$path->getContent(); //returns 'path/to/the/sky'
~~~

### Using a collection of path segments.

A path is a collection of segment delimited by the path delimiter `/`. So it is possible to create a `HierarchicalPath` object using a collection of segments with the `HierarchicalPath::createFromSegments` method.

The method expects at most 2 arguments:

- The first required argument must be a collection of segments (an `array` or a `Traversable` object)
- The second optional argument, a `HierarchicalPath` constant, tells whether this is a rootless path or not:
    - `HierarchicalPath::IS_ABSOLUTE`: the created object will represent an absolute path;
    - `HierarchicalPath::IS_RELATIVE`: the created object will represent a rootless path;

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$relative_path =  HierarchicalPath::createFromSegments(['shop', 'example', 'com']);
echo $relative_path; //display 'shop/example/com'

$absolute_path = HierarchicalPath::createFromSegments(['shop', 'example', 'com'], Path::IS_ABSOLUTE);
echo $absolute_path; //display '/shop/example/com'

$end_slash = HierarchicalPath::createFromSegments(['shop', 'example', 'com', ''], Path::IS_ABSOLUTE);
echo $end_slash; //display '/shop/example/com/'
~~~

<p class="message-info">To force the end slash when using the <code>Path::createFromSegments</code> method you need to add an empty string as the last member of the submitted array.</p>

## Manipulating the path as a filesystem path

The `HierarchicalPath` allows you to access and manipulate the path as if it was a filesystem path.

### Accessing the path

~~~php
<?php

public HierarchicalPath::getDirname(void): string
public HierarchicalPath::getBasename(void): string
public HierarchicalPath::getExtension(void): string
~~~

#### Usage

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path = HierarchicalPath::createFromString('/path/to/the/sky.txt');
$path->getExtension(); //return 'txt'
$path->getBasename();  //return 'sky.txt'
$path->getDirname();   //return '/path/to/the'
~~~

### Modifying the path

~~~php
<?php

public HierarchicalPath::withDirname(string $dirname): self
public HierarchicalPath::withBasename(string $basename): self
public HierarchicalPath::withExtension(string $extension): self
~~~

<p class="message-warning"><code>withExtension</code> will throw an <code>League\Uri\Components\Exception</code> exception if the extension contains the path delimiter.</p>

#### Usage

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path = HierarchicalPath::createFromString('/path/to/the/sky.txt;foo=bar');
$new_path = $path
    ->withDirname('/foo')
    ->withExtension('csv');
echo $new_path; // display /foo/sky.csv;foo=bar

$alt_path = $path
    ->withBasename('paradise.html');
echo $alt_path; // display /path/to/the/paradise.html
~~~

## The path as a collection of segments

~~~php
<?php
const HierarchicalPath::IS_RELATIVE = 0;
const HierarchicalPath::IS_ABSOLUTE = 1;
public HierarchicalPath::isAbsolute(void): bool
public HierarchicalPath::segments(void): array
public HierarchicalPath::get(int $offset, $default = null): mixed
public HierarchicalPath::keys([string $segment]): array
public HierarchicalPath::count(void): int
public HierarchicalPath::getIterator(void): ArrayIterator
public HierarchicalPath::prepend(string $path): self
public HierarchicalPath::append(string $path): self
public HierarchicalPath::withSegment(int $offset, string $path): self
public HierarchicalPath::withoutSegment(int $offsets, int ...$offsets): self
public HierarchicalPath::withoutEmptySegment(): self
~~~

### Accessing the path segments

A path can be represented as an array of its internal segments. Through the use of the `HierarchicalPath::getSegments` method the class returns the object array representations.

<p class="message-info">A path ending with a slash will have an empty string as the last member of its array representation.</p>

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path = new HierarchicalPath('/path/to/the/sky');
$path->segments(); //return ['path', 'to', 'the', 'sky'];

$absolute_path = new HierarchicalPath('/path/to/the/sky/');
$absolute_path->segments(); //return ['path', 'to', 'the', 'sky', ''];

$relative_path = new HierarchicalPath('path/to/the/sky/');
$relative_path->segments(); //return ['path', 'to', 'the', 'sky', ''];
~~~

The class implements PHP's `Countable` and `IteratorAggregate` interfaces. This means that you can count the number of segments and use the `foreach` construct to iterate overs them.

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path = HierarchicalPath::createFromString('/path/to/the/sky');
count($path); //return 4
foreach ($path as $offset => $segment) {
    //do something meaningful here
}
~~~

### Accessing the segments offset

If you are interested in getting all the segments offsets you can do so using the `HierarchicalPath::keys` method like shown below:

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path = HierarchicalPath::createFromString('/path/to/the/sky');
$path->keys();        //return [0, 1, 2, 3];
$path->keys('sky');   //return [3];
$path->keys('gweta'); //return [];
~~~

The method returns all the segment keys, but if you supply an argument, only the keys whose segment value equals the argument are returned.

<p class="message-info">The supplied argument is decoded to enable matching the corresponding keys.</p>

### Accessing the segments content

If you are only interested in a given segment you can access it directly using the `HierarchicalPath::getSegment` method as show below:

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path = HierarchicalPath::createFromString('/path/to/the/sky');
$path->get(0);         //return 'path'
$path->get(23);        //return null
$path->get(23, 'now'); //return 'now'
~~~

<p class="message-notice"><code>HierarchicalPath::getSegment</code> always returns the decoded representation.</p>

If the offset does not exists it will return the value specified by the optional second argument or `null`.

<p class="message-info"><code>HierarchicalPath::getSegment</code> supports negative offsets</code></p>

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path = HierarchicalPath::createFromString('/path/to/the/sky');
$path->get(-1);         //return 'sky'
$path->get(-23);        //return null
$path->get(-23, 'now'); //return 'now'
~~~

## Manipulating the path segments

### Append segments

To append segments to the current object you need to use the `HierarchicalPath::append` method. This method accept a single argument which represents the data to be appended. This data can be a string, an object which implements the `__toString` method or another `HierarchicalPath` object:

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path    = HierarchicalPath::createFromString();
$newPath = $path->append('path')->append('to/the/sky');
$newPath->__toString(); //return path/to/the/sky
~~~

### Prepend segments

To prepend segments to the current path you need to use the `HierarchicalPath::prepend` method. This method accept a single argument which represents the data to be prepended. This data can be a string, an object which implements the `__toString` method or another `HierarchicalPath` object:

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path    = HierarchicalPath::createFromString();
$newPath = $path->prepend('sky')->prepend('path/to/the');
$newPath->__toString(); //return path/to/the/sky
~~~

### Replace segments

To replace a segment you must use the `HierarchicalPath::withSegment` method with the following arguments:

- `$offset` which represents the segment offset to remove if it exists.
- `$data` which represents the data to be inject.  This data can be a string, an object which implements the `__toString` method or another `HierarchicalPath` object.

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path    = HierarchicalPath::createFromString('/foo/example/com');
$newPath = $path->withSegment(0, 'bar/baz');
$newPath->__toString(); //return /bar/baz/example/com
~~~

<p class="message-info">Just like the <code>HierarchicalPath::getSegment</code> this method supports negative offset.</p>

<p class="message-notice">if the specified offset does not exists, no modification is performed and the current object is returned.</p>

### Remove segments

To remove segments from the current object and returns a new `HierarchicalPath` object without them you must use the `HierarchicalPath::withoutSegments` method. This method expects a single argument. This argument is an array containing a list of parameter names to remove.

~~~php
<?php

use League\Uri\Components\HierarchicalPath;

$path = new HierarchicalPath('/path/to/the/sky');
$newPath = $path->withoutSegment(0, 1);
$newPath->__toString(); //return '/the/sky'
~~~

<p class="message-info">Just like the <code>HierarchicalPath::getSegment</code> this method supports negative offset.</p>

<p class="message-notice">if the specified offset does not exists, no modification is performed and the current object is returned.</p>