Skip to content

Commit

Permalink
[TASK] Introduce ArrayHelper::trimExplode() method
Browse files Browse the repository at this point in the history
  • Loading branch information
eliashaeussler authored and renovate[bot] committed Apr 7, 2024
1 parent 6e3cf94 commit 308088e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
11 changes: 2 additions & 9 deletions src/Config/Adapter/EnvironmentVariablesConfigAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
use EliasHaeussler\CacheWarmup\Config;
use EliasHaeussler\CacheWarmup\Crawler;
use EliasHaeussler\CacheWarmup\Exception;
use EliasHaeussler\CacheWarmup\Helper;

use function array_filter;
use function array_map;
use function explode;
use function getenv;
use function gettype;
use function in_array;
Expand Down Expand Up @@ -121,12 +119,7 @@ private function processValue(string $envVarValue, mixed $defaultValue, string $
return match (gettype($defaultValue)) {
'array' => match ($name) {
'crawlerOptions' => $this->crawlerFactory->parseCrawlerOptions($envVarValue),
default => array_filter(
array_map(
'trim',
explode(',', $envVarValue),
),
),
default => Helper\ArrayHelper::trimExplode($envVarValue),
},
'boolean' => in_array(trim($envVarValue), self::BOOLEAN_VALUES, true),
default => $envVarValue,
Expand Down
24 changes: 22 additions & 2 deletions src/Helper/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

use function array_filter;
use function array_key_exists;
use function array_map;
use function array_values;
use function explode;
use function is_array;
use function is_int;
Expand All @@ -43,7 +45,7 @@ final class ArrayHelper
*/
public static function getValueByPath(iterable $subject, string $path, string $delimiter = '/'): mixed
{
$pathSegments = array_filter(explode($delimiter, $path));
$pathSegments = self::trimExplode($path, $delimiter);
$reference = &$subject;

foreach ($pathSegments as $pathSegment) {
Expand All @@ -63,7 +65,7 @@ public static function getValueByPath(iterable $subject, string $path, string $d
*/
public static function setValueByPath(iterable &$subject, string $path, mixed $value, string $delimiter = '/'): void
{
$pathSegments = array_filter(explode($delimiter, $path));
$pathSegments = self::trimExplode($path, $delimiter);
$reference = &$subject;

foreach ($pathSegments as $pathSegment) {
Expand Down Expand Up @@ -111,6 +113,24 @@ public static function mergeRecursive(array &$subject, array $other): void
}
}

/**
* @param non-empty-string $delimiter
*
* @return list<non-empty-string>
*/
public static function trimExplode(string $subject, string $delimiter = ','): array
{
return array_values(
array_filter(
array_map(
'trim',
explode($delimiter, $subject),
),
static fn (string $value) => '' !== $value,
),
);
}

private static function pathSegmentExists(mixed $subject, string $pathSegment): bool
{
return is_array($subject) && array_key_exists($pathSegment, $subject);
Expand Down

0 comments on commit 308088e

Please sign in to comment.