1
|
|
|
<?php
|
2
|
|
|
namespace Ext;
|
3
|
|
|
|
4
|
|
|
class SerializableObject implements Serializable
|
5
|
|
|
{
|
6
|
|
|
/** @var array */
|
7
|
|
|
private $properties = array();
|
8
|
|
|
|
9
|
|
|
/** @var array */
|
10
|
|
|
protected $ignore = array();
|
11
|
|
|
|
12
|
|
|
public function serialize(Serializer $serializer) {
|
13
|
|
|
return $serializer->serialize($this);
|
14
|
|
|
}
|
15
|
|
|
|
16
|
|
|
public function properties() {
|
17
|
|
|
$properties = array();
|
18
|
|
|
|
19
|
|
|
foreach($this->properties as $key => $value) {
|
20
|
|
|
if (!in_array($key, $this->ignore)){
|
21
|
|
|
$properties[$key] = $value;
|
22
|
|
|
}
|
23
|
|
|
}
|
24
|
|
|
|
25
|
|
|
return $properties;
|
26
|
|
|
}
|
27
|
|
|
|
28
|
|
|
protected function add($key, $item) {
|
29
|
|
|
$values = array();
|
30
|
|
|
|
31
|
|
|
if (array_key_exists($key, $this->properties)) {
|
32
|
|
|
$values = $this->properties[$key];
|
33
|
|
|
}
|
34
|
|
|
|
35
|
|
|
$values[] = $item;
|
36
|
|
|
|
37
|
|
|
$this->properties[$key] = $values;
|
38
|
|
|
|
39
|
|
|
return $this;
|
40
|
|
|
}
|
41
|
|
|
|
42
|
|
|
protected function getProperty($key) {
|
43
|
|
|
if ($this->hasProperty($key)) {
|
44
|
|
|
return $this->properties[$key];
|
45
|
|
|
}
|
46
|
|
|
|
47
|
|
|
return null;
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
protected function setProperty($key, $value) {
|
51
|
|
|
$this->properties[$key] = $value;
|
52
|
|
|
return $this;
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
protected function hasProperty($key) {
|
56
|
|
|
return array_key_exists($key, $this->properties);
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
public static function search($component,$class='\Ext\Base',$property=null,$propertyValue=null,$findFirst = false){
|
60
|
|
|
$return = [];
|
61
|
|
|
if(is_a($component, $class) and ($property==null or ($property!=null and $component->getProperty($property)==$propertyValue))){
|
|
|
|
|
62
|
|
|
if($findFirst){
|
63
|
|
|
return $component;
|
64
|
|
|
}else{
|
65
|
|
|
$return[] = $component;
|
66
|
|
|
}
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
if($foundComponents = static::searchArray($component->properties(),$class,$property,$propertyValue,$findFirst)){
|
70
|
|
|
if($findFirst){
|
71
|
|
|
return $foundComponents;
|
72
|
|
|
}else{
|
73
|
|
|
$return = array_merge($return,$foundComponents);
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
}
|
77
|
|
|
return $return;
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
protected static function searchArray($array,$class='\Ext\Base',$property=null,$propertyValue='',$findFirst = false){
|
81
|
|
|
$return = [];
|
82
|
|
|
foreach($array as $element){
|
83
|
|
|
$foundComponent = null;
|
84
|
|
|
if($element instanceof SerializableObject){
|
85
|
|
|
$foundComponent = static::search($element,$class,$property,$propertyValue,$findFirst);
|
86
|
|
|
}elseif(is_array($element)){
|
87
|
|
|
$foundComponent = static::searchArray($element,$class,$property,$propertyValue,$findFirst);
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
if($foundComponent) {
|
91
|
|
|
if ($findFirst) {
|
92
|
|
|
$return = $foundComponent;
|
93
|
|
|
break;
|
94
|
|
|
} else {
|
95
|
|
|
$return = array_merge($return, $foundComponent);
|
96
|
|
|
}
|
97
|
|
|
}
|
98
|
|
|
}
|
99
|
|
|
return $return;
|
100
|
|
|
}
|
101
|
|
|
|
102
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.