modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-05-30 07:00:59 +04:00
parent cb6ae7d11e
commit 18562c4152
3 changed files with 78 additions and 64 deletions

View File

@ -16,12 +16,15 @@ class ControlSelect extends ControlVisual {
"items" => ["?iterable", null, "liste d'éléments à afficher"],
"items_func" => ["?callable", null, "fonction fournissant une liste d'éléments à afficher"],
"item_value_key" => ["?key", null, "clé de la valeur d'un élément"],
"item_value_pkey" => ["?key", null, "chemin de clé de la valeur d'un élément"],
"item_value_func" => ["?callable", null, "fonction fournissant la valeur d'un élément"],
"item_text_key" => ["?key", null, "clé du libellé d'un élément"],
"item_text_pkey" => ["?key", null, "chemin de clé du libellé d'un élément"],
"item_text_func" => ["?callable", null, "fonction fournissant le libellé d'un élément"],
"no_item_value" => ["?string", null, "valeur si aucun élément n'est sélectionné"],
"no_item_text" => ["?content", null, "libellé si aucun élément n'est sélectionné"],
"group_key" => ["?key", null, "clé d'une valeur sur laquelle grouper les éléments"],
"group_pkey" => ["?key", null, "chemin de clé d'une valeur sur laquelle grouper les éléments"],
"group_func" => ["?callable", null, "fonction fournissant une valeur sur laquelle grouper les éléments"],
"required" => ["?bool", null, "ce champ est-il requis?"],
"show_required" => ["?bool", null, "faut-il afficher la marque de champ requis?"],
@ -46,12 +49,18 @@ class ControlSelect extends ControlVisual {
/** @var string|int|null */
protected $ppItemValueKey;
/** @var string|int|null */
protected $ppItemValuePkey;
/** @var ?callable */
protected $ppItemValueFunc;
/** @var string|int|null */
protected $ppItemTextKey;
/** @var string|int|null */
protected $ppItemTextPkey;
/** @var ?callable */
protected $ppItemTextFunc;
@ -64,6 +73,9 @@ class ControlSelect extends ControlVisual {
/** @var string|int|null */
protected $ppGroupKey;
/** @var string|int|null */
protected $ppGroupPkey;
/** @var ?callable */
protected $ppGroupFunc;
@ -81,7 +93,7 @@ class ControlSelect extends ControlVisual {
protected function vof(
$value, ?callable $func,
$item=null, $itemKey=null, ?int $itemIndex=null,
$item=null, $itemKey=null, $itemPkey = null, ?int $itemIndex=null,
...$funcArgs) {
if ($value !== null) return $value;
if ($func !== null) {
@ -95,16 +107,16 @@ class ControlSelect extends ControlVisual {
$value = A::get($item, $itemKey);
if ($value !== null) return $value;
}
if ($itemPkey !== null) {
$value = A::pget($item, $itemPkey);
if ($value !== null) return $value;
}
if ($itemIndex !== null) {
$index = 0;
foreach ($item as $value) {
if ($index === $itemIndex) return $value;
$index++;
}
return null;
$itemKeys = array_keys($item);
return $item[$itemKeys[$itemIndex]] ?? null;
}
}
return $item;
return null;
}
function getControl(): array {
@ -124,11 +136,11 @@ class ControlSelect extends ControlVisual {
}
$haveGroups = $this->ppGroupKey !== null || $this->ppGroupFunc !== null;
foreach ($items as $key => $item) {
$itemValue = $this->vof(null, $this->ppItemValueFunc, $item, $this->ppItemValueKey, 0, $item, $key);
$itemText = $this->vof(null, $this->ppItemTextFunc, $item, $this->ppItemTextKey, 1, $item, $key);
$itemValue = $this->vof(null, $this->ppItemValueFunc, $item, $this->ppItemValueKey, $this->ppItemValuePkey, 0, $item, $key);
$itemText = $this->vof(null, $this->ppItemTextFunc, $item, $this->ppItemTextKey, $this->ppItemTextPkey, 1, $item, $key);
$itemGroup = null;
if ($haveGroups) {
$itemGroup = $this->vof(null, $this->ppGroupFunc, $item, $this->ppGroupKey, null, $item, $key);
$itemGroup = $this->vof(null, $this->ppGroupFunc, $item, $this->ppGroupKey, $this->ppGroupPkey, null, $item, $key);
}
if (!$itemText) $itemText = $itemValue;
$options[] = array(

View File

@ -18,7 +18,7 @@ class A {
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);
elseif ($array instanceof Traversable) $array = cl::all($array);
else $array = [$array];
return false;
}
@ -28,10 +28,10 @@ class A {
* $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 === null || is_array($array)) return true;
if ($array === false) $array = [];
elseif ($array instanceof Traversable) $array = iterator_to_array($array);
elseif ($array instanceof Traversable) $array = cl::all($array);
else $array = [$array];
return false;
}

View File

@ -12,19 +12,69 @@ use Traversable;
* pour retourner un nouveau tableau
*/
class cl {
/**
* retourner un array avec les éléments retournés par l'itérateur. les clés
* numériques sont réordonnées, les clés chaine sont laissées en l'état
*/
static final function all(?iterable $iterable): array {
if ($iterable === null) return [];
if (is_array($iterable)) return $iterable;
$array = [];
foreach ($iterable as $key => $value) {
if (is_int($key)) $array[] = $value;
else $array[$key] = $value;
}
return $array;
}
/**
* retourner la première valeur de $array ou $default si le tableau est null
* ou vide
*/
static final function first(?iterable $iterable, $default=null) {
if (is_array($iterable)) {
return $iterable[array_key_first($iterable)];
}
if (is_iterable($iterable)) {
foreach ($iterable as $value) {
return $value;
}
}
return $default;
}
/**
* retourner la dernière valeur de $array ou $default si le tableau est null
* ou vide
*/
static final function last(?iterable $iterable, $default=null) {
if (is_array($iterable)) {
return $iterable[array_key_last($iterable)];
}
$value = $default;
if (is_iterable($iterable)) {
foreach ($iterable as $value) {
# parcourir tout l'iterateur pour avoir le dernier élément
}
}
return $value;
}
/** retourner un array non null à partir de $array */
static final function with($array): array {
if ($array instanceof IArrayWrapper) $array = $array->wrappedArray();
if (is_array($array)) return $array;
elseif ($array === null || $array === false) return [];
elseif ($array instanceof Traversable) return iterator_to_array($array);
elseif ($array instanceof Traversable) return self::all($array);
else return [$array];
}
/** retourner un array à partir de $array, ou null */
static final function withn($array): ?array {
if ($array instanceof IArrayWrapper) $array = $array->wrappedArray();
if (is_array($array)) return $array;
elseif ($array === null || $array === false) return null;
elseif ($array instanceof Traversable) return iterator_to_array($array);
elseif ($array instanceof Traversable) return self::all($array);
else return [$array];
}
@ -146,54 +196,6 @@ class cl {
return $array !== null? array_keys($array): [];
}
/**
* retourner un array avec les éléments retournés par l'itérateur. les clés
* numériques sont réordonnées, les clés chaine sont laissées en l'état
*/
static final function all(?iterable $iterable): array {
if ($iterable === null) return [];
if (is_array($iterable)) return $iterable;
$array = [];
foreach ($iterable as $key => $value) {
if (is_int($key)) $array[] = $value;
else $array[$key] = $value;
}
return $array;
}
/**
* retourner la première valeur de $array ou $default si le tableau est null
* ou vide
*/
static final function first(?iterable $iterable, $default=null) {
if (is_array($iterable)) {
return $iterable[array_key_first($iterable)];
}
if (is_iterable($iterable)) {
foreach ($iterable as $value) {
return $value;
}
}
return $default;
}
/**
* retourner la dernière valeur de $array ou $default si le tableau est null
* ou vide
*/
static final function last(?iterable $iterable, $default=null) {
if (is_array($iterable)) {
return $iterable[array_key_last($iterable)];
}
$value = $default;
if (is_iterable($iterable)) {
foreach ($iterable as $value) {
# parcourir tout l'iterateur pour avoir le dernier élément
}
}
return $value;
}
#############################################################################
/**