PropertyHandler.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Reflection;
  6. /**
  7. * Temando Property Handler
  8. *
  9. * @package Temando\Shipping\Rest
  10. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  11. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  12. * @link http://www.temando.com/
  13. */
  14. class PropertyHandler implements PropertyHandlerInterface
  15. {
  16. /**
  17. * Convert snake case to UpperCamelCase.
  18. *
  19. * @param string $key
  20. * @return string
  21. */
  22. public function camelizeUp($key)
  23. {
  24. // separate
  25. $key = str_replace('_', ' ', $key);
  26. // camelize separated words
  27. $key = ucwords($key);
  28. // remove whitespace
  29. $key = str_replace(' ', '', $key);
  30. return $key;
  31. }
  32. /**
  33. * Convert snake case to lowerCamelCase.
  34. *
  35. * @param string $key
  36. * @return string
  37. */
  38. public function camelizeLow($key)
  39. {
  40. // camelize
  41. $key = $this->camelizeUp($key);
  42. // convert first character to lower case
  43. $key = lcfirst($key);
  44. return $key;
  45. }
  46. /**
  47. * Convert Capitalized, UpperCamelCase or lowerCamelCase to snake case.
  48. *
  49. * @param string $key
  50. * @return string
  51. */
  52. public function underscore($key)
  53. {
  54. // separate
  55. $key = preg_replace('/(.)([A-Z])/', "$1_$2", $key);
  56. // convert to lower case
  57. $key = strtolower($key);
  58. return $key;
  59. }
  60. /**
  61. * Obtain getter method name from snake case property name.
  62. *
  63. * @param string $key
  64. * @return string
  65. */
  66. public function getter($key)
  67. {
  68. $key = $this->camelizeUp($key);
  69. return "get$key";
  70. }
  71. /**
  72. * Obtain setter method name from snake case property name.
  73. *
  74. * @param string $key
  75. * @return string
  76. */
  77. public function setter($key)
  78. {
  79. $key = $this->camelizeUp($key);
  80. return "set$key";
  81. }
  82. }