|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Radowoj\Yaah; |
|
4
|
|
|
|
|
5
|
|
|
use Radowoj\Yaah\Constants\AuctionFids; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
class Auction implements AuctionInterface |
|
9
|
|
|
{ |
|
10
|
|
|
const MAX_PHOTOS = 8; |
|
11
|
|
|
|
|
12
|
|
|
protected $fields = []; |
|
13
|
|
|
|
|
14
|
|
|
protected $photos = []; |
|
15
|
|
|
|
|
16
|
2 |
|
public function __construct(array $fields = []) |
|
17
|
|
|
{ |
|
18
|
2 |
|
$this->setFields($fields); |
|
19
|
2 |
|
} |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Sets photos for auction |
|
24
|
|
|
* @param array $photos array of photo file paths |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function setPhotos(array $photos) |
|
27
|
|
|
{ |
|
28
|
1 |
|
$photosCount = count($photos); |
|
29
|
|
|
|
|
30
|
1 |
|
if ($photosCount > self::MAX_PHOTOS) { |
|
31
|
1 |
|
throw new InvalidArgumentException("Photo files limit exceeded, " . self::MAX_PHOTOS . " allowed, " . $photosCount . " given"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->photos = $photos; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
2 |
|
public function setFields(array $fields) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$this->fields = $fields; |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns WebAPI's representation of an auction (array of fields) |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function toApiRepresentation() |
|
49
|
|
|
{ |
|
50
|
1 |
|
$fields = []; |
|
51
|
|
|
|
|
52
|
1 |
|
foreach ($this->fields as $fid => $value) { |
|
53
|
1 |
|
$fields[] = (new Field($fid, $value))->toArray(); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
$this->addPhotoFields($fields); |
|
57
|
|
|
|
|
58
|
|
|
return [ |
|
59
|
1 |
|
'fields' => $fields, |
|
60
|
1 |
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Creates an auction from WebAPI's representation (array of fields) |
|
66
|
|
|
* @param array $fields |
|
67
|
|
|
*/ |
|
68
|
|
|
public function fromApiRepresentation(array $fields) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->fields = []; |
|
71
|
|
|
$this->photos = []; |
|
72
|
|
|
|
|
73
|
|
|
foreach ($fields as $apiField) { |
|
74
|
|
|
$field = new Field(); |
|
75
|
|
|
$field->fromArray((array)$apiField); |
|
76
|
|
|
$this->fields[$field->getFid()] = $field->getValue(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Add photos to given array of Fields |
|
83
|
|
|
* @param array& $fields array of Fields to extend with photos |
|
|
|
|
|
|
84
|
|
|
*/ |
|
85
|
1 |
|
protected function addPhotoFields(array& $fields) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$count = count($this->photos); |
|
88
|
1 |
|
if (!$count) { |
|
89
|
1 |
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$index = 0; |
|
93
|
|
|
foreach ($this->photos as $photo) { |
|
94
|
|
|
if (!is_readable($photo)) { |
|
95
|
|
|
throw new Exception("Photo file {$photo} is not readable"); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$fields[] = (new Field(AuctionFids::FID_PHOTO + $index, file_get_contents($photo), Field::VALUE_IMAGE))->toArray(); |
|
99
|
|
|
$index++; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Simplified array representation (similar to constructor params) |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function toArray() |
|
109
|
|
|
{ |
|
110
|
|
|
$fields = $this->fields; |
|
111
|
|
|
$this->addPhotoFields($fields); |
|
112
|
|
|
return $this->fields; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.