PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Soap/Wsdl/Strategy/ArrayOfTypeSequence.php

https://github.com/Exercise/zf2
PHP | 150 lines | 60 code | 20 blank | 70 comment | 9 complexity | 3d488ee29ff0403019bec863afcc3f02 MD5 | raw file
  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. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Soap\Wsdl\Strategy;
  25. use Zend\Soap\Wsdl;
  26. /**
  27. * Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence
  28. *
  29. * @uses \Zend\Soap\Wsdl\Strategy\DefaultComplexType
  30. * @category Zend
  31. * @package Zend_Soap
  32. * @subpackage WSDL
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class ArrayOfTypeSequence extends DefaultComplexType
  37. {
  38. /**
  39. * Add an unbounded ArrayOfType based on the xsd:sequence syntax if type[] is detected in return value doc comment.
  40. *
  41. * @param string $type
  42. * @return string tns:xsd-type
  43. */
  44. public function addComplexType($type)
  45. {
  46. $nestedCounter = $this->_getNestedCount($type);
  47. if($nestedCounter > 0) {
  48. $singularType = $this->_getSingularType($type);
  49. for($i = 1; $i <= $nestedCounter; $i++) {
  50. $complexType = $this->_getTypeBasedOnNestingLevel($singularType, $i);
  51. $complexTypePhp = $singularType . str_repeat('[]', $i);
  52. $childType = $this->_getTypeBasedOnNestingLevel($singularType, $i-1);
  53. $this->_addSequenceType($complexType, $childType, $complexTypePhp);
  54. }
  55. return $complexType;
  56. } else if (($soapType = $this->scanRegisteredTypes($type)) !== null) {
  57. // Existing complex type
  58. return $soapType;
  59. } else {
  60. // New singular complex type
  61. return parent::addComplexType($type);
  62. }
  63. }
  64. /**
  65. * Return the ArrayOf or simple type name based on the singular xsdtype and the nesting level
  66. *
  67. * @param string $singularType
  68. * @param int $level
  69. * @return string
  70. */
  71. protected function _getTypeBasedOnNestingLevel($singularType, $level)
  72. {
  73. if($level == 0) {
  74. // This is not an Array anymore, return the xsd simple type
  75. return $this->getContext()->getType($singularType);
  76. } else {
  77. return 'tns:' . str_repeat('ArrayOf', $level) . ucfirst(Wsdl::translateType($singularType));
  78. }
  79. }
  80. /**
  81. * From a nested defintion with type[], get the singular xsd:type
  82. *
  83. * @throws \Zend\Soap\WsdlException When no xsd:simpletype can be detected.
  84. * @param string $type
  85. * @return string
  86. */
  87. protected function _getSingularType($type)
  88. {
  89. return str_replace('[]', '', $type);
  90. }
  91. /**
  92. * Return the array nesting level based on the type name
  93. *
  94. * @param string $type
  95. * @return integer
  96. */
  97. protected function _getNestedCount($type)
  98. {
  99. return substr_count($type, '[]');
  100. }
  101. /**
  102. * Append the complex type definition to the WSDL via the context access
  103. *
  104. * @param string $arrayType Array type name (e.g. 'tns:ArrayOfArrayOfInt')
  105. * @param string $childType Qualified array items type (e.g. 'xsd:int', 'tns:ArrayOfInt')
  106. * @param string $phpArrayType PHP type (e.g. 'int[][]', '\MyNamespace\MyClassName[][][]')
  107. * @return void
  108. */
  109. protected function _addSequenceType($arrayType, $childType, $phpArrayType)
  110. {
  111. if (($soapType = $this->scanRegisteredTypes($phpArrayType)) !== null) {
  112. return;
  113. }
  114. // Register type here to avoid recursion
  115. $this->getContext()->addType($phpArrayType, $arrayType);
  116. $dom = $this->getContext()->toDomDocument();
  117. $arrayTypeName = substr($arrayType, strpos($arrayType, ':') + 1);
  118. $complexType = $dom->createElement('xsd:complexType');
  119. $complexType->setAttribute('name', $arrayTypeName);
  120. $sequence = $dom->createElement('xsd:sequence');
  121. $element = $dom->createElement('xsd:element');
  122. $element->setAttribute('name', 'item');
  123. $element->setAttribute('type', $childType);
  124. $element->setAttribute('minOccurs', 0);
  125. $element->setAttribute('maxOccurs', 'unbounded');
  126. $sequence->appendChild($element);
  127. $complexType->appendChild($sequence);
  128. $this->getContext()->getSchema()->appendChild($complexType);
  129. }
  130. }