ValueTrait::value()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits;
4
5
use Janisbiz\LightOrm\Dms\MySQL\Enum\ConditionEnum;
6
7
trait ValueTrait
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $value = [];
13
14
    /**
15
     * @var array
16
     */
17
    protected $bindValue = [];
18
19
    /**
20
     * @param string $column
21
     * @param null|int|string|double $value
22
     *
23
     * @return $this
24
     */
25
    public function value(string $column, $value)
26
    {
27
        $columnNormalised = \sprintf(
28
            '%s_Value',
29
            \implode(
30
                '_',
31
                \array_map(
32
                    function ($columnPart) {
33
                        return \mb_convert_case($columnPart, MB_CASE_TITLE);
34
                    },
35
                    \explode('.', $column)
36
                )
37
            )
38
        );
39
40
        $this->value[$column] = \sprintf(':%s', $columnNormalised);
41
        $this->bindValue([
42
            $columnNormalised => $value,
43
        ]);
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param array $bindValue
50
     *
51
     * @return $this
52
     */
53
    public function bindValue(array $bindValue = [])
54
    {
55
        $this->bindValue = \array_merge($this->bindValue, $bindValue);
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function bindValueData(): array
64
    {
65
        return $this->bindValue;
66
    }
67
68
    /**
69
     * @return null|string
70
     */
71
    protected function buildValueQueryPart(): ?string
72
    {
73
        return empty($this->value)
74
            ? null
75
            : \sprintf(
76
                '(%s) %s (%s)',
77
                \implode(', ', \array_keys($this->value)),
78
                ConditionEnum::VALUES,
79
                \implode(', ', $this->value)
80
            )
81
        ;
82
    }
83
}
84