Decorator.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage StrikeIron
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Decorates a StrikeIron response object returned by the SOAP extension
  24. * to provide more a PHP-like interface.
  25. *
  26. * @category Zend
  27. * @package Zend_Service
  28. * @subpackage StrikeIron
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Service_StrikeIron_Decorator
  33. {
  34. /**
  35. * Name of the decorated object
  36. * @var null|string
  37. */
  38. protected $_name = null;
  39. /**
  40. * Object to decorate
  41. * @var object
  42. */
  43. protected $_object = null;
  44. /**
  45. * Class constructor
  46. *
  47. * @param object $object Object to decorate
  48. * @param null|string $name Name of the object
  49. */
  50. public function __construct($object, $name = null)
  51. {
  52. $this->_object = $object;
  53. $this->_name = $name;
  54. }
  55. /**
  56. * Proxy property access to the decorated object, inflecting
  57. * the property name and decorating any child objects returned.
  58. * If the property is not found in the decorated object, return
  59. * NULL as a convenience feature to avoid notices.
  60. *
  61. * @param string $property Property name to retrieve
  62. * @return mixed Value of property or NULL
  63. */
  64. public function __get($property)
  65. {
  66. $result = null;
  67. if (! isset($this->_object->$property)) {
  68. $property = $this->_inflect($property);
  69. }
  70. if (isset($this->_object->$property)) {
  71. $result = $this->_object->$property;
  72. $result = $this->_decorate($result);
  73. }
  74. return $result;
  75. }
  76. /**
  77. * Proxy method calls to the decorated object. This will only
  78. * be used when the SOAPClient returns a custom PHP object via
  79. * its classmap option so no inflection is done.
  80. *
  81. * @param string $method Name of method called
  82. * @param array $args Arguments for method
  83. */
  84. public function __call($method, $args)
  85. {
  86. return call_user_func_array(array($this->_object, $method), $args);
  87. }
  88. /**
  89. * Inflect a property name from PHP-style to the result object's
  90. * style. The default implementation here only inflects the case
  91. * of the first letter, e.g. from "fooBar" to "FooBar".
  92. *
  93. * @param string $property Property name to inflect
  94. * @return string Inflected property name
  95. */
  96. protected function _inflect($property)
  97. {
  98. return ucfirst($property);
  99. }
  100. /**
  101. * Decorate a value returned by the result object. The default
  102. * implementation here only decorates child objects.
  103. *
  104. * @param mixed $result Value to decorate
  105. * @return mixed Decorated result
  106. */
  107. protected function _decorate($result)
  108. {
  109. if (is_object($result)) {
  110. $result = new self($result);
  111. }
  112. return $result;
  113. }
  114. /**
  115. * Return the object being decorated
  116. *
  117. * @return object
  118. */
  119. public function getDecoratedObject()
  120. {
  121. return $this->_object;
  122. }
  123. /**
  124. * Return the name of the object being decorated
  125. *
  126. * @return null|string
  127. */
  128. public function getDecoratedObjectName()
  129. {
  130. return $this->_name;
  131. }
  132. }