PHP code example of karelwintersky / arris.entity.path

1. Go to this page and download the library: Download karelwintersky/arris.entity.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/ */

    

karelwintersky / arris.entity.path example snippets


use Arris\Entity\Path;

// Создать путь из строки
$path = new Path('/var/www/html');
echo $path; // /var/www/html

// Создать через фабричный метод
$path = Path::create('uploads/images');
echo $path->toString(); // uploads/images

// Соединить с подпутём
$full = Path::create('/var/www')->join('html/index.php');
echo $full; // /var/www/html/index.php

// URL-адрес тоже работает
$url = new Path('https://cdn.example.com/assets/app.js');
echo $url; // https://cdn.example.com/assets/app.js

new Path(string|array|Path $path, ?bool $isAbsolutePath = null, ?bool $hasTrailingSeparator = null)

// Строка
$p = new Path('/foo/bar/baz');

// Массив сегментов
$p = new Path(['foo', 'bar', 'baz']);

// Другой экземпляр Path
$copy = new Path($existingPath);

Path::create(string|array|Path $path, ?bool $isAbsolutePath = null, ?bool $hasTrailingSeparator = null): Path

$path = Path::create('/var/www')
    ->setTrailingSeparator(true);

$p = new Path('/var/www/html');
$p->toString();       // /var/www/html
$p->toString(true);   // /var/www/html/

echo new Path('/etc/nginx'); // /etc/nginx

$base = new Path('/var/www');
$full = $base->join('html');
echo $full; // /var/www/html

// Можно передать массив, строку или Path
$base->join(['assets', 'css']);  // /var/www/assets/css
$base->join(new Path('logs'));   // /var/www/logs

$dir  = new Path('/var/www/');
$file = $dir->joinName('index.php');
echo $file; // /var/www/index.php

$path = new Path('foo/bar');
$path->setAbsolutePath(true);
echo $path; // /foo/bar

$path = new Path('/var/www');
$path->setTrailingSeparator(true);
echo $path->toString(true); // /var/www/

$path->setOptions([
    'isAbsolute'           => true,
    'hasTrailingSeparator' => false,
]);

if ((new Path('/var/log'))->isPresent()) { /* ... */ }

if ((new Path('/etc/hosts'))->isFile()) { /* ... */ }

$created = (new Path('/tmp/app/cache'))->makePath(0755);

$url = new Path('https://example.com/api/v1');
echo $url; // https://example.com/api/v1

// Двойной слэш в пути (не в схеме) схлопывается
$url = new Path('https://cdn.example.com/a//b');
echo $url; // https://cdn.example.com/a/b