Skip to content

Commit

Permalink
Simplify flattening of "ip_whitelist" config option
Browse files Browse the repository at this point in the history
  • Loading branch information
danielburger1337 authored and scheb committed Jan 26, 2024
1 parent 22275f8 commit 94ded3b
Showing 1 changed file with 5 additions and 28 deletions.
33 changes: 5 additions & 28 deletions src/bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Scheb\TwoFactorBundle\DependencyInjection;

use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use Scheb\TwoFactorBundle\Model\BackupCodeInterface;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface as EMailTwoFactorInterface;
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface as GoogleTwoFactorInterface;
Expand All @@ -15,7 +17,7 @@
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
use function interface_exists;
use function is_iterable;
use function iterator_to_array;

/**
* @final
Expand Down Expand Up @@ -45,13 +47,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayNode('ip_whitelist')
->beforeNormalization()
->ifArray()
->then(static function (mixed $value): array {
$values = [];
foreach (self::flatten($value) as $v) {
$values[] = $v;
}

return $values;
->then(static function (array $value): array {
return iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($value)), false);
})
->end()
->defaultValue([])
Expand Down Expand Up @@ -224,24 +221,4 @@ private function addGoogleAuthenticatorConfiguration(ArrayNodeDefinition $rootNo
->end()
->end();
}

/**
* @param iterable<mixed> $iterableValue
*
* @return iterable<mixed>
*/
private static function flatten(iterable $iterableValue): iterable
{
foreach ($iterableValue as $value) {
if (is_iterable($value)) {
foreach (self::flatten($value) as $x) {
yield $x;
}

continue;
}

yield $value;
}
}
}

0 comments on commit 94ded3b

Please sign in to comment.