NameFinder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Reflection;
  7. use Zend\Code\Reflection\ClassReflection;
  8. class NameFinder
  9. {
  10. /**
  11. * Convert Data Object getter name into field name.
  12. *
  13. * @param string $getterName
  14. * @return string
  15. */
  16. public function getFieldNameFromGetterName($getterName)
  17. {
  18. if ((strpos($getterName, 'get') === 0)) {
  19. /** Remove 'get' prefix and make the first letter lower case */
  20. $fieldName = substr($getterName, strlen('get'));
  21. } elseif ((strpos($getterName, 'is') === 0)) {
  22. /** Remove 'is' prefix and make the first letter lower case */
  23. $fieldName = substr($getterName, strlen('is'));
  24. } elseif ((strpos($getterName, 'has') === 0)) {
  25. /** Remove 'has' prefix and make the first letter lower case */
  26. $fieldName = substr($getterName, strlen('has'));
  27. } else {
  28. $fieldName = $getterName;
  29. }
  30. return lcfirst($fieldName);
  31. }
  32. /**
  33. * Convert Data Object getter short description into field description.
  34. *
  35. * @param string $shortDescription
  36. * @return string
  37. */
  38. public function getFieldDescriptionFromGetterDescription($shortDescription)
  39. {
  40. return ucfirst(substr(strstr($shortDescription, " "), 1));
  41. }
  42. /**
  43. * Find the getter method name for a property from the given class
  44. *
  45. * @param ClassReflection $class
  46. * @param string $camelCaseProperty
  47. * @return string processed method name
  48. * @throws \LogicException If $camelCaseProperty has no corresponding getter method
  49. */
  50. public function getGetterMethodName(ClassReflection $class, $camelCaseProperty)
  51. {
  52. $getterName = 'get' . $camelCaseProperty;
  53. $boolGetterName = 'is' . $camelCaseProperty;
  54. return $this->findAccessorMethodName($class, $camelCaseProperty, $getterName, $boolGetterName);
  55. }
  56. /**
  57. * Find the setter method name for a property from the given class
  58. *
  59. * @param ClassReflection $class
  60. * @param string $camelCaseProperty
  61. * @return string processed method name
  62. * @throws \LogicException If $camelCaseProperty has no corresponding setter method
  63. */
  64. public function getSetterMethodName(ClassReflection $class, $camelCaseProperty)
  65. {
  66. $setterName = 'set' . $camelCaseProperty;
  67. $boolSetterName = 'setIs' . $camelCaseProperty;
  68. return $this->findAccessorMethodName($class, $camelCaseProperty, $setterName, $boolSetterName);
  69. }
  70. /**
  71. * Find the accessor method name for a property from the given class
  72. *
  73. * @param ClassReflection $class
  74. * @param string $camelCaseProperty
  75. * @param string $accessorName
  76. * @param bool $boolAccessorName
  77. * @return string processed method name
  78. * @throws \LogicException If $camelCaseProperty has no corresponding setter method
  79. */
  80. public function findAccessorMethodName(
  81. ClassReflection $class,
  82. $camelCaseProperty,
  83. $accessorName,
  84. $boolAccessorName
  85. ) {
  86. if ($this->hasMethod($class, $accessorName)) {
  87. $methodName = $accessorName;
  88. return $methodName;
  89. } elseif ($this->hasMethod($class, $boolAccessorName)) {
  90. $methodName = $boolAccessorName;
  91. return $methodName;
  92. } else {
  93. throw new \LogicException(
  94. sprintf(
  95. 'Property "%s" does not have accessor method "%s" in class "%s".',
  96. $camelCaseProperty,
  97. $accessorName,
  98. $class->getName()
  99. )
  100. );
  101. }
  102. }
  103. /**
  104. * Checks if method is defined
  105. *
  106. * Case sensitivity of the method is taken into account.
  107. *
  108. * @param ClassReflection $class
  109. * @param string $methodName
  110. * @return bool
  111. */
  112. public function hasMethod(ClassReflection $class, $methodName)
  113. {
  114. return $class->hasMethod($methodName) && ($class->getMethod($methodName)->getName() == $methodName);
  115. }
  116. }