1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Radowoj\Yaah\Decorators;
|
4
|
|
|
|
5
|
|
|
use Radowoj\Yaah\Exception;
|
6
|
|
|
use Radowoj\Yaah\AuctionInterface;
|
7
|
|
|
use Radowoj\Yaah\Constants\AuctionFids;
|
8
|
|
|
|
9
|
|
|
/**
|
10
|
|
|
* Each category supported by your application should be represented by its own Decorator class,
|
11
|
|
|
* as even two similar Allegro categories can have same field under different FIDs,
|
12
|
|
|
* for example in Magic: The Gathering cards:
|
13
|
|
|
* - category id 6089 (artifacts) has condition (new/used) under fid 26013
|
14
|
|
|
* - category id 6090 (white) has condition under fid 20624
|
15
|
|
|
*/
|
16
|
|
|
|
17
|
|
|
abstract class AuctionArrayMapDecorator implements AuctionInterface
|
18
|
|
|
{
|
19
|
|
|
|
20
|
|
|
protected $auction = null;
|
21
|
|
|
|
22
|
|
|
/**
|
23
|
|
|
* This function should return array of mappings (human readable key => Allegro WebAPI FID)
|
24
|
|
|
* @return array
|
25
|
|
|
*/
|
26
|
|
|
abstract protected function getMap();
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* This function should return id of Allegro category related to concrete decorator class
|
31
|
|
|
* @return integer
|
32
|
|
|
*/
|
33
|
|
|
abstract protected function getIdCategory();
|
34
|
|
|
|
35
|
|
|
|
36
|
9 |
|
public function __construct(AuctionInterface $auction)
|
37
|
|
|
{
|
38
|
9 |
|
$this->auction = $auction;
|
39
|
9 |
|
}
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/**
|
43
|
|
|
* Makes sure that if a concrete class represents specific category, category id passed in fields array is correct:
|
44
|
|
|
* - set it as default, if none was provided
|
45
|
|
|
* - throw an exception, if some other category id was provided in $fields
|
46
|
|
|
* @param array $fields (reference) fields array to check
|
47
|
|
|
*/
|
48
|
6 |
|
protected function forceCategory(array& $fields)
|
49
|
|
|
{
|
50
|
|
|
//no category defined in concrete class - do nothing;
|
51
|
6 |
|
if (!$this->getIdCategory()) {
|
52
|
2 |
|
return;
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
//no category is given - default to set constant
|
56
|
4 |
|
if (!array_key_exists(AuctionFids::FID_CATEGORY, $fields)) {
|
57
|
2 |
|
$fields[AuctionFids::FID_CATEGORY] = $this->getIdCategory();
|
58
|
2 |
|
}
|
59
|
|
|
|
60
|
|
|
//wrong category is given
|
61
|
4 |
|
if ($fields[AuctionFids::FID_CATEGORY] !== $this->getIdCategory()) {
|
62
|
2 |
|
throw new Exception("Invalid category. {$this->getIdCategory()} expected, {$fields[AuctionFids::FID_CATEGORY]} given.");
|
63
|
|
|
}
|
64
|
|
|
|
65
|
2 |
|
}
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* Remap field keys using given map
|
70
|
|
|
* @param array $array to remap
|
71
|
|
|
* @param array $map to remap with
|
72
|
|
|
* @return array remapped array
|
73
|
|
|
*/
|
74
|
5 |
|
protected function remap(array $array, array $map)
|
75
|
|
|
{
|
76
|
5 |
|
$result = [];
|
77
|
|
|
|
78
|
5 |
|
foreach($array as $key => $value) {
|
79
|
4 |
|
if (array_key_exists($key, $map)) {
|
80
|
4 |
|
$result[$map[$key]] = $value;
|
81
|
4 |
|
}
|
82
|
5 |
|
}
|
83
|
|
|
|
84
|
5 |
|
return $result;
|
85
|
|
|
}
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* @see Radowoj\Yaah\Auction::fromArray()
|
89
|
|
|
* @param array $humanReadableArray with human readable keys
|
90
|
|
|
*/
|
91
|
3 |
|
public function fromArray(array $humanReadableArray)
|
92
|
|
|
{
|
93
|
3 |
|
$map = $this->getMap();
|
94
|
3 |
|
$fields = $this->remap($humanReadableArray, $map);
|
95
|
3 |
|
$this->forceCategory($fields);
|
96
|
2 |
|
$this->auction->fromArray($fields);
|
97
|
2 |
|
}
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/**
|
101
|
|
|
* @see Radowoj\Yaah\Auction::toArray()
|
102
|
|
|
* @return array with human readable keys
|
103
|
|
|
*/
|
104
|
3 |
|
public function toArray()
|
105
|
|
|
{
|
106
|
3 |
|
$map = $this->getMap();
|
107
|
3 |
|
$fields = $this->auction->toArray();
|
108
|
3 |
|
$this->forceCategory($fields);
|
109
|
2 |
|
$flippedMap = array_flip($map);
|
110
|
2 |
|
return $this->remap($fields, $flippedMap);
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/**
|
115
|
|
|
* @see Radowoj\Yaah\Auction::toApiRepresentation()
|
116
|
|
|
*/
|
117
|
1 |
|
public function toApiRepresentation()
|
118
|
|
|
{
|
119
|
1 |
|
return $this->auction->toApiRepresentation();
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/**
|
124
|
|
|
* @see Radowoj\Yaah\Auction::fromApiRepresentation()
|
125
|
|
|
*/
|
126
|
1 |
|
public function fromApiRepresentation(array $fields)
|
127
|
|
|
{
|
128
|
1 |
|
$this->auction->fromApiRepresentation($fields);
|
129
|
1 |
|
}
|
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/**
|
133
|
|
|
* @see Radowoj\Yaah\Auction::setPhotos()
|
134
|
|
|
*/
|
135
|
1 |
|
public function setPhotos(array $photos)
|
136
|
|
|
{
|
137
|
1 |
|
$this->auction->setPhotos($photos);
|
138
|
1 |
|
}
|
139
|
|
|
|
140
|
|
|
|
141
|
|
|
}
|
142
|
|
|
|