Reflection.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest\SchemaMapper;
  6. use Magento\Framework\Reflection\TypeProcessor;
  7. use Temando\Shipping\Rest\SchemaMapper\Reflection\ReflectionInterface;
  8. /**
  9. * Temando API Type Reflection Utility
  10. *
  11. * @package Temando\Shipping\Rest
  12. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  13. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  14. * @link http://www.temando.com/
  15. */
  16. class Reflection implements ReflectionInterface
  17. {
  18. /**
  19. * @var TypeProcessor
  20. */
  21. private $typeProcessor;
  22. /**
  23. * Reflection constructor.
  24. * @param TypeProcessor $typeProcessor
  25. */
  26. public function __construct(TypeProcessor $typeProcessor)
  27. {
  28. $this->typeProcessor = $typeProcessor;
  29. }
  30. /**
  31. * @param \stdClass $type
  32. * @param string $property
  33. * @return string
  34. */
  35. public function getPropertyType($type, $property)
  36. {
  37. try {
  38. $reflectionClass = new \Zend\Code\Reflection\ClassReflection($type);
  39. $tag = $reflectionClass->getProperty($property)->getDocBlock()->getTag('var');
  40. } catch (\ReflectionException $e) {
  41. return '';
  42. }
  43. if ($tag instanceof \Zend\Code\Reflection\DocBlock\Tag\PhpDocTypedTagInterface) {
  44. $propertyTypes = $tag->getTypes();
  45. return current($propertyTypes);
  46. } elseif ($tag instanceof \Zend\Code\Reflection\DocBlock\Tag\GenericTag) {
  47. return $tag->getContent();
  48. }
  49. return '';
  50. }
  51. /**
  52. * @param \stdClass $type
  53. * @param string $getter
  54. * @return mixed
  55. */
  56. public function getReturnValueType($type, $getter)
  57. {
  58. try {
  59. $reflectionMethod = new \Zend\Code\Reflection\MethodReflection($type, $getter);
  60. $typeInfo = $this->typeProcessor->getGetterReturnType($reflectionMethod);
  61. } catch (\ReflectionException $e) {
  62. return null;
  63. }
  64. return $typeInfo['type'];
  65. }
  66. }