DynamicSearchBuilder::getReplacingAttributes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/salesforce/blob/master/LICENSE.md
6
 * @link       https://github.com/flipbox/salesforce
7
 */
8
9
namespace Flipbox\Salesforce\Search;
10
11
/**
12
 * @author Flipbox Factory <[email protected]>
13
 * @since 3.0.0
14
 */
15
class DynamicSearchBuilder extends RawSearchBuilder implements DynamicSearchBuilderInterface
16
{
17
    use DynamicVariablesAttributeTrait;
18
19
    /**
20
     * The opening variable character
21
     */
22
    const VARIABLE_OPENING = '{{';
23
24
    /**
25
     * The closing variable character
26
     */
27
    const VARIABLE_CLOSING = '}}';
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function build(): string
33
    {
34
        return $this->prepareSearch($this->search);
35
    }
36
37
    /**
38
     * @param string $soql
39
     * @return string
40
     */
41
    private function prepareSearch(string $soql): string
42
    {
43
        if (false === (preg_match_all(
44
            '/' . self::VARIABLE_OPENING . '(.*?)' . self::VARIABLE_CLOSING . '/',
45
            $soql,
46
            $matches
47
        ))) {
48
            return $soql;
49
        }
50
51
        $replace = $this->getReplacingAttributes($matches[1]);
52
53
        return str_ireplace(array_keys($replace), array_values($replace), $soql);
54
    }
55
56
    /**
57
     * @param array $variables
58
     * @return array
59
     */
60
    private function getReplacingAttributes(array $variables = [])
61
    {
62
        $attributes = $this->getVariables();
63
64
        $values = [];
65
66
        foreach ($variables as $variable) {
67
            $values[self::VARIABLE_OPENING . $variable . self::VARIABLE_CLOSING] =
68
                ($attributes[$variable] ?? $variable);
69
        }
70
71
        return $values;
72
    }
73
}
74