Skip to content

Commit

Permalink
feat: Added onlyVisible optional param for TreeWalker definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Mar 17, 2023
1 parent cacb93c commit abf93a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ final class MultiTypeChildrenDefinition
{
use ContextualDefinitionTrait;

/**
* @var array
*/
private $types;
private array $types;
private bool $onlyVisible;

/**
* @param WalkerContextInterface $context
* @param array<string> $types
* @param bool $onlyVisible
*/
public function __construct(WalkerContextInterface $context, array $types)
public function __construct(WalkerContextInterface $context, array $types, bool $onlyVisible = true)
{
$this->context = $context;
$this->types = $types;
$this->onlyVisible = $onlyVisible;
}

/**
Expand All @@ -38,14 +38,17 @@ public function __invoke(NodesSources $source)
if ($this->context instanceof NodeSourceWalkerContext) {
$this->context->getStopwatch()->start(self::class);
$bag = $this->context->getNodeTypesBag();
$children = $this->context->getNodeSourceApi()->getBy([
$criteria = [
'node.parent' => $source->getNode(),
'node.visible' => true,
'translation' => $source->getTranslation(),
'node.nodeType' => array_map(function (string $singleType) use ($bag) {
return $bag->get($singleType);
}, $this->types)
], [
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
$children = $this->context->getNodeSourceApi()->getBy($criteria, [
'node.position' => 'ASC',
]);
$this->context->getStopwatch()->stop(self::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,29 @@ final class NonReachableNodeSourceBlockDefinition
{
use ContextualDefinitionTrait;

private bool $onlyVisible;

public function __construct(bool $onlyVisible = true)
{
$this->onlyVisible = $onlyVisible;
}

/**
* @throws Exception
*/
public function __invoke(NodesSources $source): array
{
if ($this->context instanceof NodeSourceWalkerContext) {
$this->context->getStopwatch()->start(self::class);
$children = $this->context->getNodeSourceApi()->getBy([
$criteria = [
'node.parent' => $source->getNode(),
'node.visible' => true,
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => false,
], [
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
$children = $this->context->getNodeSourceApi()->getBy($criteria, [
'node.position' => 'ASC',
]);
$this->context->getStopwatch()->stop(self::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,29 @@ final class ReachableNodeSourceDefinition
{
use ContextualDefinitionTrait;

private bool $onlyVisible;

public function __construct(bool $onlyVisible = true)
{
$this->onlyVisible = $onlyVisible;
}

/**
* @throws Exception
*/
public function __invoke(NodesSources $source): array
{
if ($this->context instanceof NodeSourceWalkerContext) {
$this->context->getStopwatch()->start(self::class);
$children = $this->context->getNodeSourceApi()->getBy([
$criteria = [
'node.parent' => $source->getNode(),
'node.visible' => true,
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => true
], [
'node.nodeType.reachable' => true,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
$children = $this->context->getNodeSourceApi()->getBy($criteria, [
'node.position' => 'ASC',
]);
$this->context->getStopwatch()->stop(self::class);
Expand Down

0 comments on commit abf93a8

Please sign in to comment.