PHP Classes

File: size.php

Recommend this page to a friend!
  Classes of Nathaniel Sherwood   Encase PHP Functional Programming   size.php   Download  
File: size.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: 5 years ago
Size: 774 bytes
 

Contents

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

/**
 * Get the size of `$value`.
 *
 * For iterables, this is the number of elements.
 * For strings, this is the number of characters.
 *
 * @param iterable|string $value
 * @return int Size of `$value` - 0 if `$value` is not an interable or string.
 *
 * @alias count
 */
function size($value)
{
    if (
isType($value, ['array', 'Countable'])) {
        return \
count($value);
    }

    if (\
is_string($value)) {
        return \
function_exists('mb_strlen') ?
            \
mb_strlen($value) :
            \
count(\preg_split('//u', $value, null, PREG_SPLIT_NO_EMPTY));
    }

   
$size = 0;

    if (
isType($value, ['iterable', 'stdClass'])) {
        foreach (
$value as $v) {
            ++
$size;
        }
        return
$size;
    }

    return
$size;
}

function
count()
{
    return
size(...\func_get_args());
}