Composite.php 5.2 KB

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