| 1: | <?php |
| 2: | |
| 3: | declare(strict_types = 1); |
| 4: | |
| 5: | namespace CloudCastle\XmlGenerator; |
| 6: | |
| 7: | use \XMLWriter; |
| 8: | Use CloudCastle\FileSystem\Dir; |
| 9: | use CloudCastle\FileSystem\File; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | class XmlGenerator |
| 20: | { |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | private ?XMLWriter $obj = null; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | private ?string $file = null; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | private ?Config $config; |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | public function __construct(Config $config) |
| 45: | { |
| 46: | $this->obj = new XMLWriter(); |
| 47: | $this->config = $config; |
| 48: | if ($this->config->getType() === 'filesystem') { |
| 49: | $this->setFileSystemGenerator(); |
| 50: | } else { |
| 51: | $this->setMemoryGenerator(); |
| 52: | } |
| 53: | } |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | private function setFileSystemGenerator(): void |
| 60: | { |
| 61: | $this->file = $this->config->getFile(); |
| 62: | if (Dir::create(dirname($this->file))) { |
| 63: | $this->obj->openUri($this->file); |
| 64: | } else { |
| 65: | $this->setMemoryGenerator(); |
| 66: | } |
| 67: | } |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | private function setMemoryGenerator(): void |
| 74: | { |
| 75: | $this->obj->openMemory(); |
| 76: | } |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | public function startDocument(string $version = '1.0', string $encoding = 'utf-8'): self |
| 85: | { |
| 86: | $this->obj->startDocument($version, $encoding); |
| 87: | return $this; |
| 88: | } |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | public function startElement(string $name, array $attribites = [], ?string $comment = null): self |
| 98: | { |
| 99: | if ($comment) { |
| 100: | $this->obj->startComment(); |
| 101: | $this->obj->text($comment); |
| 102: | $this->obj->endComment(); |
| 103: | } |
| 104: | $this->obj->startElement($name); |
| 105: | if ($attribites) { |
| 106: | foreach ($attribites AS $key => $value) { |
| 107: | $this->addAttribute($key, $value); |
| 108: | } |
| 109: | } |
| 110: | return $this; |
| 111: | } |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | public function closeElement(): self |
| 118: | { |
| 119: | $this->obj->endElement(); |
| 120: | return $this; |
| 121: | } |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | public function get(): Xml |
| 128: | { |
| 129: | $this->obj->endDocument(); |
| 130: | $response = new Xml(); |
| 131: | $response->file = $this->file; |
| 132: | if ($this->config->getType() === 'filesystem') { |
| 133: | $response->structure = File::read($this->file); |
| 134: | } else { |
| 135: | $response->structure = $this->obj->outputMemory($this->config->getFlush()); |
| 136: | } |
| 137: | return $response; |
| 138: | } |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | public function addAttribute(string $name, $text): self |
| 147: | { |
| 148: | if ($name AND $text) { |
| 149: | $this->obj->startAttribute($name); |
| 150: | $this->obj->text((string)$text); |
| 151: | $this->obj->endAttribute(); |
| 152: | } |
| 153: | return $this; |
| 154: | } |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | public function addElement(string $name, $content = false, array $attribites = [], ?string $comment = null): self |
| 165: | { |
| 166: | if ($name AND $content) { |
| 167: | $this->startElement($name, $attribites, $comment); |
| 168: | $this->obj->text((string)$content); |
| 169: | $this->closeElement(); |
| 170: | } |
| 171: | return $this; |
| 172: | } |
| 173: | |
| 174: | } |
| 175: | |