PHP Classes

File: src/path_merge.php

Recommend this page to a friend!
  Classes of Axel Pardemann   PHP Scalar Objects   src/path_merge.php   Download  
File: src/path_merge.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: PHP Scalar Objects
Implement objects that behave like scalar values
Author: By
Last change:
Date: 5 years ago
Size: 744 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

namespace
NorseBlue\ScalarObjects;

/**
 * Merge the path segments to the base path.
 *
 * @param string $base
 * @param string|array<string> $segments
 * @param string $separator
 * @param bool $trailing_separator
 *
 * @return string
 *
 * @codeCoverageIgnore
 */
function path_merge(
   
string $base,
   
$segments,
   
string $separator = DIRECTORY_SEPARATOR,
   
bool $trailing_separator = false
): string {
   
$path = rtrim($base, $separator);

    if (!
is_array($segments)) {
       
$segments = [$segments];
    }

    foreach (
$segments as $segment) {
       
$path .= $separator . $segment;
    }

   
$path = rtrim($path, $separator);

    return
$path . ($trailing_separator ? $separator : '');
}