/library/Zend/Soap/Wsdl/ComplexTypeStrategy/DefaultComplexType.php

https://github.com/jtai/zf2 · PHP · 108 lines · 43 code · 19 blank · 46 comment · 5 complexity · f640642344573601e25ed64a1f784489 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-2012 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\ComplexTypeStrategy;
  25. use Zend\Soap;
  26. use Zend\Soap\Exception;
  27. /**
  28. * Zend_Soap_Wsdl_Strategy_DefaultComplexType
  29. *
  30. * @uses ReflectionClass
  31. * @uses \Zend\Soap\WsdlException
  32. * @uses \Zend\Soap\Wsdl\Strategy\AbstractStrategy
  33. * @category Zend
  34. * @package Zend_Soap
  35. * @subpackage WSDL
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class DefaultComplexType extends AbstractComplexTypeStrategy
  40. {
  41. /**
  42. * Add a complex type by recursivly using all the class properties fetched via Reflection.
  43. *
  44. * @param string $type Name of the class to be specified
  45. * @return string XSD Type for the given PHP type
  46. */
  47. public function addComplexType($type)
  48. {
  49. if(!class_exists($type)) {
  50. throw new Exception\InvalidArgumentException(sprintf(
  51. 'Cannot add a complex type %s that is not an object or where '
  52. . 'class could not be found in \'DefaultComplexType\' strategy.', $type
  53. ));
  54. }
  55. if (($soapType = $this->scanRegisteredTypes($type)) !== null) {
  56. return $soapType;
  57. }
  58. $dom = $this->getContext()->toDomDocument();
  59. $class = new \ReflectionClass($type);
  60. $soapTypeName = $this->getContext()->translateType($type);
  61. $soapType = 'tns:' . $soapTypeName;
  62. // Register type here to avoid recursion
  63. $this->getContext()->addType($type, $soapType);
  64. $defaultProperties = $class->getDefaultProperties();
  65. $defaultProperties = $class->getDefaultProperties();
  66. $complexType = $dom->createElement('xsd:complexType');
  67. $complexType->setAttribute('name', $soapTypeName);
  68. $all = $dom->createElement('xsd:all');
  69. foreach ($class->getProperties() as $property) {
  70. if ($property->isPublic() && preg_match_all('/@var\s+([^\s]+)/m', $property->getDocComment(), $matches)) {
  71. /**
  72. * @todo check if 'xsd:element' must be used here (it may not be compatible with using 'complexType'
  73. * node for describing other classes used as attribute types for current class
  74. */
  75. $element = $dom->createElement('xsd:element');
  76. $element->setAttribute('name', $propertyName = $property->getName());
  77. $element->setAttribute('type', $this->getContext()->getType(trim($matches[1][0])));
  78. // If the default value is null, then this property is nillable.
  79. if ($defaultProperties[$propertyName] === null) {
  80. $element->setAttribute('nillable', 'true');
  81. }
  82. $all->appendChild($element);
  83. }
  84. }
  85. $complexType->appendChild($all);
  86. $this->getContext()->getSchema()->appendChild($complexType);
  87. return $soapType;
  88. }
  89. }