Skip to content

Commit 2bdafef

Browse files
committed
CHORE: Improved array handling
1 parent d4abdeb commit 2bdafef

5 files changed

Lines changed: 136 additions & 3 deletions

File tree

HlebBootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function __construct(?string $publicPath = null, array $config = [], ?Log
109109

110110
// The current version of the framework.
111111
// Текущая версия фреймворка.
112-
\defined('HLEB_CORE_VERSION') or \define('HLEB_CORE_VERSION', '2.1.20');
112+
\defined('HLEB_CORE_VERSION') or \define('HLEB_CORE_VERSION', '2.1.21');
113113

114114
$this->logger = $logger;
115115

@@ -1010,7 +1010,7 @@ protected function switchInit(array &$config): void
10101010
$uri = \parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
10111011
\http_response_code(200);
10121012
\header('Content-Type: text/plain');
1013-
\header('Connection: close');
1013+
// \header('Connection: close');
10141014
if (\str_starts_with($uri, '/user/')) {
10151015
$output = \substr($uri, 6);
10161016
}

Init/Functions.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
namespace Hleb\Init;
6+
57
use Hleb\Constructor\Data\SystemSettings;
68
use Hleb\Static\Debug;
79
use Hleb\Static\Path;
@@ -21,7 +23,7 @@ public function create(): bool
2123
return false;
2224
}
2325
if ($files = SystemSettings::getValue('system', 'custom.function.files')) {
24-
foreach($files as $file) {
26+
foreach ($files as $file) {
2527
require Path::get('global') . DIRECTORY_SEPARATOR . \ltrim($file, '\\/');
2628
}
2729
}
@@ -288,6 +290,40 @@ function array_all(array $array, callable $callback): bool
288290
}
289291
}
290292

293+
if (!function_exists('array_first')) {
294+
/**
295+
* Returns the first value of the array without the pointer offset.
296+
* Similar to array_first() in PHP 8.5
297+
*
298+
* Возвращает первое значение массива без смещения указателя.
299+
* Аналогично array_first() в PHP 8.5
300+
*/
301+
function array_first(array $array): mixed
302+
{
303+
if (empty($array)) {
304+
return null;
305+
}
306+
return $array[\array_key_first($array)];
307+
}
308+
}
309+
310+
if (!function_exists('array_last')) {
311+
/**
312+
* Returns the last value of the array without the pointer offset.
313+
* Similar to array_last() in PHP 8.5
314+
*
315+
* Возвращает последнее значение массива без смещения указателя.
316+
* Аналогично array_last() в PHP 8.5
317+
*/
318+
function array_last()
319+
{
320+
if (empty($array)) {
321+
return null;
322+
}
323+
return $array[\array_key_last($array)];
324+
}
325+
}
326+
291327
if (!function_exists('array_any')) {
292328
/**
293329
* Checks whether at least one array element matches a condition.
@@ -310,4 +346,5 @@ function array_any(array $array, callable $callback): bool
310346

311347
return true;
312348
}
349+
313350
}

Reference/ArrInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,31 @@ public function set(array &$array, string|int|null $key, mixed $value): array;
190190
* ```
191191
*/
192192
public function expand(iterable $array): array;
193+
194+
/**
195+
* Returns a new array that checks the original for all elements with the keys listed in $keys.
196+
*
197+
* Возвращает новый массив, исключая из исходного все элементы с ключами, перечисленными в $keys.
198+
*/
199+
public function except(array $array, array|string $keys): array;
200+
201+
/**
202+
* Returns an array that contains only the matching values from $values.
203+
*
204+
* Возвращает массив, в котором оставлены только совпавшие значения из $values.
205+
*
206+
* @param bool $strict - strict comparison when searching for matches.
207+
* - строгое сравнение при поиске совпадений.
208+
*/
209+
public function onlyValues(array $array, array $values, bool $strict = false): array;
210+
211+
/**
212+
* Returns an array excluding all values from $values.
213+
*
214+
* Возвращает массив, в котором исключены все значения из $values.
215+
*
216+
* @param bool $strict - strict comparison when searching for matches.
217+
* - строгое сравнение при поиске совпадений.
218+
*/
219+
public function exceptValues(array $array, array $values, bool $strict = false): array;
193220
}

Reference/ArrReference.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,25 @@ public function expand(iterable $array): array
102102
{
103103
return ArrayHelper::expand($array);
104104
}
105+
106+
/** @inheritDoc */
107+
#[\Override]
108+
public function except(array $array, array|string $keys): array
109+
{
110+
return ArrayHelper::except($array, $keys);
111+
}
112+
113+
/** @inheritDoc */
114+
#[\Override]
115+
public function onlyValues(array $array, array $values, bool $strict = false): array
116+
{
117+
return ArrayHelper::onlyValues($array, $values, $strict);
118+
}
119+
120+
/** @inheritDoc */
121+
#[\Override]
122+
public function exceptValues(array $array, array $values, bool $strict = false): array
123+
{
124+
return ArrayHelper::exceptValues($array, $values, $strict);
125+
}
105126
}

Static/Arr.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,54 @@ public static function expand(iterable $array): array
316316
return BaseContainer::instance()->get(ArrInterface::class)->expand($array);
317317
}
318318

319+
/**
320+
* Returns a new array that checks the original for all elements with the keys listed in $keys.
321+
*
322+
* Возвращает новый массив, исключая из исходного все элементы с ключами, перечисленными в $keys.
323+
*/
324+
public static function except(array $array, array|string $keys): array
325+
{
326+
if (self::$replace) {
327+
return self::$replace->except($array, $keys);
328+
}
329+
330+
return BaseContainer::instance()->get(ArrInterface::class)->except($array, $keys);
331+
}
332+
333+
/**
334+
* Returns an array that contains only the matching values from $values.
335+
*
336+
* Возвращает массив, в котором оставлены только совпавшие значения из $values.
337+
*
338+
* @param bool $strict - strict comparison when searching for matches.
339+
* - строгое сравнение при поиске совпадений.
340+
*/
341+
public static function onlyValues(array $array, array $values, bool $strict = false): array
342+
{
343+
if (self::$replace) {
344+
return self::$replace->onlyValues($array, $values, $strict);
345+
}
346+
347+
return BaseContainer::instance()->get(ArrInterface::class)->onlyValues($array, $values, $strict);
348+
}
349+
350+
/**
351+
* Returns an array excluding all values from $values.
352+
*
353+
* Возвращает массив, в котором исключены все значения из $values.
354+
*
355+
* @param bool $strict - strict comparison when searching for matches.
356+
* - строгое сравнение при поиске совпадений.
357+
*/
358+
public static function exceptValues(array $array, array $values, bool $strict = false): array
359+
{
360+
if (self::$replace) {
361+
return self::$replace->exceptValues($array, $values, $strict);
362+
}
363+
364+
return BaseContainer::instance()->get(ArrInterface::class)->exceptValues($array, $values, $strict);
365+
}
366+
319367
/**
320368
* @internal
321369
*

0 commit comments

Comments
 (0)