PHP Classes

File: pop.php

Recommend this page to a friend!
  Classes of Nathaniel Sherwood   Encase PHP Functional Programming   pop.php   Download  
File: pop.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Encase PHP Functional Programming
Make functions work as if they are class functions
Author: By
Last change:
Date: 5 years ago
Size: 1,247 bytes
 

Contents

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

/**
 * Remove the last element from the container and return it.
 * This function takes the input by reference and changes its length.
 *
 * If used on a string, the string is treated as a sequence of unicode
 * characters in the default encoding and removes the last character.
 *
 * This function does not work with `\Generator`.
 *
 * @param array|string|\ArrayAccess|\stdClass $arrayish Array-like container.
 * @return mixed The last element of the container.
 */
function pop(&$arrayish)
{
   
$type = assertType(
       
$arrayish,
        [
'array', '\ArrayAccess', '\Traversable', 'string', 'stdClass'],
       
'type'
   
);

    if (
$type === 'array') {
        return \
array_pop($arrayish);
    }

    if (
$type === 'string') {
       
$result = \mb_substr($arrayish, -1);
       
$arrayish = \mb_substr($arrayish, 0, \mb_strlen($arrayish) - 1);
        return
$result;
    }

    if (
$type === '\ArrayAccess') {
        if (
$arrayish instanceof \IteratorAggregate) {
           
$iterator = $arrayish->getIterator();
           
$value = \end($iterator);
           
$key = \key($iterator);
        } else {
            foreach (
$arrayish as $key => $value) {
            }
        }

        unset(
$arrayish[$key]);
    } else {
       
$value = \end($arrayish);
       
$key = \key($arrayish);
        unset(
$arrayish->$key);
    }
    return
$value;
}