public function testComplexEncode() { $data = new FormatterData(); $data->setData(["foo" => "bar", ["name" => "banana", "type" => "fruit"], ["orange" => ["type" => "fruit"], "banana" => ["like" => "Yes"]]]); $dom = new \DOMDocument("1.0"); $base = $dom->appendChild(new \DOMElement($this->formatter->root)); /** @var \DOMElement $node */ $node = $base->appendChild(new \DOMElement($this->formatter->nodeName)); $node->setAttribute("name", "foo"); $node->setAttribute("value", "bar"); $baz = $base->appendChild(new \DOMElement($this->formatter->rowName)); $node = $baz->appendChild(new \DOMElement($this->formatter->nodeName)); $node->setAttribute("name", "name"); $node->setAttribute("value", "banana"); $node = $baz->appendChild(new \DOMElement($this->formatter->nodeName)); $node->setAttribute("name", "type"); $node->setAttribute("value", "fruit"); $baz = $base->appendChild(new \DOMElement($this->formatter->rowName)); $orange = $baz->appendChild(new \DOMElement("orange")); $node = $orange->appendChild(new \DOMElement($this->formatter->nodeName)); $node->setAttribute("name", "type"); $node->setAttribute("value", "fruit"); $banana = $baz->appendChild(new \DOMElement("banana")); $node = $banana->appendChild(new \DOMElement($this->formatter->nodeName)); $node->setAttribute("name", "like"); $node->setAttribute("value", "Yes"); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $this->assertEquals($dom->saveXML(), $this->formatter->encode($data)); }
/** * That's why an alias MUST not be the same as a present value */ public function testSetRawDataMultipleDepthWithReverseAliasesFail() { $aliases = ["orange" => "cherry", "blackberry" => "banana"]; $formatterData = new FormatterData($aliases); $data = ["orange" => "banana", "apple" => "pear", ["orange" => "tomato", "pepper" => "pear"], [["strawberry" => "raspberry", "blackberry" => "cranberry"], ["cherry" => "lemon", "mango" => "cranberry"]]]; $formattedData = $formatterData->setData($data)->getDataReverseAliases(); $this->assertNotEquals($data, $formattedData); }
protected function generateExport() { $orderIdList = $this->getRequest()->request->get("order-selection"); $orderAllInOne = $this->getRequest()->request->get("order-all-in-one"); $shippingDate = $this->getRequest()->request->get("export-date", null); $filename = 'tnt-export'; $dataOrders = []; foreach ($orderIdList as $orderId) { if (!array_key_exists($orderId, $dataOrders)) { $dataOrders[$orderId] = [TNTFranceExportEvent::ALL_IN_ONE_LBL => TNTFranceExportEvent::ALL_IN_ONE]; } if (array_key_exists($orderId, $orderAllInOne)) { $dataOrders[$orderId][TNTFranceExportEvent::ALL_IN_ONE_LBL] = $orderAllInOne[$orderId]; //If there is a package for each order product if ($orderAllInOne[$orderId] != TNTFranceExportEvent::ALL_IN_ONE) { if (null != ($orderProductIds = $this->getRequest()->request->get("order-product-selection[" . $orderId . "]", null))) { $dataOrders[$orderId][TNTFranceExportEvent::ORDER_PRODUCTS_LBL] = []; foreach ($orderProductIds as $orderProductId) { $dataOrders[$orderId][TNTFranceExportEvent::ORDER_PRODUCTS_LBL][] = $orderProductId; } } } } } $event = new TNTFranceExportEvent($dataOrders); $event->setShippingDate($shippingDate); $this->dispatch(TNTFranceEvents::TNT_FRANCE_EXPORT, $event); /** * Get needed services */ $formatterManager = $this->getFormatterManager($this->container); try { /** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */ $exportFormat = $this->getRequest()->request->get("export-format", "CSV"); if (!in_array($exportFormat, ["CSV", "Json", "XML"])) { $exportFormat = "CSV"; } $formatter = $formatterManager->get($exportFormat); $formatterData = new FormatterData(); $data = $event->getData(); if ("CSV" == $exportFormat) { $this->flattenArrayRow($data); } $formatterData->setData($data); $formattedContent = $formatter->encode($formatterData); return new Response($formattedContent, 200, ["Content-Type" => $formatter->getMimeType(), "Content-Disposition" => "attachment; filename=\"" . $filename . "." . strtolower($exportFormat) . "\""]); } catch (\Exception $e) { throw $e; } return $this->pageNotFound(); }
/** * @param \Thelia\Model\Lang $lang * @return \Thelia\Core\FileFormat\Formatting\FormatterData * * The method builds the FormatterData for the formatter */ public function buildData(Lang $lang) { $data = new FormatterData($this->getAliases()); $query = $this->buildDataSet($lang); if ($query instanceof ModelCriteria) { return $data->loadModelCriteria($query); } elseif (is_array($query)) { return $data->setData($query); } elseif ($query instanceof BaseLoop) { $pagination = null; $results = $query->exec($pagination); for ($results->rewind(); $results->valid(); $results->next()) { $current = $results->current(); $data->addRow($current->getVarVal()); } return $data; } throw new InvalidValueException(Translator::getInstance()->trans("The method \"%class\"::buildDataSet must return an array or a ModelCriteria", ["%class" => get_class($this)])); }