PHP Classes
elePHPant
Icontem

File: concat.php

Recommend this page to a friend!
  Classes of Nathaniel Sherwood  >  Encase PHP Functional Programming  >  concat.php  >  Download  
File: concat.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Encase PHP Functional Programming
Make functions work as if they are class functions
Author: By
Last change:
Date: 8 months ago
Size: 869 bytes
 

 

Contents

Class file image Download
<?php
namespace Encase\Functional;

/**
 * Concatenate one or more values to an iterable or string.
 *
 * @param iterable|string $container The container to append $values to.
 * @param mixed ...$values The values to append to $container.
 * @return iterable|string Copy of $container with $values appended.
 */
function concat($container, ...$values)
{
   
$type = assertType($container, ['string', 'iterable'], 'value');

    if (
$type === 'string') {
        foreach (
$values as $v) {
           
$container .= $v;
        }
        return
$container;
    }

   
// Well, generators *are* iterables... I guess we can support them? (but SHOULD we?)
   
if ($container instanceof \Generator) {
       
$container = \iterator_to_array($container, true);
    } elseif (\
is_object($container)) {
       
$container = clone $container;
    }

    foreach (
$values as $v) {
       
$container[] = $v;
    }

    return
$container;
}