/**
  * Test if a value is accepted or not
  *
  * @param AttributeInterface $attribute
  * @param array              $value
  *
  * @return boolean
  */
 protected function acceptValue(AttributeInterface $attribute, $value, array $options = [])
 {
     if (null !== $value['locale']) {
         $locale = $this->getLocale($value['locale']);
         if (null === $locale) {
             return false;
         }
         if (!$attribute->isLocalizable()) {
             return false;
         }
         if (!$locale->isActivated()) {
             return false;
         }
         if ($this->objectFilter->filterObject($this->getLocale($value['locale']), 'pim.internal_api.locale.edit', $options)) {
             return false;
         }
         if ($attribute->isLocaleSpecific() && !in_array($value['locale'], $attribute->getLocaleSpecificCodes())) {
             return false;
         }
     }
     if (null !== $value['scope']) {
         if (!$attribute->isScopable()) {
             return false;
         }
         if (null === $this->getChannel($value['scope'])) {
             return false;
         }
     }
     return true;
 }
 /**
  * @param array $valuesData
  *
  * @return array
  */
 protected function filterValuesData($valuesData)
 {
     $newValuesData = [];
     foreach ($valuesData as $attributeCode => $contextValues) {
         $attribute = $this->getAttribute($attributeCode);
         if (!$this->objectFilter->filterObject($attribute, 'pim.internal_api.attribute.edit')) {
             $newContextValues = [];
             foreach ($contextValues as $contextValue) {
                 if (null === $contextValue['locale'] || !$this->objectFilter->filterObject($this->getLocale($contextValue['locale']), 'pim.internal_api.locale.edit')) {
                     $newContextValues[] = $contextValue;
                 }
             }
             $newValuesData[$attributeCode] = $newContextValues;
         }
     }
     return array_filter($newValuesData);
 }
 /**
  * Find a product by its id or return a 404 response
  *
  * @param string $id the product id
  *
  * @throws NotFoundHttpException
  *
  * @return ProductInterface
  */
 protected function findProductOr404($id)
 {
     $product = $this->productRepository->findOneByWithValues($id);
     $product = $this->objectFilter->filterObject($product, 'pim.internal_api.product.view') ? null : $product;
     if (!$product) {
         throw new NotFoundHttpException(sprintf('Product with id %s could not be found.', $id));
     }
     return $product;
 }
 /**
  * Get attribute by identifier
  *
  * @param string $identifier
  *
  * @return JsonResponse
  */
 public function getAction($identifier)
 {
     $attribute = $this->attributeRepository->findOneByIdentifier($identifier);
     $attribute = $this->attributeFilter->filterObject($attribute, 'pim.internal_api.attribute.view') ? null : $attribute;
     if (null === $attribute) {
         throw new NotFoundHttpException(sprintf('Attribute with code "%s" not found', $identifier));
     }
     return new JsonResponse($this->normalizer->normalize($attribute, 'internal_api'));
 }
 /**
  * @param AttributeInterface $attribute
  * @param array              $values
  *
  * @throws ObjectNotFoundException
  *
  * @return array
  */
 protected function getNewValuesData(AttributeInterface $attribute, array $values)
 {
     $newValues = [];
     foreach ($values as $value) {
         $acceptValue = true;
         if (null !== $value['locale']) {
             $isAuthorizedOnLocale = !$this->objectFilter->filterObject($this->getLocale($value['locale']), 'pim.internal_api.locale.edit');
             $isEditableOnLocale = $attribute->isLocaleSpecific() ? in_array($value['locale'], $attribute->getLocaleSpecificCodes()) : true;
             $acceptValue = $isAuthorizedOnLocale && $isEditableOnLocale;
         }
         if ($acceptValue) {
             $newValues[] = $value;
         }
     }
     return $newValues;
 }