public function test_normalizeType() { /** === Test Data === */ $TYPE_FULL = '\\Some\\Type\\Here[]'; $TYPE = 'Some\\Type\\Here'; /** === Setup Mocks === */ // $result = $this->_typeProcessor->normalizeType($type); $this->mTypeProcessor->shouldReceive('normalizeType')->once()->andReturn($TYPE_FULL); /** === Call and asserts === */ $res = $this->obj->normalizeType($TYPE_FULL); $this->assertEquals($TYPE, $res); }
/** * Analyze $type and save type properties into the registry. * * @param string $type * @return \Praxigento\Core\Reflection\Data\Property[] array with type properties or empty array for simple types. */ public function register($type) { $typeNorm = $this->_toolsType->normalizeType($type); $isSimple = $this->_typeProcessor->isTypeSimple($typeNorm); if (!isset($this->_registry[$typeNorm])) { if (!$isSimple) { /* analyze properties for complex type */ $this->_registry[$typeNorm] = []; /* process annotated methods */ /** @var \Zend\Code\Reflection\ClassReflection $reflection */ $reflection = new \Zend\Code\Reflection\ClassReflection($typeNorm); $docBlock = $reflection->getDocBlock(); if ($docBlock) { $this->_processDocBlock($typeNorm, $docBlock); } /* process normal methods (not annotated) */ $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); $this->_processMethods($typeNorm, $methods); } else { /* this is simple type w/o props */ $this->_registry[$typeNorm] = []; } } return $this->_registry[$typeNorm]; }
/** * @param string $type * @param array $data * @return DataObject|mixed */ public function parseArrayData($type, $data) { $isArray = $this->_toolType->isArray($type); $typeNorm = $this->_toolType->normalizeType($type); if (is_subclass_of($typeNorm, \Flancer32\Lib\DataObject::class)) { /* Process data objects separately. Register annotated class and parse parameters types. */ if ($isArray) { /* process $data as array of $types */ $result = []; foreach ($data as $key => $item) { $result[$key] = $this->parseArrayDataRecursive($typeNorm, $item); } } else { /* process $data as data object of $type */ $typeData = $this->_typePropsRegistry->register($typeNorm); $result = $this->_manObj->create($typeNorm); foreach ($data as $key => $value) { $propName = $this->_toolType->formatPropertyName($key); if (isset($typeData[$propName])) { /** @var \\Praxigento\Core\Reflection\Data\Property $propertyData */ $propertyData = $typeData[$propName]; $propertyType = $propertyData->getType(); $propertyIsArray = $propertyData->getIsArray(); if ($propertyIsArray) { /* property is the array of types */ $propertyType = $this->_toolType->getTypeAsArrayOfTypes($propertyType); $complex = $this->parseArrayDataRecursive($propertyType, $value); $result->setData($propName, $complex); } else { if ($this->_toolType->isSimple($propertyType)) { /* property is the simple type */ $result->setData($propName, $value); } else { /* property is the complex type, we need to convert recursively */ $complex = $this->parseArrayDataRecursive($propertyType, $value); $result->setData($propName, $complex); } } } } } } else { $result = $data; } return $result; }
/** * Analyze given type and return methods meta data (name, return type, description, etc.) * * @param string $type * @return \Praxigento\Core\Reflection\Data\Method[] */ public function getMethods($type) { $result = []; $typeNorm = $this->_toolsType->normalizeType($type); /** @var \Zend\Code\Reflection\ClassReflection $reflection */ $reflection = $this->_manObj->create(\Zend\Code\Reflection\ClassReflection::class, ['argument' => $typeNorm]); /** @var \Zend\Code\Reflection\DocBlockReflection $docBlock */ $docBlock = $reflection->getDocBlock(); $annotatedMethods = $this->_processClassDocBlock($docBlock); /* process normal methods (not annotated) */ $publicMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); $generalMethods = $this->_processClassMethods($publicMethods); $merged = array_merge($generalMethods, $annotatedMethods); /* convert results to array form according Magento requirements to be saved in the cache */ /** @var \Praxigento\Core\Reflection\Data\Method $item */ foreach ($merged as $item) { $methodName = $item->getName(); $entry = ['type' => $item->getType(), 'isRequired' => $item->getIsRequired(), 'description' => $item->getDescription(), 'parameterCount' => $item->getParameterCount()]; $result[$methodName] = $entry; } return $result; }