public function normalize($object, $format = null, array $context = [])
    {
        $data = $this->decorated->normalize($object, $format, $context);
        $class = get_class($object);

        if ($this->entityCollection->contains($class)) {
            $token = $this->tokenStorage->getToken();

            if (!$token) {
                throw new LogicException('Token is not defined.');
            }

            switch (true) {
                case array_key_exists('collection_operation_name', $context):
                    $attribute = Permission::BROWSE;
                    break;

                case array_key_exists('item_operation_name', $context):
                    $attribute = Permission::READ;
                    break;

                default:
                    throw new DomainException('Operation does not exist.');
            }

            $subject = [
                0 => $object,
                1 => null
            ];

            foreach (array_keys($data) as $property) {
                $subject[1] = $property;
                $vote = $this->voter->vote($token, $subject, [$attribute]);

                if (VoterInterface::ACCESS_ABSTAIN === $vote) {
                    throw new LogicException('Voter cannot abstain from voting.');
                }

                if (VoterInterface::ACCESS_DENIED === $vote) {
                    unset($data[$property]);
                }
            }
        }

        return $data;
    }
public function normalize($object, $format = null, array $context = [])
    {
        $data = $this->decorated->normalize($object, $format, $context);
        $class = get_class($object);

        if ($this->entityCollection->contains($class)) {
            $token = $this->tokenStorage->getToken();

            if (!$token) {
                throw new LogicException('Token is not defined.');
            }

            switch (true) {
                case array_key_exists('collection_operation_name', $context):
                    $attribute = Permission::BROWSE;
                    break;

                case array_key_exists('item_operation_name', $context):
                    $attribute = Permission::READ;
                    break;

                default:
                    throw new DomainException('Operation does not exist.');
            }

            $subject = [
                0 => $object,
                1 => null
            ];

            foreach (array_keys($data) as $property) {
                $subject[1] = $property;
                $vote = $this->voter->vote($token, $subject, [$attribute]);

                if (VoterInterface::ACCESS_ABSTAIN === $vote) {
                    throw new LogicException('Voter cannot abstain from voting.');
                }

                if (VoterInterface::ACCESS_DENIED === $vote) {
                    unset($data[$property]);
                }
            }
        }

        return $data;
    }
{@inheritdoc}