/**
  * Add separate flow for annotated data objects.
  *
  * @param \Magento\Framework\Webapi\ServiceOutputProcessor $subject
  * @param \Closure $proceed
  * @param $data
  * @param $type
  * @return mixed
  */
 public function aroundConvertValue(\Magento\Framework\Webapi\ServiceOutputProcessor $subject, \Closure $proceed, $data, $type)
 {
     if ($data instanceof \Flancer32\Lib\DataObject) {
         $result = [];
         $typeData = $this->_typePropertiesRegistry->register($type);
         /**
          * @var string $propertyName
          * @var \Praxigento\Core\Reflection\Data\Property $propertyData
          */
         foreach ($typeData as $propertyName => $propertyData) {
             $attrName = $this->_toolConvert->camelCaseToSnakeCase($propertyName);
             $getter = 'get' . ucfirst($propertyName);
             $value = call_user_func([$data, $getter]);
             $isRequired = $propertyData->getIsRequired();
             $propertyType = $propertyData->getType();
             if ($isRequired || $value) {
                 if ($this->_toolType->isSimple($propertyType)) {
                     $result[$attrName] = $value;
                 } else {
                     // the last 2 chars will be removed for arrays
                     // in \Magento\Framework\Webapi\ServiceOutputProcessor::convertValue
                     if (is_array($value)) {
                         $propertyType = $this->_toolType->getTypeAsArrayOfTypes($propertyType);
                     }
                     $result[$attrName] = $proceed($value, $propertyType);
                 }
             }
         }
     } else {
         $result = $proceed($data, $type);
     }
     return $result;
 }
 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];
 }
Exemplo n.º 4
0
 /**
  * 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;
 }
Exemplo n.º 5
0
 /**
  * @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;
 }