Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsModifier
7
 */
8
/**
9
 * Smarty count modifier plugin
10
 * Type:     modifier
11
 * Name:     count
12
 * Purpose:  counts all elements in an array or in a Countable object
13
 * Input:
14
 *          - Countable|array: array or object to count
15
 *          - mode: int defaults to 0 for normal count mode, if set to 1 counts recursive
16
  *
17
 * @param mixed $arrayOrObject  input array/object
18
 * @param int $mode       count mode
19
 *
20
 * @return int
21
 */
22
function smarty_modifier_count($arrayOrObject, $mode = 0)
23
{
24
    /*
25
     * @see https://www.php.net/count
26
     * > Prior to PHP 8.0.0, if the parameter was neither an array nor an object that implements the Countable interface,
27
     * > 1 would be returned, unless value was null, in which case 0 would be returned.
28
     */
29
 
30
    if ($arrayOrObject instanceof Countable || is_array($arrayOrObject)) {
31
        return count($arrayOrObject, (int) $mode);
32
    } elseif ($arrayOrObject === null) {
33
        return 0;
34
    }
35
    return 1;
36
}