1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate\Traits; |
6
|
|
|
|
7
|
|
|
use Suricate\DBObject; |
8
|
|
|
|
9
|
|
|
trait DBObjectProtected |
10
|
|
|
{ |
11
|
|
|
protected $protectedVariables = []; |
12
|
|
|
protected $protectedValues = []; |
13
|
|
|
protected $loadedProtectedVariables = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param string $name |
17
|
|
|
*/ |
18
|
1 |
|
private function getProtectedVariable(string $name) |
19
|
|
|
{ |
20
|
|
|
// Variable exists, and is already loaded |
21
|
|
|
if ( |
22
|
1 |
|
isset($this->protectedValues[$name]) && |
23
|
1 |
|
$this->isProtectedVariableLoaded($name) |
24
|
|
|
) { |
25
|
|
|
return $this->protectedValues[$name]; |
26
|
|
|
} |
27
|
|
|
// Variable has not been loaded |
28
|
1 |
|
if (!$this->isProtectedVariableLoaded($name)) { |
29
|
1 |
|
if ($this->accessToProtectedVariable($name)) { |
30
|
1 |
|
$this->markProtectedVariableAsLoaded($name); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
if (isset($this->protectedValues[$name])) { |
35
|
1 |
|
return $this->protectedValues[$name]; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
return null; |
39
|
|
|
} |
40
|
|
|
/** |
41
|
|
|
* Mark a protected variable as loaded |
42
|
|
|
* @param string $name varialbe name |
43
|
|
|
* |
44
|
|
|
* @return DBObject |
45
|
|
|
*/ |
46
|
1 |
|
public function markProtectedVariableAsLoaded(string $name) |
47
|
|
|
{ |
48
|
1 |
|
if ($this->isProtectedVariable($name)) { |
49
|
1 |
|
$this->loadedProtectedVariables[$name] = true; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check if variable is a protected variable |
57
|
|
|
* @param string $name variable name |
58
|
|
|
* @return boolean |
59
|
|
|
*/ |
60
|
8 |
|
public function isProtectedVariable(string $name): bool |
61
|
|
|
{ |
62
|
8 |
|
return in_array($name, $this->protectedVariables); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check if a protected variable already have been loaded |
67
|
|
|
* @param string $name Variable name |
68
|
|
|
* @return boolean |
69
|
|
|
*/ |
70
|
1 |
|
protected function isProtectedVariableLoaded(string $name): bool |
71
|
|
|
{ |
72
|
1 |
|
return isset($this->loadedProtectedVariables[$name]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function accessToProtectedVariable(string $name): bool |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.