Test Failed
Push — master ( 21a99b...e0e87a )
by Kirill
04:20
created

Field::parseFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Criteria\Common;
11
12
/**
13
 * Class Field
14
 */
15
class Field implements FieldInterface
16
{
17
    /**
18
     * Inherit value delimiter
19
     */
20
    public const DEEP_DELIMITER = '.';
21
22
    /**
23
     * Prefix using for disable aliasing field
24
     */
25
    public const NON_ALIASED_PREFIX = ':';
26
27
    /**
28
     * @var bool
29
     */
30
    private $hasFunction = false;
31
32
    /**
33
     * @var bool
34
     */
35
    private $isAliased = true;
36
37
    /**
38
     * @var string
39
     */
40
    private $field;
41
42
    /**
43
     * @var string
44
     */
45
    private $wrapper = '%s';
46
47
    /**
48
     * Field constructor.
49
     * @param string $field
50
     */
51
    public function __construct(string $field)
52
    {
53
        \assert(\strlen($field) > 0);
54
55
        $this->field = $this->extractFieldPrefixLogic(
56
            $this->extractFieldFromFunction($field)
57
        );
58
59
    }
60
61
    /**
62
     * @param string $field
63
     * @return string
64
     */
65
    private function extractFieldPrefixLogic(string $field): string
66
    {
67
        if (\strpos($field, self::NON_ALIASED_PREFIX) === 0) {
68
            $this->isAliased = false;
69
70
            return \substr($field, \strlen(self::NON_ALIASED_PREFIX));
71
        }
72
73
        return $field;
74
    }
75
76
    /**
77
     * @param string $field
78
     * @return string
79
     */
80
    private function extractFieldFromFunction(string $field): string
81
    {
82
        $pattern = '/\(([\w|\.|\:]+)\)/u';
83
        \preg_match($pattern, $field, $chunks);
84
85
        if (\count($chunks)) {
86
            $this->wrapper = \str_replace($chunks[1], '%s', $chunks[0]);
87
            $this->hasFunction = true;
88
89
            return $chunks[1];
90
        }
91
92
        return $field;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    public function isComposite(): bool
99
    {
100
        return \substr_count($this->field, self::DEEP_DELIMITER) > 0;
101
    }
102
103
    /**
104
     * @param string $alias
105
     * @return string
106
     */
107
    public function withAlias(string $alias): string
108
    {
109
        $field = $this->isAliased
110
            ? \implode(self::DEEP_DELIMITER, [$alias, $this->field])
111
            : $this->field;
112
113
        return \sprintf($this->wrapper, $field);
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function toString(): string
120
    {
121
        return $this->field;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function __toString(): string
128
    {
129
        return $this->field;
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function isPrefixed(): bool
136
    {
137
        return $this->isAliased;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getName(): string
144
    {
145
        return $this->field;
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function isFunction(): bool
152
    {
153
        return $this->hasFunction;
154
    }
155
156
    /**
157
     * @return iterable|Field[]
158
     */
159
    public function split(): iterable
160
    {
161
        foreach (\explode(self::DEEP_DELIMITER, $this->field) as $chunk) {
162
            yield new static($chunk);
163
        }
164
    }
165
}
166