Composite.php 5.1 KB

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