functions.php
208 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Sabre\Xml\Serializer; |
| 6 | |
| 7 | use Sabre\Xml\Writer; |
| 8 | use Sabre\Xml\XmlSerializable; |
| 9 | |
| 10 | /** |
| 11 | * This file provides a number of 'serializer' helper functions. |
| 12 | * |
| 13 | * These helper functions can be used to easily xml-encode common PHP |
| 14 | * data structures, or can be placed in the $classMap. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * The 'enum' serializer writes simple list of elements. |
| 19 | * |
| 20 | * For example, calling: |
| 21 | * |
| 22 | * enum($writer, [ |
| 23 | * "{http://sabredav.org/ns}elem1", |
| 24 | * "{http://sabredav.org/ns}elem2", |
| 25 | * "{http://sabredav.org/ns}elem3", |
| 26 | * "{http://sabredav.org/ns}elem4", |
| 27 | * "{http://sabredav.org/ns}elem5", |
| 28 | * ]); |
| 29 | * |
| 30 | * Will generate something like this (if the correct namespace is declared): |
| 31 | * |
| 32 | * <s:elem1 /> |
| 33 | * <s:elem2 /> |
| 34 | * <s:elem3 /> |
| 35 | * <s:elem4>content</s:elem4> |
| 36 | * <s:elem5 attr="val" /> |
| 37 | * |
| 38 | * @param string[] $values |
| 39 | */ |
| 40 | function enum(Writer $writer, array $values): void |
| 41 | { |
| 42 | foreach ($values as $value) { |
| 43 | $writer->writeElement($value); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * The valueObject serializer turns a simple PHP object into a classname. |
| 49 | * |
| 50 | * Every public property will be encoded as an XML element with the same |
| 51 | * name, in the XML namespace as specified. |
| 52 | * |
| 53 | * Values that are set to null or an empty array are not serialized. To |
| 54 | * serialize empty properties, you must specify them as an empty string. |
| 55 | */ |
| 56 | function valueObject(Writer $writer, object $valueObject, string $namespace): void |
| 57 | { |
| 58 | foreach (get_object_vars($valueObject) as $key => $val) { |
| 59 | if (is_array($val)) { |
| 60 | // If $val is an array, it has a special meaning. We need to |
| 61 | // generate one child element for each item in $val |
| 62 | foreach ($val as $child) { |
| 63 | $writer->writeElement('{'.$namespace.'}'.$key, $child); |
| 64 | } |
| 65 | } elseif (null !== $val) { |
| 66 | $writer->writeElement('{'.$namespace.'}'.$key, $val); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * This serializer helps you serialize xml structures that look like |
| 73 | * this:. |
| 74 | * |
| 75 | * <collection> |
| 76 | * <item>...</item> |
| 77 | * <item>...</item> |
| 78 | * <item>...</item> |
| 79 | * </collection> |
| 80 | * |
| 81 | * In that previous example, this serializer just serializes the item element, |
| 82 | * and this could be called like this: |
| 83 | * |
| 84 | * repeatingElements($writer, $items, '{}item'); |
| 85 | * |
| 86 | * @param array<int,mixed> $items |
| 87 | */ |
| 88 | function repeatingElements(Writer $writer, array $items, string $childElementName): void |
| 89 | { |
| 90 | foreach ($items as $item) { |
| 91 | $writer->writeElement($childElementName, $item); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * This function is the 'default' serializer that is able to serialize most |
| 97 | * things, and delegates to other serializers if needed. |
| 98 | * |
| 99 | * The standardSerializer supports a wide-array of values. |
| 100 | * |
| 101 | * $value may be a string or integer, it will just write out the string as text. |
| 102 | * $value may be an instance of XmlSerializable or Element, in which case it |
| 103 | * calls it's xmlSerialize() method. |
| 104 | * $value may be a PHP callback/function/closure, in case we call the callback |
| 105 | * and give it the Writer as an argument. |
| 106 | * $value may be an object, and if it's in the classMap we automatically call |
| 107 | * the correct serializer for it. |
| 108 | * $value may be null, in which case we do nothing. |
| 109 | * |
| 110 | * If $value is an array, the array must look like this: |
| 111 | * |
| 112 | * [ |
| 113 | * [ |
| 114 | * 'name' => '{namespaceUri}element-name', |
| 115 | * 'value' => '...', |
| 116 | * 'attributes' => [ 'attName' => 'attValue' ] |
| 117 | * ] |
| 118 | * [, |
| 119 | * 'name' => '{namespaceUri}element-name2', |
| 120 | * 'value' => '...', |
| 121 | * ] |
| 122 | * ] |
| 123 | * |
| 124 | * This would result in xml like: |
| 125 | * |
| 126 | * <element-name xmlns="namespaceUri" attName="attValue"> |
| 127 | * ... |
| 128 | * </element-name> |
| 129 | * <element-name2> |
| 130 | * ... |
| 131 | * </element-name2> |
| 132 | * |
| 133 | * The value property may be any value standardSerializer supports, so you can |
| 134 | * nest data-structures this way. Both value and attributes are optional. |
| 135 | * |
| 136 | * Alternatively, you can also specify the array using this syntax: |
| 137 | * |
| 138 | * [ |
| 139 | * [ |
| 140 | * '{namespaceUri}element-name' => '...', |
| 141 | * '{namespaceUri}element-name2' => '...', |
| 142 | * ] |
| 143 | * ] |
| 144 | * |
| 145 | * This is excellent for simple key->value structures, and here you can also |
| 146 | * specify anything for the value. |
| 147 | * |
| 148 | * You can even mix the two array syntaxes. |
| 149 | * |
| 150 | * @param string|int|float|bool|array<int|string, mixed>|object $value |
| 151 | */ |
| 152 | function standardSerializer(Writer $writer, $value): void |
| 153 | { |
| 154 | if (is_scalar($value)) { |
| 155 | // String, integer, float, boolean |
| 156 | $writer->text((string) $value); |
| 157 | } elseif ($value instanceof XmlSerializable) { |
| 158 | // XmlSerializable classes or Element classes. |
| 159 | $value->xmlSerialize($writer); |
| 160 | } elseif (is_object($value) && isset($writer->classMap[get_class($value)])) { |
| 161 | // It's an object which class appears in the classmap. |
| 162 | $writer->classMap[get_class($value)]($writer, $value); |
| 163 | } elseif (is_callable($value)) { |
| 164 | // A callback |
| 165 | $value($writer); |
| 166 | } elseif (is_array($value) && array_key_exists('name', $value)) { |
| 167 | // if the array had a 'name' element, we assume that this array |
| 168 | // describes a 'name' and optionally 'attributes' and 'value'. |
| 169 | |
| 170 | $name = $value['name']; |
| 171 | $attributes = isset($value['attributes']) ? $value['attributes'] : []; |
| 172 | $value = isset($value['value']) ? $value['value'] : null; |
| 173 | |
| 174 | $writer->startElement($name); |
| 175 | $writer->writeAttributes($attributes); |
| 176 | $writer->write($value); |
| 177 | $writer->endElement(); |
| 178 | } elseif (is_array($value)) { |
| 179 | foreach ($value as $name => $item) { |
| 180 | if (is_int($name)) { |
| 181 | // This item has a numeric index. We just loop through the |
| 182 | // array and throw it back in the writer. |
| 183 | standardSerializer($writer, $item); |
| 184 | } elseif (is_string($name) && is_array($item) && isset($item['attributes'])) { |
| 185 | // The key is used for a name, but $item has 'attributes' and |
| 186 | // possibly 'value' |
| 187 | $writer->startElement($name); |
| 188 | $writer->writeAttributes($item['attributes']); |
| 189 | if (isset($item['value'])) { |
| 190 | $writer->write($item['value']); |
| 191 | } |
| 192 | $writer->endElement(); |
| 193 | } elseif (is_string($name)) { |
| 194 | // This was a plain key-value array. |
| 195 | $writer->startElement($name); |
| 196 | $writer->write($item); |
| 197 | $writer->endElement(); |
| 198 | } else { |
| 199 | throw new \InvalidArgumentException('The writer does not know how to serialize arrays with keys of type: '.gettype($name)); |
| 200 | } |
| 201 | } |
| 202 | } elseif (is_object($value)) { |
| 203 | throw new \InvalidArgumentException('The writer cannot serialize objects of class: '.get_class($value)); |
| 204 | } elseif (!is_null($value)) { |
| 205 | throw new \InvalidArgumentException('The writer cannot serialize values of type: '.gettype($value)); |
| 206 | } |
| 207 | } |
| 208 |