Composite.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_Soap
  17. * @subpackage Wsdl
  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. * @see Zend_Soap_Wsdl_Strategy_Interface
  24. */
  25. require_once "Zend/Soap/Wsdl/Strategy/Interface.php";
  26. /**
  27. * Zend_Soap_Wsdl_Strategy_Composite
  28. *
  29. * @category Zend
  30. * @package Zend_Soap
  31. * @subpackage Wsdl
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Soap_Wsdl_Strategy_Composite implements Zend_Soap_Wsdl_Strategy_Interface
  36. {
  37. /**
  38. * Typemap of Complex Type => Strategy pairs.
  39. *
  40. * @var array
  41. */
  42. protected $_typeMap = array();
  43. /**
  44. * Default Strategy of this composite
  45. *
  46. * @var string|Zend_Soap_Wsdl_Strategy_Interface
  47. */
  48. protected $_defaultStrategy;
  49. /**
  50. * Context WSDL file that this composite serves
  51. *
  52. * @var Zend_Soap_Wsdl|null
  53. */
  54. protected $_context;
  55. /**
  56. * Construct Composite WSDL Strategy.
  57. *
  58. * @throws Zend_Soap_Wsdl_Exception
  59. * @param array $typeMap
  60. * @param string|Zend_Soap_Wsdl_Strategy_Interface $defaultStrategy
  61. */
  62. public function __construct(array $typeMap=array(), $defaultStrategy="Zend_Soap_Wsdl_Strategy_DefaultComplexType")
  63. {
  64. foreach($typeMap AS $type => $strategy) {
  65. $this->connectTypeToStrategy($type, $strategy);
  66. }
  67. $this->_defaultStrategy = $defaultStrategy;
  68. }
  69. /**
  70. * Connect a complex type to a given strategy.
  71. *
  72. * @throws Zend_Soap_Wsdl_Exception
  73. * @param string $type
  74. * @param string|Zend_Soap_Wsdl_Strategy_Interface $strategy
  75. * @return Zend_Soap_Wsdl_Strategy_Composite
  76. */
  77. public function connectTypeToStrategy($type, $strategy)
  78. {
  79. if(!is_string($type)) {
  80. /**
  81. * @see Zend_Soap_Wsdl_Exception
  82. */
  83. require_once "Zend/Soap/Wsdl/Exception.php";
  84. throw new Zend_Soap_Wsdl_Exception("Invalid type given to Composite Type Map.");
  85. }
  86. $this->_typeMap[$type] = $strategy;
  87. return $this;
  88. }
  89. /**
  90. * Return default strategy of this composite
  91. *
  92. * @throws Zend_Soap_Wsdl_Exception
  93. * @param string $type
  94. * @return Zend_Soap_Wsdl_Strategy_Interface
  95. */
  96. public function getDefaultStrategy()
  97. {
  98. $strategy = $this->_defaultStrategy;
  99. if(is_string($strategy) && class_exists($strategy)) {
  100. $strategy = new $strategy;
  101. }
  102. if( !($strategy instanceof Zend_Soap_Wsdl_Strategy_Interface) ) {
  103. /**
  104. * @see Zend_Soap_Wsdl_Exception
  105. */
  106. require_once "Zend/Soap/Wsdl/Exception.php";
  107. throw new Zend_Soap_Wsdl_Exception(
  108. "Default Strategy for Complex Types is not a valid strategy object."
  109. );
  110. }
  111. $this->_defaultStrategy = $strategy;
  112. return $strategy;
  113. }
  114. /**
  115. * Return specific strategy or the default strategy of this type.
  116. *
  117. * @throws Zend_Soap_Wsdl_Exception
  118. * @param string $type
  119. * @return Zend_Soap_Wsdl_Strategy_Interface
  120. */
  121. public function getStrategyOfType($type)
  122. {
  123. if(isset($this->_typeMap[$type])) {
  124. $strategy = $this->_typeMap[$type];
  125. if(is_string($strategy) && class_exists($strategy)) {
  126. $strategy = new $strategy();
  127. }
  128. if( !($strategy instanceof Zend_Soap_Wsdl_Strategy_Interface) ) {
  129. /**
  130. * @see Zend_Soap_Wsdl_Exception
  131. */
  132. require_once "Zend/Soap/Wsdl/Exception.php";
  133. throw new Zend_Soap_Wsdl_Exception(
  134. "Strategy for Complex Type '".$type."' is not a valid strategy object."
  135. );
  136. }
  137. $this->_typeMap[$type] = $strategy;
  138. } else {
  139. $strategy = $this->getDefaultStrategy();
  140. }
  141. return $strategy;
  142. }
  143. /**
  144. * Method accepts the current WSDL context file.
  145. *
  146. * @param Zend_Soap_Wsdl $context
  147. */
  148. public function setContext(Zend_Soap_Wsdl $context)
  149. {
  150. $this->_context = $context;
  151. return $this;
  152. }
  153. /**
  154. * Create a complex type based on a strategy
  155. *
  156. * @throws Zend_Soap_Wsdl_Exception
  157. * @param string $type
  158. * @return string XSD type
  159. */
  160. public function addComplexType($type)
  161. {
  162. if(!($this->_context instanceof Zend_Soap_Wsdl) ) {
  163. /**
  164. * @see Zend_Soap_Wsdl_Exception
  165. */
  166. require_once "Zend/Soap/Wsdl/Exception.php";
  167. throw new Zend_Soap_Wsdl_Exception(
  168. "Cannot add complex type '".$type."', no context is set for this composite strategy."
  169. );
  170. }
  171. $strategy = $this->getStrategyOfType($type);
  172. $strategy->setContext($this->_context);
  173. return $strategy->addComplexType($type);
  174. }
  175. }