File: shift.php

Recommend this page to a friend!
  Classes of Nathaniel Sherwood  >  Encase PHP Functional Programming  >  shift.php  >  Download  
File: shift.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: 1 year ago
Size: 950 bytes
 

Contents

Download
<?php
namespace Encase\Functional;

/**
 * Remove the first 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 first character.
 *
 * @param array|string|\ArrayAccess|\stdClass $arrayish Array-like container.
 * @return mixed The first element of the container.
 */
function shift(&$arrayish)
{
   
$type = assertType(
       
$arrayish,
        [
'array', '\ArrayAccess', 'string', 'stdClass'],
       
'type'
   
);

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

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

    foreach (
$arrayish as $key => $value) {
        break;
    }

    if (
$type === '\ArrayAccess') {
        unset(
$arrayish[$key]);
    } else {
        unset(
$arrayish->$key);
    }
    return
$value;
}


For more information send a message to info at phpclasses dot org.