AbstractDatabaseQuery::kladrCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace GeoFixer\models\queries;
4
5
use Cake\Database\Connection;
6
use GeoFixer\models\DatabaseConnection;
7
8
/**
9
 * Class AbstractDatabaseQuery
10
 *
11
 * @package GeoFixer\models\queries
12
 */
13
class AbstractDatabaseQuery
14
{
15
    /**
16
     * Address levels в базе ФИАС
17
     */
18
    const CITY = 4;
19
    const SETTLEMENT = 6;
20
    const STREET = 7;
21
22
    const KLADR_CODE = 'code';
23
    const FIAS_CODE = 'address_id';
24
    const TITLE = 'title';
25
26
27
    /**
28
     * @var Connection::newQuery()
29
     */
30
    protected $db;
31
32
    /**
33
     * AbstractDatabaseQuery constructor.
34
     *
35
     * @uses Connection::newQuery()
36
     */
37 31
    public function __construct()
38
    {
39 31
        $this->db = DatabaseConnection::connection()->newQuery();
40 31
    }
41
42
    /**
43
     * Первые буквы равны
44
     *
45
     * @param $letters
46
     *
47
     * @return $this
48
     */
49 6
    public function firstLetters($letters)
50
    {
51 6
        $this->db = $this->db->andWhere([self::TITLE . ' LIKE' => $letters . '%']);
52
53 6
        return $this;
54
    }
55
56
    /**
57
     * Код региона равен
58
     *
59
     * @param $code
60
     *
61
     * @return $this
62
     */
63 10
    public function regionCode($code)
64
    {
65 10
        $this->db = $this->db->andWhere(['region_code' => $code]);
66
67 10
        return $this;
68
    }
69
70
    /**
71
     * Код КЛАДР равен
72
     *
73
     * @param $code
74
     *
75
     * @return $this
76
     */
77 4
    public function kladrCode($code)
78
    {
79 4
        $this->db = $this->db->andWhere([self::KLADR_CODE => $code]);
80
81 4
        return $this;
82
    }
83
84
    /**
85
     * ID родителя равен
86
     *
87
     * @param $id
88
     *
89
     * @return $this
90
     */
91 7
    public function parentId($id)
92
    {
93 7
        $this->db = $this->db->andWhere(['parent_id' => $id]);
94
95 7
        return $this;
96
    }
97
98
    /**
99
     * Получаем массив значений
100
     *
101
     * @return array
102
     */
103 25
    public function findAll()
104
    {
105 25
        return $this->db->execute()->fetchAll('assoc');
106
    }
107
108
    /**
109
     * Получаем одно значение
110
     *
111
     * @return array
112
     */
113 9
    public function findOne()
114
    {
115 9
        return $this->db->execute()->fetch('assoc');
116
    }
117
}
118