1<?php
2
3namespace FINDOLOGIC\Export\CSV;
4
5use FINDOLOGIC\Export\Data\Attribute;
6use FINDOLOGIC\Export\Data\Item;
7
8class CSVItem extends Item
9{
10    /**
11     * @inheritdoc
12     */
13    public function getDomSubtree(\DOMDocument $document)
14    {
15        throw new \BadMethodCallException('CSVItem does not implement XML export.');
16    }
17
18    /**
19     * @inheritdoc
20     */
21    public function getCsvFragment()
22    {
23        $ordernumbers = $this->ordernumbers->getCsvFragment();
24        $name = $this->sanitize($this->name->getCsvFragment());
25        $summary = $this->sanitize($this->summary->getCsvFragment());
26        $description = $this->sanitize($this->description->getCsvFragment());
27        $price = $this->price->getCsvFragment();
28        $url = $this->sanitize($this->url->getCsvFragment());
29        $keywords = $this->sanitize($this->keywords->getCsvFragment());
30        $bonus = $this->sanitize($this->bonus->getCsvFragment());
31        $salesFrequency = $this->sanitize($this->salesFrequency->getCsvFragment());
32        $dateAdded = $this->sanitize($this->dateAdded->getCsvFragment());
33        $sort = $this->sanitize($this->sort->getCsvFragment());
34
35        $instead = ''; // TODO
36        $maxPrice = ''; // TODO
37        $taxRate = ''; // TODO
38        $groups = ''; // TODO
39
40
41        $image = $this->buildImages();
42        $attributes = $this->buildAttributes();
43        $properties = $this->buildProperties();
44
45        $line = sprintf(
46            "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
47            $this->id,
48            $ordernumbers,
49            $name,
50            $summary,
51            $description,
52            $price,
53            $instead,
54            $maxPrice,
55            $taxRate,
56            $url,
57            $image,
58            $attributes,
59            $keywords,
60            $groups,
61            $bonus,
62            $salesFrequency,
63            $dateAdded,
64            $sort,
65            $properties
66        );
67
68        return $line;
69    }
70
71    private function buildProperties()
72    {
73        // TODO
74
75        return '';
76    }
77
78    private function buildAttributes()
79    {
80        $attributes = '';
81
82        /** @var Attribute $attribute */
83        foreach ($this->attributes as $attribute) {
84            $attributes .= sprintf('%s&', $attribute->getCsvFragment());
85        }
86
87        $attributes = rtrim($attributes, '&');
88
89        return $attributes;
90    }
91
92    private function buildImages()
93    {
94        // Use the first available image that is not restricted by usergroup.
95        if (array_key_exists('', $this->images) && count($this->images['']) > 0) {
96            $imageUrl = $this->images[''][0];
97        } else {
98            $imageUrl = '';
99        }
100
101        return $imageUrl;
102    }
103
104    private function sanitize($input, $stripTags = true)
105    {
106        if ($stripTags) {
107            $input = strip_tags($input);
108        }
109
110        $sanitized = preg_replace('/\t/', ' ', $input);
111
112        return $sanitized;
113    }
114}
115