/**
  * Test applyTy method of builder
  *
  * ApplyTo method transform string to array
  *
  * @dataProvider applyToDataProvider()
  *
  * @param $applyTo
  */
 public function testApplyTo($applyTo)
 {
     $objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
     /** @var \Magento\Catalog\Service\V1\Data\Eav\OptionBuilder $optionBuilder */
     $optionBuilder = $objectManager->getObject('Magento\\Catalog\\Service\\V1\\Data\\Eav\\OptionBuilder');
     /** @var \Magento\Catalog\Service\V1\Data\Eav\ValidationRuleBuilder $validationRuleBuilder */
     $validationRuleBuilder = $objectManager->getObject('Magento\\Catalog\\Service\\V1\\Data\\Eav\\ValidationRuleBuilder');
     $frontendLabelBuilder = $objectManager->getObject('Magento\\Catalog\\Service\\V1\\Data\\Eav\\Product\\Attribute\\FrontendLabelBuilder');
     $attributeBuilder = $objectManager->getObject('Magento\\Catalog\\Service\\V1\\Data\\Eav\\AttributeMetadataBuilder', ['optionBuilder' => $optionBuilder, 'validationRuleBuilder' => $validationRuleBuilder, 'frontendLabelBuilder' => $frontendLabelBuilder]);
     $attributeBuilder->populateWithArray([AttributeMetadata::APPLY_TO => $applyTo]);
     $attributeMetadata = new AttributeMetadata($attributeBuilder);
     $this->assertTrue(is_array($attributeMetadata->getApplyTo()));
     $this->assertEquals(3, count($attributeMetadata->getApplyTo()));
     $attributeBuilder->setApplyTo($applyTo);
     $attributeMetadata = new AttributeMetadata($attributeBuilder);
     $this->assertTrue(is_array($attributeMetadata->getApplyTo()));
     $this->assertEquals(3, count($attributeMetadata->getApplyTo()));
 }
Beispiel #2
0
 /**
  * Denormalizes a relation.
  *
  * @param ResourceInterface $currentResource
  * @param AttributeMetadata $attributeMetadata
  * @param                   $class
  * @param                   $value
  *
  * @return object
  * @throws InvalidArgumentException
  */
 private function denormalizeRelation(ResourceInterface $currentResource, AttributeMetadata $attributeMetadata, $class, $value)
 {
     if ('Datetime' === $class) {
         $dateTimeNormalizer = new DateTimeNormalizer();
         return $dateTimeNormalizer->denormalize($value, $class ?: null, self::FORMAT);
     }
     $attributeName = $attributeMetadata->getName();
     //        // Always allow IRI to be compliant with the Hydra spec
     //        if (is_string($value)) {
     //            $item = $this->dataProvider->getItemFromIri($value);
     //            if (null === $item) {
     //                throw new InvalidArgumentException(sprintf(
     //                    'IRI  not supported (found "%s" in "%s" of "%s")',
     //                    $value,
     //                    $attributeName,
     //                    $currentResource->getEntityClass()
     //                ));
     //            }
     //            return $item;
     //        }
     if (!$this->resourceCollection->getResourceForEntity($class)) {
         $shortname = ucfirst(Inflector::singularize($attributeName));
         if (!$this->resourceCollection->getResourceForShortName($shortname)) {
             throw new InvalidArgumentException(sprintf('Type not supported (found "%s" in attribute "%s" of "%s")', $class, $attributeName, $currentResource->getEntityClass()));
         } else {
             $resource = $this->resourceCollection->getResourceForShortName($shortname);
             $context = $this->contextBuilder->bootstrapRelation($resource, $resource->getEntityClass());
         }
     } else {
         $context = $this->contextBuilder->bootstrapRelation($currentResource, $class);
     }
     if (!$attributeMetadata->isDenormalizationLink()) {
         $object = $this->denormalize(json_encode($value), $resource->getEntityClass(), self::FORMAT, $context);
         //$this->deserializeObjects[] = $object;
         return $object;
     }
     throw new InvalidArgumentException(sprintf('Nested objects for attribute "%s" of "%s" are not enabled. Use serialization groups to change that behavior.', $attributeName, $currentResource->getEntityClass()));
 }
 /**
  * {@inheritdoc}
  */
 public function addAttribute(AttributeMetadata $attributeMetadata)
 {
     $this->attributes[$attributeMetadata->getName()] = $attributeMetadata;
 }
 /**
  * @param AttributeMetadata $attributeMetadata
  */
 public function removeAttribute(AttributeMetadata $attributeMetadata)
 {
     if (isset($this->attributes[$attributeMetadata->getName()])) {
         unset($this->attributes[$attributeMetadata->getName()]);
     }
 }