TaggingConfig.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace OSS\Model;
  3. use OSS\Core\OssException;
  4. /**
  5. * Class TaggingConfig
  6. * @package OSS\Model
  7. *
  8. */
  9. class TaggingConfig implements XmlConfig
  10. {
  11. /**
  12. * TaggingConfig constructor.
  13. */
  14. public function __construct()
  15. {
  16. $this->tags = array();
  17. }
  18. /**
  19. * Get Tag list
  20. *
  21. * @return Tag[]
  22. */
  23. public function getTags()
  24. {
  25. return $this->tags;
  26. }
  27. /**
  28. * Add a new Tag
  29. *
  30. * @param Tag $tag
  31. * @throws OssException
  32. */
  33. public function addTag($tag)
  34. {
  35. $this->tags[] = $tag;
  36. }
  37. /**
  38. * Parse TaggingConfig from the xml.
  39. *
  40. * @param string $strXml
  41. * @throws OssException
  42. * @return null
  43. */
  44. public function parseFromXml($strXml)
  45. {
  46. $xml = simplexml_load_string($strXml);
  47. if (!isset($xml->TagSet) || !isset($xml->TagSet->Tag)) return;
  48. foreach ($xml->TagSet->Tag as $tag) {
  49. $this->addTag(new Tag($tag->Key, $tag->Value));
  50. }
  51. }
  52. /**
  53. * Serialize the object into xml string.
  54. *
  55. * @return string
  56. */
  57. public function serializeToXml()
  58. {
  59. $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><Tagging></Tagging>');
  60. $xmlTagSet = $xml->addChild('TagSet');
  61. foreach ($this->tags as $tag) {
  62. $xmlTag = $xmlTagSet->addChild('Tag');
  63. $xmlTag->addChild('Key', strval($tag->getKey()));
  64. $xmlTag->addChild('Value', strval($tag->getValue()));
  65. }
  66. return $xml->asXML();
  67. }
  68. public function __toString()
  69. {
  70. return $this->serializeToXml();
  71. }
  72. /**
  73. * Tag list
  74. *
  75. * @var Tag[]
  76. */
  77. private $tags = array();
  78. }