39 lines
1.3 KiB
PHP
39 lines
1.3 KiB
PHP
|
<?php
|
||
|
namespace nur\sery;
|
||
|
|
||
|
use Traversable;
|
||
|
|
||
|
/**
|
||
|
* Class A: gestion de tableaux ou d'instances de {@link IArrayWrapper}
|
||
|
*
|
||
|
* contrairement à {@link cl}, les méthodes de cette classes sont plutôt conçues
|
||
|
* pour modifier le tableau en place
|
||
|
*/
|
||
|
class A {
|
||
|
/**
|
||
|
* s'assurer que $array est un array non null. retourner true si $array n'a
|
||
|
* pas été modifié (s'il était déjà un array), false sinon.
|
||
|
*/
|
||
|
static final function ensure_array(&$array): bool {
|
||
|
if (is_array($array)) return true;
|
||
|
if ($array instanceof IArrayWrapper) $array = $array->wrappedArray();
|
||
|
if ($array === null || $array === false) $array = [];
|
||
|
elseif ($array instanceof Traversable) $array = iterator_to_array($array);
|
||
|
else $array = [$array];
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* s'assurer que $array est un array s'il est non null. retourner true si
|
||
|
* $array n'a pas été modifié (s'il était déjà un array ou s'il valait null).
|
||
|
*/
|
||
|
static final function ensure_narray(&$array): bool {
|
||
|
if ($array === null || is_array($array)) return true;
|
||
|
if ($array instanceof IArrayWrapper) $array = $array->wrappedArray();
|
||
|
if ($array === false) $array = [];
|
||
|
elseif ($array instanceof Traversable) $array = iterator_to_array($array);
|
||
|
else $array = [$array];
|
||
|
return false;
|
||
|
}
|
||
|
}
|