1. Go to this page and download the library: Download vinogradsoft/scanner library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vinogradsoft / scanner example snippets
return [
'tasks' => [
'make breakfast' => [
'sandwich' => [
'cut a piece of bread' => [
'take the knife in your right hand',
'cut a piece of bread on a wooden board'
],
'spread butter on bread',
'put a piece of cheese on top',
],
'coffee' => [
'take a cup',
'pour coffee into a cup',
],
]
],
'other' => [
'setting1' => 'value1',
'setting2' => 'value2',
'setting3' => 'value3'
]
];
declare(strict_types=1);
namespace Example;
use Vinograd\Scanner\AbstractTraversalStrategy;
use Vinograd\Scanner\Visitor;
class Handler implements Visitor
{
public function scanStarted(AbstractTraversalStrategy $scanStrategy, mixed $detect): void
{
}
public function scanCompleted(AbstractTraversalStrategy $scanStrategy, mixed $detect): void
{
}
public function visitLeaf(AbstractTraversalStrategy $scanStrategy, mixed $parentNode, mixed $currentElement, mixed $data = null): void
{
$leaf = array_shift($currentElement);
echo 'Execute: ', $leaf, PHP_EOL;
}
public function visitNode(AbstractTraversalStrategy $scanStrategy, mixed $parentNode, mixed $currentNode, mixed $data = null): void
{
$nodeName = array_key_first($currentNode);
echo 'Start: ' . $nodeName, PHP_EOL;
}
}
declare(strict_types=1);
namespace Example;
use Vinograd\Scanner\AbstractTraversalStrategy;
use Vinograd\Scanner\Scanner;
use Vinograd\Scanner\Visitor;
class ProxyHandler implements Visitor
{
private Visitor $handler;
private Scanner $scanner;
public function __construct(Visitor $handler, Scanner $scanner)
{
$this->handler = $handler;
$this->scanner = $scanner;
}
public function scanStarted(AbstractTraversalStrategy $scanStrategy, mixed $detect): void
{
$this->handler->scanStarted($scanStrategy, $detect);
}
public function scanCompleted(AbstractTraversalStrategy $scanStrategy, mixed $detect): void
{
$this->handler->scanCompleted($scanStrategy, $detect);
}
public function visitLeaf(AbstractTraversalStrategy $scanStrategy, mixed $parentNode, mixed $currentElement, mixed $data = null): void
{
$this->handler->visitLeaf($scanStrategy, $parentNode, $currentElement);
}
public function visitNode(AbstractTraversalStrategy $scanStrategy, mixed $parentNode, mixed $currentNode, mixed $data = null): void
{
$name = array_key_first($currentNode);
if ($name !== 'tasks') {
$this->handler->visitNode($scanStrategy, $parentNode, $currentNode);
}
$this->scanner->traverse($currentNode[$name]);
}
}
declare(strict_types=1);
namespace Example;
class FilterForNodeOther implements \Vinograd\Scanner\Filter
{
public function filter(mixed $element): bool
{
$name = array_key_first($element);
return $name !== 'other';
}
}
declare(strict_types=1);
namespace Example;
use Vinograd\Scanner\ArrayDriver;
use Vinograd\Scanner\Scanner;
use Vinograd\Scanner\SingleStrategy;
class Application
{
public function run(array $config): void
{
$scanner = new Scanner();
$scanner->setDriver(new ArrayDriver());
$scanner->setVisitor(new ProxyHandler(new Handler(), $scanner));
$scanner->setStrategy(new SingleStrategy());
$scanner->addNodeFilter(new FilterForNodeOther());
$scanner->traverse($config);
}
}