eZ\Publish\Core\Limitation\ParentDepthLimitationType::evaluate PHP Method

evaluate() public method

Evaluate permission against content & target(placement/parent/assignment).
public evaluate ( eZ\Publish\API\Repository\Values\User\Limitation $value, eZ\Publish\API\Repository\Values\User\UserReference $currentUser, eZ\Publish\API\Repository\Values\ValueObject $object, array $targets = null ) : boolean
$value eZ\Publish\API\Repository\Values\User\Limitation
$currentUser eZ\Publish\API\Repository\Values\User\UserReference
$object eZ\Publish\API\Repository\Values\ValueObject
$targets array The context of the $object, like Location of Content, if null none where provided by caller
return boolean
    public function evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets = null)
    {
        if (!$value instanceof APIParentDepthLimitation) {
            throw new InvalidArgumentException('$value', 'Must be of type: APIParentDepthLimitation');
        }
        if ($object instanceof ContentCreateStruct) {
            return $this->evaluateForContentCreateStruct($value, $targets);
        } elseif ($object instanceof Content) {
            $object = $object->getVersionInfo()->getContentInfo();
        } elseif ($object instanceof VersionInfo) {
            $object = $object->getContentInfo();
        } elseif (!$object instanceof ContentInfo) {
            throw new InvalidArgumentException('$object', 'Must be of type: ContentCreateStruct, Content, VersionInfo or ContentInfo');
        }
        // Load locations if no specific placement was provided
        if (empty($targets)) {
            if ($object->published) {
                $targets = $this->persistence->locationHandler()->loadLocationsByContent($object->id);
            } else {
                // @todo Need support for draft locations to to work correctly
                $targets = $this->persistence->locationHandler()->loadParentLocationsForDraftContent($object->id);
            }
        }
        // Parent Limitations are usually used by content/create where target is specified,
        // so we return false if not provided.
        if (empty($targets)) {
            return false;
        }
        foreach ($targets as $target) {
            if ($target instanceof Location || $target instanceof SPILocation) {
                $depth = $target->depth;
            } else {
                throw new InvalidArgumentException('$targets', 'Must contain objects of type: Location');
            }
            // All placements must match
            if (!in_array($depth, $value->limitationValues)) {
                return false;
            }
        }
        return true;
    }