AbstractSearchBuilder   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 0
dl 0
loc 139
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A __construct() 0 4 1
A populate() 0 12 4
A hasProperty() 0 4 2
A canGetProperty() 0 4 3
A canSetProperty() 0 4 3
A hasMethod() 0 4 1
A __get() 0 11 3
A __set() 0 11 3
A __isset() 0 9 2
A __unset() 0 9 3
A __call() 0 4 1
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
use InvalidArgumentException;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 3.0.0
16
 */
17
abstract class AbstractSearchBuilder implements SearchBuilderInterface
18
{
19
    /**
20
     * @return mixed|string
21
     */
22
    public function __toString()
23
    {
24
        return $this->build();
25
    }
26
27
    /**
28
     * @param array $properties
29
     */
30
    public function __construct(array $properties = [])
31
    {
32
        $this->populate($properties);
33
    }
34
35
    /**
36
     * @param array $properties
37
     * @return static
38
     */
39
    public function populate(array $properties = [])
40
    {
41
        if (!empty($properties)) {
42
            foreach ($properties as $name => $value) {
43
                if ($this->canSetProperty($name)) {
44
                    $this->{$name} = $value;
45
                }
46
            }
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function hasProperty($name, $checkVars = true): bool
56
    {
57
        return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function canGetProperty($name, $checkVars = true): bool
64
    {
65
        return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function canSetProperty($name, $checkVars = true): bool
72
    {
73
        return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function hasMethod($name): bool
80
    {
81
        return method_exists($this, $name);
82
    }
83
84
    /**
85
     * @param $name
86
     * @return mixed
87
     * @throws InvalidArgumentException
88
     */
89
    public function __get($name)
90
    {
91
        $getter = 'get' . $name;
92
        if (method_exists($this, $getter)) {
93
            return $this->$getter();
94
        } elseif (method_exists($this, 'set' . $name)) {
95
            throw new InvalidArgumentException('Getting write-only property: ' . get_class($this) . '::' . $name);
96
        } else {
97
            throw new InvalidArgumentException('Getting unknown property: ' . get_class($this) . '::' . $name);
98
        }
99
    }
100
101
    /**
102
     * @param $name
103
     * @param $value
104
     * @throws InvalidArgumentException
105
     */
106
    public function __set($name, $value)
107
    {
108
        $setter = 'set' . $name;
109
        if (method_exists($this, $setter)) {
110
            $this->$setter($value);
111
        } elseif (method_exists($this, 'get' . $name)) {
112
            throw new InvalidArgumentException('Setting read-only property: ' . get_class($this) . '::' . $name);
113
        } else {
114
            throw new InvalidArgumentException('Setting unknown property: ' . get_class($this) . '::' . $name);
115
        }
116
    }
117
118
    /**
119
     * @param $name
120
     * @return bool
121
     */
122
    public function __isset($name)
123
    {
124
        $getter = 'get' . $name;
125
        if (method_exists($this, $getter)) {
126
            return $this->$getter() !== null;
127
        } else {
128
            return false;
129
        }
130
    }
131
132
    /**
133
     * @param $name
134
     * @throws InvalidArgumentException
135
     */
136
    public function __unset($name)
137
    {
138
        $setter = 'set' . $name;
139
        if (method_exists($this, $setter)) {
140
            $this->$setter(null);
141
        } elseif (method_exists($this, 'get' . $name)) {
142
            throw new InvalidArgumentException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
143
        }
144
    }
145
146
    /**
147
     * @param $name
148
     * @param $params
149
     * @throws InvalidArgumentException
150
     */
151
    public function __call($name, $params)
152
    {
153
        throw new InvalidArgumentException('Calling unknown method: ' . get_class($this) . "::$name()");
154
    }
155
}
156