eZ\Publish\Core\Limitation\ObjectStateLimitationType::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
return boolean
    public function evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets = null)
    {
        if (!$value instanceof APIObjectStateLimitation) {
            throw new InvalidArgumentException('$value', 'Must be of type: APIObjectStateLimitation');
        }
        if ($object instanceof Content) {
            $object = $object->getVersionInfo()->getContentInfo();
        } elseif ($object instanceof VersionInfo) {
            $object = $object->getContentInfo();
        } elseif (!$object instanceof ContentInfo && !$object instanceof ContentCreateStruct) {
            throw new InvalidArgumentException('$object', 'Must be of type: Content, VersionInfo or ContentInfo');
        }
        if (empty($value->limitationValues)) {
            return false;
        }
        $objectStateIdArray = array();
        $objectStateHandler = $this->persistence->objectStateHandler();
        $stateGroups = $objectStateHandler->loadAllGroups();
        // First deal with unpublished content
        if ($object instanceof ContentCreateStruct || !$object->published) {
            foreach ($stateGroups as $stateGroup) {
                $states = $objectStateHandler->loadObjectStates($stateGroup->id);
                if (empty($states)) {
                    continue;
                }
                $defaultStateId = null;
                $defaultStatePriority = -1;
                foreach ($states as $state) {
                    if ($state->priority > $defaultStatePriority) {
                        $defaultStateId = $state->id;
                        $defaultStatePriority = $state->priority;
                    }
                }
                if ($defaultStateId === null) {
                    throw new BadStateException('$defaultStateId', "Could not find a default state for object state group {$stateGroup->id}");
                }
                $objectStateIdArray[] = $defaultStateId;
            }
        } else {
            /*
             * @var $object ContentInfo
             */
            foreach ($stateGroups as $stateGroup) {
                $objectStateIdArray[] = $objectStateHandler->getContentState($object->id, $stateGroup->id)->id;
            }
        }
        $intersect = array_intersect($value->limitationValues, $objectStateIdArray);
        return !empty($intersect);
    }