nur-sery/src/A.php

163 lines
4.7 KiB
PHP
Raw Normal View History

2024-05-19 11:19:00 +04:00
<?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 = [];
2024-05-30 07:00:59 +04:00
elseif ($array instanceof Traversable) $array = cl::all($array);
2024-05-19 11:19:00 +04:00
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 instanceof IArrayWrapper) $array = $array->wrappedArray();
2024-05-30 07:00:59 +04:00
if ($array === null || is_array($array)) return true;
2024-05-19 11:19:00 +04:00
if ($array === false) $array = [];
2024-05-30 07:00:59 +04:00
elseif ($array instanceof Traversable) $array = cl::all($array);
2024-05-19 11:19:00 +04:00
else $array = [$array];
return false;
}
2024-06-06 16:07:58 +04:00
2024-06-15 15:23:42 +04:00
static function merge(&$dest, ...$merges): void {
self::ensure_narray($dest);
2024-06-06 16:07:58 +04:00
$dest = cl::merge($dest, ...$merges);
}
2024-06-06 17:32:07 +04:00
2024-06-15 15:23:42 +04:00
static final function select(&$dest, ?array $mappings, bool $inverse=false): void {
self::ensure_narray($dest);
2024-06-06 17:32:07 +04:00
$dest = cl::select($dest, $mappings, $inverse);
}
2024-06-15 15:23:42 +04:00
static final function selectm(&$dest, ?array $mappings, ?array $merge=null): void {
self::ensure_narray($dest);
2024-06-06 17:32:07 +04:00
$dest = cl::selectm($dest, $mappings, $merge);
}
2024-06-15 15:23:42 +04:00
static final function mselect(&$dest, ?array $merge, ?array $mappings): void {
self::ensure_narray($dest);
2024-06-06 17:32:07 +04:00
$dest = cl::mselect($dest, $merge, $mappings);
}
2024-06-15 15:23:42 +04:00
static final function pselect(&$dest, ?array $pkeys): void {
self::ensure_narray($dest);
2024-06-06 17:32:07 +04:00
$dest = cl::pselect($dest, $pkeys);
}
2024-06-15 15:23:42 +04:00
static final function pselectm(&$dest, ?array $pkeys, ?array $merge=null): void {
self::ensure_narray($dest);
2024-06-06 17:32:07 +04:00
$dest = cl::pselectm($dest, $pkeys, $merge);
}
2024-06-15 15:23:42 +04:00
static final function mpselect(&$dest, ?array $merge, ?array $pkeys): void {
self::ensure_narray($dest);
2024-06-06 17:32:07 +04:00
$dest = cl::mpselect($dest, $merge, $pkeys);
}
2024-06-13 12:24:15 +04:00
2024-06-15 15:23:42 +04:00
static final function set_nn(&$dest, $key, $value) {
self::ensure_narray($dest);
2024-06-13 12:24:15 +04:00
if ($value !== null) {
if ($key === null) $dest[] = $value;
else $dest[$key] = $value;
}
return $value;
}
2024-06-15 15:23:42 +04:00
static final function append_nn(&$dest, $value) {
return self::set_nn($dest, null, $value);
2024-06-14 19:57:50 +04:00
}
2024-06-15 15:23:42 +04:00
static final function set_nz(&$dest, $key, $value) {
self::ensure_narray($dest);
2024-06-13 12:24:15 +04:00
if ($value !== null && $value !== false) {
if ($key === null) $dest[] = $value;
else $dest[$key] = $value;
}
return $value;
}
2024-06-15 15:23:42 +04:00
static final function append_nz(&$dest, $value) {
self::ensure_narray($dest);
return self::set_nz($dest, null, $value);
2024-06-14 19:57:50 +04:00
}
2024-06-15 15:23:42 +04:00
static final function prepend_nn(&$dest, $value) {
self::ensure_narray($dest);
2024-06-13 12:24:15 +04:00
if ($value !== null) {
if ($dest === null) $dest = [];
array_unshift($dest, $value);
}
return $value;
}
2024-06-15 15:23:42 +04:00
static final function prepend_nz(&$dest, $value) {
self::ensure_narray($dest);
2024-06-13 12:24:15 +04:00
if ($value !== null && $value !== false) {
if ($dest === null) $dest = [];
array_unshift($dest, $value);
}
return $value;
}
2024-06-14 19:57:50 +04:00
2024-06-15 15:23:42 +04:00
static final function replace_nx(&$dest, $key, $value) {
self::ensure_narray($dest);
2024-06-14 19:57:50 +04:00
if ($dest !== null && !array_key_exists($key, $dest)) {
return $dest[$key] = $value;
} else {
return $dest[$key] ?? null;
}
}
2024-06-15 15:23:42 +04:00
static final function replace_n(&$dest, $key, $value) {
self::ensure_narray($dest);
2024-06-14 19:57:50 +04:00
$pvalue = $dest[$key] ?? null;
if ($pvalue === null) $dest[$key] = $value;
return $pvalue;
}
2024-06-15 15:23:42 +04:00
static final function replace_z(&$dest, $key, $value) {
self::ensure_narray($dest);
2024-06-14 19:57:50 +04:00
$pvalue = $dest[$key] ?? null;
if ($pvalue === null || $pvalue === false) $dest[$key] = $value;
return $pvalue;
}
2024-06-15 15:23:42 +04:00
static final function pop(&$dest, $key, $default=null) {
2024-06-14 19:57:50 +04:00
if ($dest === null) return $default;
2024-06-15 15:23:42 +04:00
self::ensure_narray($dest);
2024-06-14 19:57:50 +04:00
if ($key === null) return array_pop($dest);
$value = $dest[$key] ?? $default;
unset($dest[$key]);
return $value;
}
2024-06-15 15:23:42 +04:00
static final function popx(&$dest, ?array $keys): array {
2024-06-14 19:57:50 +04:00
$values = [];
if ($dest === null) return $values;
2024-06-15 15:23:42 +04:00
self::ensure_narray($dest);
2024-06-14 19:57:50 +04:00
if ($keys === null) return $values;
foreach ($keys as $key) {
$values[$key] = self::pop($dest, $key);
}
return $values;
}
2024-05-19 11:19:00 +04:00
}