gestione gare pubbliche
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

198 righe
5.2 KiB

<?php
class DotPath
{
/**
* Remove one or many array items from a given array using "dot" notation.
*
* @param array $array
* @param array|string $keys
* @return void
*/
public static function forget(&$array, $keys)
{
$original = &$array;
$keys = (array) $keys;
if (count($keys) === 0) {
return;
}
foreach ($keys as $key) {
// if the exact key exists in the top-level, remove it
if (array_key_exists($key, $array)) {
unset($array[$key]);
continue;
}
$parts = explode('.', $key);
// clean up before each pass
$array = &$original;
while (count($parts) > 1) {
$part = array_shift($parts);
if (isset($array[$part]) && is_array($array[$part])) {
$array = &$array[$part];
} else {
continue 2;
}
}
unset($array[array_shift($parts)]);
}
}
/**
* Get an item from an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string|int $key
* @param mixed $default
* @param mixed $dictionary
* @return mixed
*/
public static function get($array, $key, $default = null, $dictionary = false)
{
if (!is_array($array)) {
return value($default);
}
if (is_null($key)) {
return $array;
}
if (array_key_exists($key, $array)) {
return $array[$key];
}
if (strpos($key, '.') === false) {
return $array[$key] ?? value($default);
}
$segments = explode(".", $key);
foreach ($segments as $segment) {
if ($segment == "*") {
if (array_is_list($array)) {
$j = substr($key, 0, strpos($key, ".*."));
$k = substr($key, strpos($key, ".*.") + 3);
foreach ($array as $index => $element) {
$array[$index] = array_get($element, $k, null, $dictionary);
}
if ($dictionary) {
$result = [];
foreach ($array as $index => $values) {
if (strpos($k, ".*.") !== false) {
foreach ($values as $l => $val) {
$result["{$j}.{$index}.{$l}"] = $val;
}
} else {
$result["{$j}.{$index}.{$k}"] = $values;
}
}
return $result;
}
return $array;
} else {
$array = array_merge_recursive(...array_values($array));
}
} else {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return value($default);
}
}
}
if ($dictionary) {
return [$key => $array];
}
return $array;
}
/**
* Unset an array item to a given value using "dot" notation.
*
* If no key is given to the method, the entire array will be replaced.
*
* @param array $array
* @param string $key
* @return array
*/
public static function unset(&$array, $key)
{
if (is_null($key)) {
unset($array);
}
$keys = explode('.', $key);
while (count($keys) > 1) {
$key = array_shift($keys);
// If the key doesn't exist at this depth, we will just create an empty array
// to hold the next value, allowing us to create the arrays to hold final
// values at the correct depth. Then we'll keep digging into the array.
if (!isset($array[$key]) || !is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
unset($array[array_shift($keys)]);
return $array;
}
/**
* Set an array item to a given value using "dot" notation.
*
* If no key is given to the method, the entire array will be replaced.
*
* @param array $array
* @param string $key
* @param mixed $value
* @return array
*/
public static function set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
while (count($keys) > 1) {
$key = array_shift($keys);
// If the key doesn't exist at this depth, we will just create an empty array
// to hold the next value, allowing us to create the arrays to hold final
// values at the correct depth. Then we'll keep digging into the array.
if (!isset($array[$key]) || !is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$array[array_shift($keys)] = $value;
return $array;
}
}