/** * 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; }
/** * 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 bool $isPermissionAllowed * @param array $expectedValue * @dataProvider buildOutputDataArrayWithPermissionProvider */ public function testBuildOutputDataArrayWithPermission($isPermissionAllowed, $expectedValue) { $dataObject = new \Magento\Framework\Reflection\Test\Unit\ExtensionAttributesObject(); $dataObjectType = 'Magento\\Framework\\Reflection\\Test\\Unit\\ExtensionAttributesObject'; $methodName = 'getAttrName'; $attributeName = 'attr_name'; $attributeValue = 'attrName'; $this->methodsMapProcessorMock->expects($this->once())->method('getMethodsMap')->with($dataObjectType)->will($this->returnValue([$methodName => []])); $this->methodsMapProcessorMock->expects($this->once())->method('isMethodValidForDataField')->with($dataObjectType, $methodName)->will($this->returnValue(true)); $this->fieldNamerMock->expects($this->once())->method('getFieldNameForMethodName')->with($methodName)->will($this->returnValue($attributeName)); $permissionName = 'Magento_Permission'; $this->configReaderMock->expects($this->once())->method('read')->will($this->returnValue([$dataObjectType => [$attributeName => [Converter::RESOURCE_PERMISSIONS => [$permissionName]]]])); $this->authorizationMock->expects($this->once())->method('isAllowed')->with($permissionName)->will($this->returnValue($isPermissionAllowed)); if ($isPermissionAllowed) { $this->methodsMapProcessorMock->expects($this->once())->method('getMethodReturnType')->with($dataObjectType, $methodName)->will($this->returnValue('string')); $this->typeCasterMock->expects($this->once())->method('castValueToType')->with($attributeValue, 'string')->will($this->returnValue($attributeValue)); } $value = $this->model->buildOutputDataArray($dataObject, $dataObjectType); $this->assertEquals($value, $expectedValue); }
/** * @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 []; } }
public function testGenerateEmptyExtension() { $this->configReaderMock->expects($this->any())->method('read')->willReturn(['Magento\\Catalog\\Api\\Data\\Product' => ['should_not_include' => 'string']]); $expectedResult = file_get_contents(__DIR__ . '/_files/SampleEmptyExtension.txt'); $this->validateGeneratedCode($expectedResult); }