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

https://github.com/leerbag/zf2 · PHP · 141 lines · 59 code · 20 blank · 62 comment · 8 complexity · d0d002491ec7842a66367e323d85591f 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. * @subpackage WSDL
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Soap\Wsdl\Strategy;
  25. use Zend\Soap;
  26. use Zend\Soap\Wsdl;
  27. use Zend\Soap\Exception;
  28. /**
  29. * ArrayOfTypeComplex strategy
  30. *
  31. * @uses \Zend\Soap\Wsdl\Exception
  32. * @uses \Zend\Soap\Wsdl\Strategy\DefaultComplexType
  33. * @category Zend
  34. * @package Zend_Soap
  35. * @subpackage WSDL
  36. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class ArrayOfTypeComplex extends DefaultComplexType
  40. {
  41. /**
  42. * Add an ArrayOfType based on the xsd:complexType syntax if type[] is detected in return value doc comment.
  43. *
  44. * @param string $type
  45. * @return string tns:xsd-type
  46. */
  47. public function addComplexType($type)
  48. {
  49. if (($soapType = $this->scanRegisteredTypes($type)) !== null) {
  50. return $soapType;
  51. }
  52. $singularType = $this->_getSingularPhpType($type);
  53. $nestingLevel = $this->_getNestedCount($type);
  54. if($nestingLevel == 0) {
  55. return parent::addComplexType($singularType);
  56. } else if($nestingLevel == 1) {
  57. // The following blocks define the Array of Object structure
  58. return $this->_addArrayOfComplexType($singularType, $type);
  59. } else {
  60. throw new Exception\InvalidArgumentException(
  61. 'ArrayOfTypeComplex cannot return nested ArrayOfObject deeper than '
  62. . 'one level. Use array object properties to return deep nested data.'
  63. );
  64. }
  65. }
  66. /**
  67. * Add an ArrayOfType based on the xsd:complexType syntax if type[] is detected in return value doc comment.
  68. *
  69. * @param string $singularType e.g. '\MyNamespace\MyClassname'
  70. * @param string $type e.g. '\MyNamespace\MyClassname[]'
  71. * @return string tns:xsd-type e.g. 'tns:ArrayOfMyNamespace.MyClassname'
  72. */
  73. protected function _addArrayOfComplexType($singularType, $type)
  74. {
  75. if (($soapType = $this->scanRegisteredTypes($type)) !== null) {
  76. return $soapType;
  77. }
  78. $xsdComplexTypeName = 'ArrayOf' . Wsdl::translateType($singularType);
  79. $xsdComplexType = 'tns:' . $xsdComplexTypeName;
  80. // Register type here to avoid recursion
  81. $this->getContext()->addType($type, $xsdComplexType);
  82. // Process singular type using DefaultComplexType strategy
  83. parent::addComplexType($singularType);
  84. // Add array type structure to WSDL document
  85. $dom = $this->getContext()->toDomDocument();
  86. $complexType = $dom->createElement('xsd:complexType');
  87. $complexType->setAttribute('name', $xsdComplexTypeName);
  88. $complexContent = $dom->createElement('xsd:complexContent');
  89. $complexType->appendChild($complexContent);
  90. $xsdRestriction = $dom->createElement('xsd:restriction');
  91. $xsdRestriction->setAttribute('base', 'soap-enc:Array');
  92. $complexContent->appendChild($xsdRestriction);
  93. $xsdAttribute = $dom->createElement('xsd:attribute');
  94. $xsdAttribute->setAttribute('ref', 'soap-enc:arrayType');
  95. $xsdAttribute->setAttribute('wsdl:arrayType',
  96. 'tns:' . Wsdl::translateType($singularType) . '[]');
  97. $xsdRestriction->appendChild($xsdAttribute);
  98. $this->getContext()->getSchema()->appendChild($complexType);
  99. return $xsdComplexType;
  100. }
  101. /**
  102. * From a nested definition with type[], get the singular PHP Type
  103. *
  104. * @param string $type
  105. * @return string
  106. */
  107. protected function _getSingularPhpType($type)
  108. {
  109. return str_replace('[]', '', $type);
  110. }
  111. /**
  112. * Return the array nesting level based on the type name
  113. *
  114. * @param string $type
  115. * @return integer
  116. */
  117. protected function _getNestedCount($type)
  118. {
  119. return substr_count($type, '[]');
  120. }
  121. }