Passed
Push — master ( db4f6d...ea2827 )
by Radosław
11:43
created

AuctionArrayMapDecorator::fromApiRepresentation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Radowoj\Yaah\Decorators;
4
5
use Radowoj\Yaah\AuctionInterface;
6
7
abstract class AuctionArrayMapDecorator implements AuctionInterface, AuctionDecoratorInterface
8
{
9
    protected $auction = null;
10
11
    protected $map = [
12
        //human readable key => Allegro WebAPI FID
13
    ];
14
15
16
    public function __construct(AuctionInterface $auction)
17
    {
18
        $this->auction = $auction;
19
    }
20
21
22
    public function fromArray(array $humanReadableArray)
23
    {
24
        $fields = [];
25
        foreach($humanReadableArray as $key => $value) {
26
            if (array_key_exists($key, $this->map)) {
27
                $fields[$this->map[$key]] = $value;
28
            }
29
        }
30
        $this->auction->setFields($fields);
31
    }
32
33
34
    public function toArray()
35
    {
36
        $fields = $this->auction->toArray();
37
        $flippedMap = array_flip($this->map);
38
        $humanReadableArray = [];
39
40
        foreach($fields as $key => $value) {
41
            if (array_key_exists($key, $flippedMap)) {
42
                $humanReadableArray[$flippedMap[$key]] = $value;
43
            }
44
        }
45
46
        return $humanReadableArray;
47
    }
48
49
50
    public function toApiRepresentation()
51
    {
52
        return $this->auction->toApiRepresentation();
53
    }
54
55
    public function fromApiRepresentation(array $fields)
56
    {
57
        $this->auction->fromApiRepresentation($fields);
58
    }
59
60
    public function setPhotos(array $photos)
61
    {
62
        $this->auction->setPhotos($photos);
63
    }
64
65
    public function setFields(array $fields)
66
    {
67
        $this->auction->setFields($fields);
68
    }
69
70
71
}
72