Completed
Push — master ( 74d8cf...88d0d9 )
by Marcin
02:58
created

Province::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
crap 20
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
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 *
14
 */
15
16
/**
17
 * Created by Marcin Pudełek <[email protected]>
18
 * Date: 07.09.2017
19
 */
20
21
namespace mrcnpdlk\Teryt\Model;
22
23
24
use mrcnpdlk\Teryt\Exception;
25
use mrcnpdlk\Teryt\NativeApi;
26
27
/**
28
 * Class Province
29
 *
30
 * @package mrcnpdlk\Teryt\Model
31
 */
32
class Province extends EntityAbstract
33
{
34
    /**
35
     * Dwuznakowy symbol województwa
36
     *
37
     * @var string
38
     */
39
    public $id;
40
    /**
41
     * Nazwa województwa
42
     *
43
     * @var static
44
     */
45
    public $name;
46
47
    /**
48
     * Province constructor.
49
     *
50
     * @param string $id Dwuznakowy symbol województwa
51
     *
52
     * @return $this
53
     * @throws Exception\NotFound
54
     */
55 1
    public function find(string $id)
56
    {
57 1
        foreach (NativeApi::getInstance()->PobierzListeWojewodztw() as $w) {
58 1
            if ($w->provinceId === $id) {
59 1
                $this->id   = $w->provinceId;
60 1
                $this->name = $w->name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $w->name of type string is incompatible with the declared type mrcnpdlk\Teryt\Model\Province of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
            }
62
        }
63 1
        if (!$this->id) {
64
            throw new Exception\NotFound(sprintf('Province [id:%s] not exists', $id));
65
        }
66
67 1
        return $this;
68
    }
69
70
71
    /**
72
     * Pełnokontekstowe wyszukiwanie powiatów w województwie
73
     *
74
     * @param string|null $phrase Szukana fraza, gdy NULL zwraca wszystkie powiaty w województwie
75
     *
76
     * @return \mrcnpdlk\Teryt\Model\District[]
77
     * @throws \Exception
78
     */
79
    public function searchDistricts(string $phrase = null)
80
    {
81
        try {
82
            $answer = [];
83
            if ($phrase) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $phrase of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
84
                $tList = NativeApi::getInstance()->WyszukajJednostkeWRejestrze($phrase, NativeApi::CATEGORY_POW_ALL);
85
                foreach ($tList as $p) {
86
                    if ($p->provinceId === $this->id) {
87
                        $answer[] = (new District())->find($p->provinceId . $p->districtId);
88
                    }
89
                }
90
            } else {
91
                $tList = NativeApi::getInstance()->PobierzListePowiatow($this->id);
92
                foreach ($tList as $p) {
93
                    $answer[] = (new District())->find($p->provinceId . $p->districtId);
94
                }
95
            }
96
97
            return $answer;
98
99
        } catch (Exception\NotFound $e) {
100
            return [];
101
        } catch (\Exception $e) {
102
            throw $e;
103
        }
104
    }
105
}
106
107
108