PHP code example of jstewmc / path

1. Go to this page and download the library: Download jstewmc/path library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jstewmc / path example snippets



$path = new Path('foo/bar/baz');

$path->getSegment('first');  // returns 'foo'
$path->getSegment(1);        // returns 'bar'

$path-appendSegment('qux');     // path becomes 'foo/bar/baz/qux'
$path->prependSegment('quux');  // path becomes 'quux/foo/bar/baz/qux'

echo $path;  // prints 'quux/foo/bar/baz/qux'

$path = new Path();

$path->appendSegment('foo');     // path becomes "foo"
$path->prependSegment('bar');    // path becomes "bar/foo"
$path->insertSegment(1, 'baz');  // path becomes "bar/baz/foo"
$path->setSegment(-1, 'qux');    // path becomes "bar/baz/qux"
$path->unsetSegment('last');     // path becomes "bar/baz"

echo $path;  // prints "bar/baz"

$path = new Path();

$path->appendSegment('foo')->prependSegment('bar')->insertSegment(1, 'baz');

echo $path;  // prints "bar/baz/foo"

$path = new Path('foo/bar/baz');

(string) $path;  // returns "foo/bar/baz"
echo $path;      // returns "foo/bar/baz"
$path .'';       // returns "foo/bar/baz"

$path = new Path('foo/bar/baz');

// get the index of the 'foo' segment
$path->getIndex('foo');  // returns 0
$path->getIndex('qux');  // returns false ('qux' does not exist)

// get the value of the 0-th (aka, 'first') segment
$path->getSegment(0);        // returns 'foo'
$path->getSegment('first');  // returns 'foo'
$path->getSegment(999);      // throws OutOfBoundsException

// does the path have a segment at the 1-st index?
$path->hasIndex(1);    // returns true
$path->hasIndex(999);  // returns false

// does the path have the given segments (at any index)?
$path->hasSegment('bar');  // returns true
$path->hassegment('qux');  // returns false

// does the path have the given segments (at the given indices)?
$path->hasSegment('foo', 0);        // returns true
$path->hasSegment('foo', 'first');  // returns true
$path->hasSegment('foo', 1);        // returns false ('foo' is 0-th)
$path->hasSegment('qux', 'last');   // returns false ('qux' does not exist)

$path = new Path('foo/bar/baz');

$path->slice(1);

echo $path;  // prints "bar/baz"

$path->reverse();

echo $path;  // prints "baz/bar"

$a = new Path('foo/bar/baz');

$b = $a->getSlice(1); 

echo $a;  // prints 'foo/bar/baz'
echo $b;  // prints 'bar/baz'

$c = $a->getReverse();

echo $a;  // prints 'foo/bar/baz'
echo $c;  // prints 'baz/bar/foo'