|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use \Common\ContainerMagic; |
|
4
|
|
|
use \Common\IPropertyContainer; |
|
5
|
|
|
|
|
6
|
|
|
class V2PropertyContainer extends ContainerMagic implements IPropertyContainer { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Property descriptions |
|
10
|
|
|
* |
|
11
|
|
|
* @var array[] |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $properties = array(); |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Array of accessors - getters/setters/etc |
|
17
|
|
|
* |
|
18
|
|
|
* Getter is a callable like |
|
19
|
|
|
* function ($value) use ($that) {} |
|
20
|
|
|
* |
|
21
|
|
|
* Setter is a callable like |
|
22
|
|
|
* function () use ($that) {} |
|
23
|
|
|
* |
|
24
|
|
|
* Importer is a callable like |
|
25
|
|
|
* function (&$row) use ($this) {} |
|
26
|
|
|
* |
|
27
|
|
|
* Exporter is a callable like |
|
28
|
|
|
* function (&$row) use ($this) {} |
|
29
|
|
|
* |
|
30
|
|
|
* @var callable[][] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $accessors; |
|
33
|
|
|
|
|
34
|
|
|
public function setProperties($properties) { |
|
35
|
|
|
$this->properties = $properties; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Is container contains no data |
|
40
|
|
|
* |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
|
|
public function isEmpty() { |
|
44
|
|
|
return empty($this->values); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function assignAccessor($varName, $type, $callable) { |
|
48
|
|
|
if (empty($callable)) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (is_callable($callable)) { |
|
53
|
|
|
$this->accessors[$type][$varName] = $callable; |
|
54
|
|
|
} else { |
|
55
|
|
|
throw new Exception('Error assigning callable in ' . get_called_class() . '! Callable typed [' . $type . '] is not a callable or not accessible in the scope'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
public function __set($name, $value) { |
|
|
|
|
|
|
60
|
|
|
if (is_callable($this->accessors[P_CONTAINER_SETTER][$name])) { |
|
61
|
|
|
call_user_func($this->accessors[P_CONTAINER_SETTER][$name], $value); |
|
62
|
|
|
} else { |
|
63
|
|
|
$this->values[$name] = $value; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
View Code Duplication |
public function __get($name) { |
|
|
|
|
|
|
68
|
|
|
if (is_callable($this->accessors[P_CONTAINER_GETTER][$name])) { |
|
69
|
|
|
return call_user_func($this->accessors[P_CONTAINER_GETTER][$name]); |
|
70
|
|
|
} else { |
|
71
|
|
|
return $this->values[$name]; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
public function importRow($row) { |
|
|
|
|
|
|
76
|
|
|
if(empty($row)) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// TODO - reset container ? |
|
81
|
|
|
|
|
82
|
|
|
foreach ($this->properties as $propertyName => $propertyData) { |
|
83
|
|
|
if (is_callable($this->accessors[P_CONTAINER_IMPORTER][$propertyName])) { |
|
84
|
|
|
call_user_func($this->accessors[P_CONTAINER_IMPORTER][$propertyName], &$row); |
|
85
|
|
|
} elseif (!empty($propertyData[P_DB_FIELD])) { |
|
86
|
|
|
$this->$propertyName = $row[$propertyData[P_DB_FIELD]]; |
|
87
|
|
|
} |
|
88
|
|
|
// Otherwise it's internal field - filled and used internally |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
public function exportRow() { |
|
|
|
|
|
|
93
|
|
|
$row = array(); |
|
94
|
|
|
|
|
95
|
|
|
foreach ($this->properties as $propertyName => $propertyData) { |
|
96
|
|
|
if (is_callable($this->accessors[P_CONTAINER_EXPORTER][$propertyName])) { |
|
97
|
|
|
call_user_func($this->accessors[P_CONTAINER_EXPORTER][$propertyName], &$row); |
|
98
|
|
|
} elseif (!empty($propertyData[P_DB_FIELD])) { |
|
99
|
|
|
$row[$propertyData[P_DB_FIELD]] = $this->$propertyName; |
|
100
|
|
|
} |
|
101
|
|
|
// Otherwise it's internal field - filled and used internally |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $row; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.