Add validation to directory key

This commit is contained in:
2023-05-25 14:13:03 +01:00
parent 7469cc78f8
commit e7c0465fdb

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Pcm\IconBundle\DependencyInjection; namespace Pcm\IconBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\Definition\ConfigurationInterface;
@@ -18,12 +19,50 @@ class Configuration implements ConfigurationInterface
->arrayNode('directories') ->arrayNode('directories')
->scalarPrototype()->end() ->scalarPrototype()->end()
->end() ->end()
->arrayNode('palletes') ->arrayNode('palletes') // Akshewally it is defined
->variablePrototype()->end() ->variablePrototype()->end()
->end() ->end()
->end() ->end()
; ;
$this->addValidationRules($treeBuilder->getRootNode());
return $treeBuilder; return $treeBuilder;
} }
private function addValidationRules(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
->arrayNode('directories')
->validate()
->ifEmpty()
->thenInvalid("Directories cannot be empty!")
->end()
->scalarPrototype()
->validate()
->ifTrue(fn($path) => !is_dir($path))
->thenInvalid('%s is not a directory!')
->end()
->end()
->end()
->end()
;
// $rootNode
// ->children()
// ->arrayNode('directories')
// ->scalarPrototype()
// ->validate()
// ->ifTrue(function ($value) {
// // Add validation logic for directories here
// return false;
// })
// ->thenInvalid('Invalid directory')
// ->end()
// ->end()
// ->end()
// ->end()
// ;
}
} }