Province::searchDistricts()   B
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 16
nop 1
dl 0
loc 23
ccs 0
cts 15
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * TERYT-API
4
 *
5
 * Copyright (c) 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 * @author  Marcin Pudełek <[email protected]>
12
 */
13
14
/**
15
 * Created by Marcin Pudełek <[email protected]>
16
 * Date: 07.09.2017
17
 */
18
19
namespace mrcnpdlk\Teryt\Model;
20
21
use mrcnpdlk\Teryt\Exception;
22
use mrcnpdlk\Teryt\NativeApi;
23
24
/**
25
 * Class Province
26
 */
27
class Province extends EntityAbstract
28
{
29
    /**
30
     * Dwuznakowy symbol województwa
31
     *
32
     * @var string
33
     */
34
    public $id;
35
    /**
36
     * Nazwa województwa
37
     *
38
     * @var string
39
     */
40
    public $name;
41
42
    /**
43
     * Province constructor.
44
     *
45
     * @param string $id Dwuznakowy symbol województwa
46
     *
47
     * @throws \mrcnpdlk\Teryt\Exception\Connection
48
     * @throws \mrcnpdlk\Teryt\Exception
49
     * @throws Exception\NotFound
50
     *
51
     * @return $this
52
     */
53 1
    public function find(string $id)
54
    {
55 1
        foreach (NativeApi::getInstance()->PobierzListeWojewodztw() as $w) {
56 1
            if ($w->provinceId === $id) {
57 1
                $this->id   = $w->provinceId;
58 1
                $this->name = $w->name;
59
            }
60
        }
61 1
        if (!$this->id) {
62
            throw new Exception\NotFound(sprintf('Province [id:%s] not exists', $id));
63
        }
64
65 1
        return $this;
66
    }
67
68
    /**
69
     * Pełnokontekstowe wyszukiwanie powiatów w województwie
70
     *
71
     * @param string|null $phrase Szukana fraza, gdy NULL zwraca wszystkie powiaty w województwie
72
     *
73
     * @throws \Exception
74
     *
75
     * @return \mrcnpdlk\Teryt\Model\District[]
76
     */
77
    public function searchDistricts(string $phrase = null)
78
    {
79
        try {
80
            $answer = [];
81
            if ($phrase) {
82
                $tList = NativeApi::getInstance()->WyszukajJednostkeWRejestrze($phrase, NativeApi::CATEGORY_POW_ALL);
83
                foreach ($tList as $p) {
84
                    if ($p->provinceId === $this->id) {
85
                        $answer[] = (new District())->find($p->provinceId . $p->districtId);
86
                    }
87
                }
88
            } else {
89
                $tList = NativeApi::getInstance()->PobierzListePowiatow($this->id);
90
                foreach ($tList as $p) {
91
                    $answer[] = (new District())->find($p->provinceId . $p->districtId);
92
                }
93
            }
94
95
            return $answer;
96
        } catch (Exception\NotFound $e) {
97
            return [];
98
        } catch (\Exception $e) {
99
            throw $e;
100
        }
101
    }
102
}
103