Skip to content

Commit

Permalink
TASK: Cleanup usage of NodeTypeConstraintsWithSubNodeTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Nov 1, 2023
1 parent 5df5430 commit 7edbbdc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePointSet;
use Neos\ContentRepository\Core\DimensionSpace\Exception\DimensionSpacePointNotFound;
use Neos\ContentRepository\Core\NodeType\ConstraintCheck;
use Neos\ContentRepository\Core\SharedModel\Exception\RootNodeAggregateDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamDoesNotExistYet;
use Neos\ContentRepository\Core\SharedModel\Exception\DimensionSpacePointIsNotYetOccupied;
Expand Down Expand Up @@ -216,18 +217,15 @@ protected function requireNodeTypeToAllowNodesOfTypeInReference(
if (is_null($propertyDeclaration)) {
throw ReferenceCannotBeSet::becauseTheNodeTypeDoesNotDeclareIt($referenceName, $nodeTypeName);
}
if (isset($propertyDeclaration['constraints']['nodeTypes'])) {
$nodeTypeConstraints = NodeTypeConstraintsWithSubNodeTypes::createFromNodeTypeDeclaration(
$propertyDeclaration['constraints']['nodeTypes'],
$this->getNodeTypeManager()

$constraints = $propertyDeclaration['constraints']['nodeTypes'] ?? [];

if (!ConstraintCheck::create($constraints)->isNodeTypeAllowed($nodeType)) {
throw ReferenceCannotBeSet::becauseTheConstraintsAreNotMatched(
$referenceName,
$nodeTypeName,
$nodeTypeNameInQuestion
);
if (!$nodeTypeConstraints->matches($nodeTypeNameInQuestion)) {
throw ReferenceCannotBeSet::becauseTheConstraintsAreNotMatched(
$referenceName,
$nodeTypeName,
$nodeTypeNameInQuestion
);
}
}
}

Expand Down
16 changes: 12 additions & 4 deletions Neos.ContentRepository.Core/Classes/NodeType/ConstraintCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@
* Performs node type constraint checks against a given set of constraints
* @internal
*/
class ConstraintCheck
final readonly class ConstraintCheck
{
/**
* @param array<string,mixed> $constraints
* @param array<string,bool> $constraints
*/
public function __construct(
private readonly array $constraints
private function __construct(
private array $constraints
) {
}

/**
* @param array<string,bool> $constraints
*/
public static function create(array $constraints): self
{
return new self($constraints);
}

public function isNodeTypeAllowed(NodeType $nodeType): bool
{
$directConstraintsResult = $this->isNodeTypeAllowedByDirectConstraints($nodeType);
Expand Down
2 changes: 1 addition & 1 deletion Neos.ContentRepository.Core/Classes/NodeType/NodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public function getNodeTypeNameOfTetheredNode(NodeName $nodeName): NodeTypeName
public function allowsChildNodeType(NodeType $nodeType): bool
{
$constraints = $this->getConfiguration('constraints.nodeTypes') ?: [];
return (new ConstraintCheck($constraints))->isNodeTypeAllowed($nodeType);
return ConstraintCheck::create($constraints)->isNodeTypeAllowed($nodeType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function isNodeTypeAllowedAsChildToTetheredNode(NodeType $parentNodeType,

$constraints = Arrays::arrayMergeRecursiveOverrule($constraints, $childNodeConstraintConfiguration);

return (new ConstraintCheck($constraints))->isNodeTypeAllowed($nodeType);
return ConstraintCheck::create($constraints)->isNodeTypeAllowed($nodeType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,6 @@ private function __construct(
) {
}

/**
* @param array<string,bool> $nodeTypeDeclaration
*/
public static function createFromNodeTypeDeclaration(
array $nodeTypeDeclaration,
NodeTypeManager $nodeTypeManager
): self {
$wildCardAllowed = false;
$explicitlyAllowedNodeTypeNames = [];
$explicitlyDisallowedNodeTypeNames = [];
foreach ($nodeTypeDeclaration as $constraintName => $allowed) {
if ($constraintName === '*') {
$wildCardAllowed = $allowed;
} else {
if ($allowed) {
$explicitlyAllowedNodeTypeNames[] = $constraintName;
} else {
$explicitlyDisallowedNodeTypeNames[] = $constraintName;
}
}
}

return new self(
$wildCardAllowed,
self::expandByIncludingSubNodeTypes(
NodeTypeNames::fromStringArray($explicitlyAllowedNodeTypeNames),
$nodeTypeManager
),
self::expandByIncludingSubNodeTypes(
NodeTypeNames::fromStringArray($explicitlyDisallowedNodeTypeNames),
$nodeTypeManager
)
);
}

public static function allowAll(): self
{
return new self(
true,
NodeTypeNames::createEmpty(),
NodeTypeNames::createEmpty(),
);
}

public static function create(NodeTypeConstraints $nodeTypeConstraints, NodeTypeManager $nodeTypeManager): self
{
// in case there are no filters, we fall back to allowing every node type.
Expand Down Expand Up @@ -137,22 +93,4 @@ public function matches(NodeTypeName $nodeTypeName): bool
// otherwise, we return $wildcardAllowed.
return $this->isWildCardAllowed;
}

public function toFilterString(): string
{
$parts = [];
if ($this->isWildCardAllowed) {
$parts[] = '*';
}

foreach ($this->explicitlyDisallowedNodeTypeNames as $nodeTypeName) {
$parts[] = '!' . $nodeTypeName->value;
}

foreach ($this->explicitlyAllowedNodeTypeNames as $nodeTypeName) {
$parts[] = $nodeTypeName->value;
}

return implode(',', $parts);
}
}

0 comments on commit 7edbbdc

Please sign in to comment.