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

Field   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 2
dl 0
loc 71
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A canBeSelected() 0 4 1
A isApplicable() 0 4 1
A getName() 0 4 1
A getSql() 0 4 1
A getAttribute() 0 4 1
A buildCondition() 0 12 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