XisbnResponse::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Colligator;
4
5
class XisbnResponse
6
{
7
    protected $data;
8
9
    public $formats = [
10
        'AA'    => 'audio',
11
        'AA BA' => 'audio book',
12
        'BA'    => 'book',
13
        'BA DA' => 'ebook',      // Yes, we DO actually get these
14
        'BB'    => 'hardcover',
15
        'BB BC' => 'book',       // ... and these
16
        'BB DA' => 'ebook',      // ... and these
17
        'BC'    => 'paperback',
18
        'BC DA' => 'ebook',      // ... and these
19
        'DA'    => 'digital',
20
        'FA'    => 'film/transp.',
21
        'MA'    => 'microform',
22
        'VA'    => 'video',
23
    ];
24
25
    public function __construct(array $data = null)
26
    {
27
        $this->data = $data ?: [];
28
    }
29
30
    public function overLimit()
31
    {
32
        foreach (array_values($this->data) as $response) {
33
            if ($response['stat'] == 'overlimit') {
34
                return true;
35
            }
36
        }
37
38
        return false;
39
    }
40
41
    protected function getForm($item)
42
    {
43
        if (!isset($item['form'])) {
44
            return;
45
        }
46
        $forms = $item['form'];
47
        sort($forms);
48
        $formStr = implode(' ', $forms);
49
        if (isset($this->formats[$formStr])) {
50
            return $this->formats[$formStr];
51
        }
52
        $formStr = implode(' ', array_map(function ($el) {
53
            return $this->formats[$el];
54
        }, $forms));
55
        \Log::warning(sprintf('Unknown form: %s', $formStr));
56
57
        return $formStr;
58
    }
59
60
    public function toArray()
61
    {
62
        return $this->data;
63
    }
64
65
    public function getSimpleRepr()
66
    {
67
        $items = [];
68
        foreach ($this->data as $sourceIsbn => $response) {
69
            if ($response['stat'] != 'ok') {
70
                continue;
71
            }
72
            foreach ($response['list'] as $listItem) {
73
                foreach ($listItem['isbn'] as $isbn) {
74
                    $item = [
75
                        'isbn' => $isbn,
76
                        'form' => $this->getForm($listItem),
77
                    ];
78
                    if (isset($listItem['ed'])) {
79
                        $item['edition'] = str_replace(['[', ']'], ['', ''], $listItem['ed']);
80
                    }
81
                    $items[] = $item;
82
                }
83
            }
84
        }
85
86
        return $items;
87
    }
88
}
89