|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WsdlToPhp\DomHandler; |
| 4 | + |
| 5 | +class AbstractAttributeHandler extends AbstractNodeHandler |
| 6 | +{ |
| 7 | + /** |
| 8 | + * @var string |
| 9 | + */ |
| 10 | + const DEFAULT_VALUE_TYPE = 'string'; |
| 11 | + /** |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + const ATTRIBUTE_NAMESPACE = 'namespace'; |
| 15 | + /** |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + const ATTRIBUTE_NAME = 'name'; |
| 19 | + /** |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + const ATTRIBUTE_REF = 'ref'; |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + const ATTRIBUTE_VALUE = 'value'; |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + const ATTRIBUTE_TYPE = 'type'; |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + const ATTRIBUTE_ABSTRACT = 'abstract'; |
| 35 | + /** |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + const ATTRIBUTE_MAX_OCCURS = 'maxOccurs'; |
| 39 | + /** |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + const ATTRIBUTE_MIN_OCCURS = 'minOccurs'; |
| 43 | + /** |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + const ATTRIBUTE_NILLABLE = 'nillable'; |
| 47 | + /** |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + const VALUE_UNBOUNDED = 'unbounded'; |
| 51 | + /** |
| 52 | + * @var string |
| 53 | + */ |
| 54 | + const DEFAULT_OCCURENCE_VALUE = 1; |
| 55 | + /** |
| 56 | + * @see \WsdlToPhp\DomHandler\AbstractNodeHandler::getNode() |
| 57 | + * @return \DOMAttr |
| 58 | + */ |
| 59 | + public function getNode() |
| 60 | + { |
| 61 | + return parent::getNode(); |
| 62 | + } |
| 63 | + /** |
| 64 | + * @return \DOMAttr |
| 65 | + */ |
| 66 | + public function getAttribute() |
| 67 | + { |
| 68 | + return $this->getNode(); |
| 69 | + } |
| 70 | + /** |
| 71 | + * Tries to get attribute type on the same node |
| 72 | + * in order to return the value of the attribute in its type |
| 73 | + * @return string|null |
| 74 | + */ |
| 75 | + public function getType() |
| 76 | + { |
| 77 | + $type = null; |
| 78 | + if (($parent = $this->getParent()) instanceof ElementHandler && $parent->hasAttribute(self::ATTRIBUTE_TYPE)) { |
| 79 | + $type = $parent->getAttribute(self::ATTRIBUTE_TYPE)->getValue(false, false); |
| 80 | + } |
| 81 | + return $type; |
| 82 | + } |
| 83 | + /** |
| 84 | + * @param bool $withNamespace |
| 85 | + * @param bool $withinItsType |
| 86 | + * @param string $asType |
| 87 | + * @return mixed |
| 88 | + */ |
| 89 | + public function getValue($withNamespace = false, $withinItsType = true, $asType = self::DEFAULT_VALUE_TYPE) |
| 90 | + { |
| 91 | + $value = $this->getAttribute()->value; |
| 92 | + if ($withNamespace === false && !empty($value)) { |
| 93 | + $value = implode('', array_slice(explode(':', $value), -1, 1)); |
| 94 | + } |
| 95 | + if ($value !== null && $withinItsType === true) { |
| 96 | + $value = self::getValueWithinItsType($value, empty($asType) ? $this->getType() : $asType); |
| 97 | + } |
| 98 | + return $value; |
| 99 | + } |
| 100 | + /** |
| 101 | + * @return null|string |
| 102 | + */ |
| 103 | + public function getValueNamespace() |
| 104 | + { |
| 105 | + $value = $this->getAttribute()->value; |
| 106 | + $namespace = null; |
| 107 | + if (strpos($value, ':') !== false) { |
| 108 | + $namespace = implode('', array_slice(explode(':', $value), 0, -1)); |
| 109 | + } |
| 110 | + return $namespace; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns the value with good type |
| 115 | + * @param mixed $value the value |
| 116 | + * @param string $knownType the value |
| 117 | + * @return mixed |
| 118 | + */ |
| 119 | + public static function getValueWithinItsType($value, $knownType = null) |
| 120 | + { |
| 121 | + if (is_int($value) || (!is_null($value) && in_array($knownType, array( |
| 122 | + 'time', |
| 123 | + 'positiveInteger', |
| 124 | + 'unsignedLong', |
| 125 | + 'unsignedInt', |
| 126 | + 'short', |
| 127 | + 'long', |
| 128 | + 'int', |
| 129 | + 'integer', |
| 130 | + ), true))) { |
| 131 | + return intval($value); |
| 132 | + } elseif (is_float($value) || (!is_null($value) && in_array($knownType, array( |
| 133 | + 'float', |
| 134 | + 'double', |
| 135 | + 'decimal', |
| 136 | + ), true))) { |
| 137 | + return floatval($value); |
| 138 | + } elseif (is_bool($value) || (!is_null($value) && in_array($knownType, array( |
| 139 | + 'bool', |
| 140 | + 'boolean', |
| 141 | + ), true))) { |
| 142 | + return ($value === 'true' || $value === true || $value === 1 || $value === '1'); |
| 143 | + } |
| 144 | + return $value; |
| 145 | + } |
| 146 | +} |
0 commit comments