Exemple #1
0
 /**
  * Get custom attribute metadata for the given class/interface.
  *
  * @param string $dataObjectClassName
  * @return \Magento\Framework\Api\MetadataObjectInterface[]
  */
 protected function getAttributesMetadata($dataObjectClassName)
 {
     $attributes = [];
     $allAttributes = $this->serviceConfigReader->read();
     if (isset($allAttributes[$dataObjectClassName]) && is_array($allAttributes[$dataObjectClassName])) {
         $attributeCodes = array_keys($allAttributes[$dataObjectClassName]);
         foreach ($attributeCodes as $attributeCode) {
             $this->attributeMetadataBuilder->setAttributeCode($attributeCode);
             $attributes[$attributeCode] = $this->attributeMetadataBuilder->create();
         }
     }
     return $attributes;
 }
Exemple #2
0
 /**
  * Convert custom attribute data array to array of AttributeValue Data Object
  *
  * @param array $customAttributesValueArray
  * @param string $returnType
  * @param string $dataObjectClassName
  * @return AttributeValue[]
  */
 protected function convertCustomAttributeValue($customAttributesValueArray, $returnType, $dataObjectClassName)
 {
     $result = [];
     $allAttributes = $this->serviceConfigReader->read();
     $dataObjectClassName = ltrim($dataObjectClassName, '\\');
     if (!isset($allAttributes[$dataObjectClassName])) {
         return $this->_convertValue($customAttributesValueArray, $returnType);
     }
     $dataObjectAttributes = $allAttributes[$dataObjectClassName];
     $camelCaseAttributeCodeKey = lcfirst(SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE));
     foreach ($customAttributesValueArray as $customAttribute) {
         if (isset($customAttribute[AttributeValue::ATTRIBUTE_CODE])) {
             $customAttributeCode = $customAttribute[AttributeValue::ATTRIBUTE_CODE];
         } elseif (isset($customAttribute[$camelCaseAttributeCodeKey])) {
             $customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey];
         } else {
             $customAttributeCode = null;
         }
         //Check if type is defined, else default to mixed
         $type = isset($dataObjectAttributes[$customAttributeCode]) ? $dataObjectAttributes[$customAttributeCode] : TypeProcessor::ANY_TYPE;
         $customAttributeValue = $customAttribute[AttributeValue::VALUE];
         if (is_array($customAttributeValue)) {
             //If type for AttributeValue's value as array is mixed, further processing is not possible
             if ($type === TypeProcessor::ANY_TYPE) {
                 continue;
             }
             $attributeValue = $this->_createDataObjectForTypeAndArrayValue($type, $customAttributeValue);
         } else {
             $attributeValue = $this->_convertValue($customAttributeValue, $type);
         }
         //Populate the attribute value data object once the value for custom attribute is derived based on type
         $result[] = $this->attributeValueBuilder->setAttributeCode($customAttributeCode)->setValue($attributeValue)->create();
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function resolveObjectType($attributeCode, $value, $context)
 {
     if (!is_object($value)) {
         throw new \InvalidArgumentException('Provided value is not object type');
     }
     $data = $this->configReader->read();
     $context = trim($context, '\\');
     $config = isset($data[$context]) ? $data[$context] : [];
     $output = get_class($value);
     if (isset($config[$attributeCode])) {
         $type = $config[$attributeCode]['type'];
         $output = $this->typeProcessor->getArrayItemType($type);
         if (!(class_exists($output) || interface_exists($output))) {
             throw new \LogicException(sprintf('Class "%s" does not exist. Please note that namespace must be specified.', $type));
         }
     }
     return $output;
 }
 /**
  * @param string $typeName
  * @param string $attributeCode
  * @return string[] A list of permissions
  */
 private function getPermissionsForTypeAndMethod($typeName, $attributeCode)
 {
     // TODO: Move function to the Config and hope this is cached
     $attributes = $this->configReader->read();
     if (isset($attributes[$typeName]) && isset($attributes[$typeName][$attributeCode])) {
         $attributeMetadata = $attributes[$typeName][$attributeCode];
         $permissions = [];
         foreach ($attributeMetadata[Converter::RESOURCE_PERMISSIONS] as $permission) {
             $permissions[] = $permission;
         }
         return $permissions;
     }
     return [];
 }
 /**
  * Retrieve a list of attributes associated with current source class.
  *
  * @return array
  */
 protected function getCustomAttributes()
 {
     if (!isset($this->allCustomAttributes)) {
         $this->allCustomAttributes = $this->configReader->read();
     }
     $dataInterface = ltrim($this->getSourceClassName(), '\\');
     if (isset($this->allCustomAttributes[$dataInterface])) {
         foreach ($this->allCustomAttributes[$dataInterface] as $attributeName => $attributeType) {
             if (strpos($attributeType, '\\') !== false) {
                 /** Add preceding slash to class names, while leaving primitive types as is */
                 $attributeType = $this->_getFullyQualifiedClassName($attributeType);
                 $this->allCustomAttributes[$dataInterface][$attributeName] = $this->_getFullyQualifiedClassName($attributeType);
             }
         }
         return $this->allCustomAttributes[$dataInterface];
     } else {
         return [];
     }
 }