DynamicSearchBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 59
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 4 1
A prepareSearch() 0 14 2
A getReplacingAttributes() 0 13 2
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