Completed
Push — master ( 796399...e742f7 )
by H
01:59
created

Classification::getSelectArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 10
nc 4
nop 0
1
<?php
2
3
namespace Wispiring\ChinaEconomyClassification;
4
5
class Classification
6
{
7
    const STANDARD = 'GB/4754';
8
    private $dataSource;
9
    private $data;
10
11
    public function __construct($dataSource = null)
12
    {
13
        if (null === $dataSource) {
14
            $this->dataSource = 'Y2011';
15
        }
16
        $className = '\Wispiring\ChinaEconomyClassification\Data\\'.$this->dataSource;
17
        $this->data = new $className();
18
        $this->data = $this->data->get();
19
    }
20
21
    public function getArray()
22
    {
23
        return $this->data;
24
    }
25
26
    public function getJson()
27
    {
28
        return json_encode($this->data);
29
    }
30
31
    public function getByCode($code)
32
    {
33
        return isset($this->data[$code]) ? $this->data[$code] : null;
34
    }
35
36
    public function getSelectArray()
37
    {
38
        $o = [];
39
        foreach ($this->data as $key => $value) {
40
            if (strlen($value['code']) !== 4) {
41
                continue;
42
            }
43
            $groupLabel = $value['top_category'].' '.$this->data[$value['top_category']]['name'];
44
            if (!isset($o[$groupLabel])) {
45
                $o[$groupLabel] = [];
46
            }
47
            $o[$groupLabel][$value['code']]= $value['name'];
48
        }
49
        return $o;
50
    }
51
52
    public function getByName($name)
53
    {
54
        foreach ($this->data as $value) {
55
            if ($value['name'] === $name) {
56
                return $value;
57
            }
58
        }
59
    }
60
61
    public function getByTopCategory($categoryValue)
62
    {
63
        return $this->getByCategory('top_category', $categoryValue);
64
    }
65
66
    public function getByFirstCategory($categoryValue)
67
    {
68
        return $this->getByCategory('first_category', $categoryValue);
69
    }
70
71
    public function getBySecondCategory($categoryValue)
72
    {
73
        return $this->getByCategory('second_category', $categoryValue);
74
    }
75
76
    public function getByThirdCategory($categoryValue)
77
    {
78
        return $this->getByCode($categoryValue);
79
    }
80
81
    private function getByCategory($categoryKey, $categoryValue)
82
    {
83
        $o = [];
84
        foreach ($this->data as $key => $value) {
85
            if ($value[$categoryKey] === $categoryValue) {
86
                $o[$key] = $value;
87
            }
88
        }
89
        return $o;
90
    }
91
}
92