KeyValue.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Sabre\VObject\Parser\XML\Element;
  3. use Sabre\Xml as SabreXml;
  4. /**
  5. * Our own sabre/xml key-value element.
  6. *
  7. * It just removes the clark notation.
  8. *
  9. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  10. * @author Ivan Enderlin
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. class KeyValue extends SabreXml\Element\KeyValue
  14. {
  15. /**
  16. * The deserialize method is called during xml parsing.
  17. *
  18. * This method is called statically, this is because in theory this method
  19. * may be used as a type of constructor, or factory method.
  20. *
  21. * Often you want to return an instance of the current class, but you are
  22. * free to return other data as well.
  23. *
  24. * Important note 2: You are responsible for advancing the reader to the
  25. * next element. Not doing anything will result in a never-ending loop.
  26. *
  27. * If you just want to skip parsing for this element altogether, you can
  28. * just call $reader->next();
  29. *
  30. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  31. * the next element.
  32. *
  33. * @param XML\Reader $reader
  34. */
  35. public static function xmlDeserialize(SabreXml\Reader $reader): array
  36. {
  37. // If there's no children, we don't do anything.
  38. if ($reader->isEmptyElement) {
  39. $reader->next();
  40. return [];
  41. }
  42. $values = [];
  43. $reader->read();
  44. do {
  45. if (SabreXml\Reader::ELEMENT === $reader->nodeType) {
  46. $name = $reader->localName;
  47. $values[$name] = $reader->parseCurrentElement()['value'];
  48. } else {
  49. $reader->read();
  50. }
  51. } while (SabreXml\Reader::END_ELEMENT !== $reader->nodeType);
  52. $reader->read();
  53. return $values;
  54. }
  55. }