Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow recursive schema validation in tree structure #40

Open
foxycode opened this issue Feb 17, 2021 · 0 comments
Open

Allow recursive schema validation in tree structure #40

foxycode opened this issue Feb 17, 2021 · 0 comments

Comments

@foxycode
Copy link

foxycode commented Feb 17, 2021

I'm trying to solve recursive schema validation in menu library when you have tree structure of menu (submenu items).

Here's what I did as temporary solution:

final class MenuExtension extends CompilerExtension
{

	public function getConfigSchema(): Schema
	{
		return Expect::arrayOf(Expect::structure([
			'loader' => Expect::string(DefaultMenuLoader::class),
			'items' => Expect::array()->required(),
		]));
	}

	public function getItemSchema(): Schema
	{
		return Expect::structure([
			'title' => Expect::string(),
			'link' => Expect::string(),
			'items' => Expect::array(),
		]);
	}

	public function loadConfiguration(): void
	{
		$config = $this->getConfig();
		$builder = $this->getContainerBuilder();
		$processor = new Processor;

		foreach ($config as $menuName => $menu) {
			$container->addSetup('addMenu', [
				$this->loadMenuConfiguration($builder, $processor, $menuName, $menu),
			]);
		}
	}

	private function loadMenuConfiguration(
		ContainerBuilder $builder,
		Processor $processor,
		string $menuName,
		stdClass $config
	): ServiceDefinition {
		$loader = $config->loader;

		if (!Strings::startsWith($config->loader, '@')) {
			$loader = $builder->addDefinition($this->prefix('menu.'. $menuName. '.loader'))
				->setType($config->loader)
				->setAutowired(false);
		}

		if ($loader->getType() === DefaultMenuLoader::class) {
			$loader->setArguments([$this->normalizeMenuItems($processor, $config->items)]);
		}
	}

	private function normalizeMenuItems(Processor $processor, array $items): array
	{
		array_walk($items, function(array &$item, string $key) use ($processor): void {
			$item = $processor->process($this->getItemSchema(), $item);

			if ($item->title === null) {
				$item->title = $key;
			}

			$item->items = $this->normalizeMenuItems($processor, $item->items);
		});

		return $items;
	}

}

Full file is here

  • It would be nice if I could use something like Expect::schemaFactory([$this, 'getItemSchema']) so function getItemSchema() could be called recursively as long as submenu's are present.
  • It would be nice if I could get Processor in CompilerExtension so I don't need to create one when I have custom config.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant