1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace carono\exchange1c\helpers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use carono\exchange1c\interfaces\DocumentInterface; |
8
|
|
|
use carono\exchange1c\interfaces\OfferInterface; |
9
|
|
|
use carono\exchange1c\interfaces\PartnerInterface; |
10
|
|
|
|
11
|
|
|
class SerializeHelper |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param PartnerInterface $partner |
15
|
|
|
* @return \SimpleXMLElement |
16
|
|
|
*/ |
17
|
|
|
public static function serializePartner(PartnerInterface $partner) |
18
|
|
|
{ |
19
|
|
|
$xml = new \SimpleXMLElement('<Контрагент></Контрагент>'); |
20
|
|
|
self::addFields($xml, $partner, $partner->getExportFields1c()); |
21
|
|
|
return $xml; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param OfferInterface $offer |
26
|
|
|
* @param DocumentInterface $document |
27
|
|
|
* @return \SimpleXMLElement |
28
|
|
|
*/ |
29
|
|
|
public static function serializeOffer(OfferInterface $offer, DocumentInterface $document) |
30
|
|
|
{ |
31
|
|
|
$productNode = new \SimpleXMLElement('<Товар></Товар>'); |
32
|
|
|
self::addFields($productNode, $offer, $offer->getExportFields1c($document)); |
33
|
|
|
if ($group = $offer->getGroup1c()) { |
34
|
|
|
$productNode->addChild('ИдКаталога', $group->{$group->getIdFieldName1c()}); |
35
|
|
|
} |
36
|
|
|
return $productNode; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param DocumentInterface $document |
41
|
|
|
* @return \SimpleXMLElement |
42
|
|
|
*/ |
43
|
|
|
public static function serializeDocument(DocumentInterface $document) |
44
|
|
|
{ |
45
|
|
|
$documentNode = new \SimpleXMLElement('<Документ></Документ>'); |
46
|
|
|
self::addFields($documentNode, $document, $document->getExportFields1c()); |
47
|
|
|
$partnersNode = $documentNode->addChild('Контрагенты'); |
48
|
|
|
$partner = $document->getPartner1c(); |
49
|
|
|
$partnerNode = self::serializePartner($partner); |
50
|
|
|
NodeHelper::appendNode($partnersNode, $partnerNode); |
51
|
|
|
$products = $documentNode->addChild('Товары'); |
52
|
|
|
foreach ($document->getOffers1c() as $offer) { |
53
|
|
|
$productNode = self::serializeOffer($offer, $document); |
54
|
|
|
NodeHelper::appendNode($products, $productNode); |
55
|
|
|
} |
56
|
|
|
return $documentNode; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $node |
61
|
|
|
* @param $object |
62
|
|
|
* @param $fields |
63
|
|
|
*/ |
64
|
|
|
public static function addFields($node, $object, $fields) |
65
|
|
|
{ |
66
|
|
|
foreach ($fields as $field => $value) { |
67
|
|
|
NodeHelper::addChild($node, $object, $field, $value); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |