1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace corpsepk\yml\helpers; |
4
|
|
|
|
5
|
|
|
use XMLWriter; |
6
|
|
|
use yii\helpers\Html; |
7
|
|
|
use corpsepk\yml\models\Offer; |
8
|
|
|
|
9
|
|
|
class XMLWriterHelper |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param Offer $offer |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
public function renderOffer($offer) |
16
|
|
|
{ |
17
|
|
|
$writer = new XMLWriter(); |
18
|
|
|
$writer->openMemory(); |
19
|
|
|
|
20
|
|
|
$writer->startElement('offer'); |
21
|
|
|
|
22
|
|
|
foreach ($offer->offerElementAttributes as $attribute) { |
23
|
|
|
if (!empty($offer->$attribute)) { |
24
|
|
|
$writer->writeAttribute($attribute, Html::encode($offer->$attribute)); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
foreach ($offer->customElements as $attribute) { |
29
|
|
|
if (!is_array($attribute)) { |
30
|
|
|
continue; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
foreach ($attribute as $attrName => $attrValue) { |
34
|
|
|
if (is_array($attrValue)) { |
35
|
|
|
$writer->startElement($attrName); |
36
|
|
|
foreach ($attrValue as $name => $value) { |
37
|
|
|
$writer->writeElement($name, Html::encode($value)); |
38
|
|
|
} |
39
|
|
|
$writer->endElement(); |
40
|
|
|
} else { |
41
|
|
|
$writer->startElement($attrName); |
42
|
|
|
$writer->writeRaw($attrValue); |
43
|
|
|
$writer->endElement(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
foreach ($offer->getOfferElements() as $attribute) { |
|
|
|
|
49
|
|
|
if (empty($offer->$attribute)) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (is_array($offer->$attribute)) { |
54
|
|
|
foreach ($offer->$attribute as $value) { |
55
|
|
|
$writer->writeElement($attribute, Html::encode($value)); |
56
|
|
|
} |
57
|
|
|
} else { |
58
|
|
|
$writer->writeElement($attribute, Html::encode($offer->$attribute)); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (is_array($offer->param)) { |
63
|
|
|
foreach ($offer->param as $param) { |
64
|
|
|
$writer->startElement('param'); |
65
|
|
|
$writer->writeAttribute('name', $param['name']); |
66
|
|
|
$writer->text($param['value']); |
67
|
|
|
$writer->endElement(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$writer->endElement(); |
72
|
|
|
|
73
|
|
|
return $writer->outputMemory(); |
74
|
|
|
} |
75
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.