title = $title; $feed->description = $description; $feed->link = $link; return $feed; } public function addItem(array $item): bool { foreach ($this->requiredProductFields as $field) { if (!isset($item[$field])) { throw new \Exception("Required field '{$field}' is missing"); } } foreach ($item as $key => $value) { $product[$key] = $value; } $this->products[] = $product; return true; } public function generate() { $this->xml = [ 'rss' => [ '_attributes' => [ 'xmlns:g' => 'http://base.google.com/ns/1.0', 'version' => '2.0', ], 'channel' => [ 'title' => $this->title, 'description' => $this->description, 'link' => $this->link, ] ] ]; foreach ($this->products as $key => $product) { $this->xml['rss']['channel']['item_'.$key] = $product; } $xml = ArrayToXml::convert($this->xml, ''); $xml = str_replace([' ', '', '', "\n", "\r", 'remove'], '', $xml); $xml = preg_replace([ "/item_[0-9][0-9][0-9][0-9]/", "/item_[0-9][0-9][0-9]/", "/item_[0-9][0-9]/", "/item_[0-9]/", ], "item", $xml); return response($xml)->header('Content-Type', 'text/xml'); } }