Completed
Push — master ( be2280...4a4169 )
by Andrii
01:43
created

Field::getSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * HiAPI Yii2 base project for building API
4
 *
5
 * @link      https://github.com/hiqdev/hiapi
6
 * @package   hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiapi\query;
12
13
use hiapi\query\attributes\AbstractAttribute;
14
15
class Field implements FieldInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $name;
21
22
    /**
23
     * @var string
24
     */
25
    protected $sql;
26
27
    /**
28
     * @var AbstractAttribute
29
     */
30
    protected $attribute;
31
32
    public function __construct($name, $sql, AbstractAttribute $attribute)
33
    {
34
        $this->name = $name;
35
        $this->sql = $sql;
36
        $this->attribute = $attribute;
37
    }
38
39
    public function canBeSelected()
40
    {
41
        return true;
42
    }
43
44
    public function isApplicable($key)
45
    {
46
        return strcasecmp($this->name, $key) === 0;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getName()
53
    {
54
        return $this->name;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getSql()
61
    {
62
        return $this->sql;
63
    }
64
65
    /**
66
     * @return AbstractAttribute
67
     */
68
    public function getAttribute()
69
    {
70
        return $this->attribute;
71
    }
72
73
    public function buildCondition($value)
74
    {
75
        if (is_array($value)) {
76
            throw new InvalidParamException('Condition ' . json_encode($value) . ' is not supported yet.');
77
        }
78
79
        $validator = $this->getAttribute()->getValidatorFor('eq');
80
        $value = $validator->normalize($value);
81
        $validator->validate($value);
82
83
        return [$this->getSql() => $value];
84
    }
85
}
86