-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayExtensions.php
212 lines (163 loc) · 6.57 KB
/
ArrayExtensions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
use Phalcon\Cache\Frontend\Data;
use Phalcon\Cache\Backend\Memory;
use Phalcon\Cache\Backend\File;
/**
* Class ArrayExtensions
* @author Jean-François CAMBOT
* @version 1.0.3
*/
class ArrayExtensions
{
private $pattern = "#(@[a-zA-Z]+\s*[a-zA-Z0-9, ()_].*)#";
private $session_save_annotations = 'SaveArrayExtensionsAnnotations_';
private $cache = null;
public function __construct() {
}
/**
* Permet de paramétrer un type de cache avant utilisation, sinon Memory par défaut
* @param string $typeCache Memory ou File
* @param int $lifetime
* @param string $cacheDir ne sera utilisé que sur le cache fichier
*/
public function activeCache($typeCache = 'Memory', $lifetime = 172800, $cacheDir = APP_PATH . '/cache/')
{
$frontCache = new Data(['lifetime' => $lifetime]);
if ($typeCache == 'Memory')
$this->cache = new Memory($frontCache);
else if ($typeCache == 'File')
$this->cache = new File($frontCache, ['cacheDir' => $cacheDir]);
}
/**
* Transforme le tableau $arr en objet $object en typant fortement et récursivement les variables qui ont été annotées
* @param array $arr
* @param $object mixed
* @return mixed
*/
public function toObject(array $arr, $object)
{
if ($this->cache == null)
$this->activeCache();
$annotations = $this->getClassAnnotations($object);
foreach($annotations as $annotation)
{
if (isset($arr[$annotation['name']]))
{
switch( strtolower( $annotation['typage']))
{
case 'string' : $object->{$annotation['name']} = (string) $arr[$annotation['name']]; break;
case 'int' :
case 'integer' : $object->{$annotation['name']} = (int) $arr[$annotation['name']]; break;
case 'bool' :
case 'boolean' : $object->{$annotation['name']} = (bool) $arr[$annotation['name']]; break;
case 'float' : $object->{$annotation['name']} = (float) $arr[$annotation['name']]; break;
case 'double' : $object->{$annotation['name']} = (double) $arr[$annotation['name']]; break;
case 'datetime' :
if ($annotation['format'] == null)
throw new Exception('La propriété @var DateTime attend également un attribut @format format_date');
$format = trim($annotation['format']);
$date = $arr[$annotation['name']];
$d = DateTime::createFromFormat($format, $date);
$object->{$annotation['name']} = ($d && $d->format($format) == $date) ? $d : null;
break;
// tableau d'objets
case ( strlen(strstr($annotation['typage'],'[]')) > 0) :
$objectName = str_replace('[]', '', $annotation['typage']);
foreach($arr[$annotation['name']] as $item)
{
$object->{$annotation['name']}[] = $this->toObject($item , new $objectName() );
}
break;
// autre objet...
default:
$object->{$annotation['name']} = $this->toObject( $arr[$annotation['name']] , new $annotation['typage']() );
}
}
}
return (method_exists($object, 'render')) ? $object->render() : $object;
}
/**
* Simple raccourci qui va prendre le POST complet pour tenter de le transformer dans l'objet
* @param $object
* @return mixed
*/
public function postToObject($object)
{
return $this->toObject($_POST, $object);
}
/**
* Retourne un tableau avec les annotations contenant les types des variables dans @var
* @param $class mixed
* @return array
*/
private function getClassAnnotations($class)
{
$cacheName = $this->session_save_annotations.get_class($class);
if ($this->cache->exists($cacheName))
return $this->cache->get($cacheName);
$r = new ReflectionObject($class);
$pros = $r->getProperties(ReflectionProperty::IS_PUBLIC);
$annotations = array();
foreach ($pros as $pr)
{
preg_match_all($this->pattern, $pr->getDocComment(),$matches, PREG_PATTERN_ORDER );
$searchAttribute = null;
$formatAttribute = null;
foreach($matches[0] as $attribute)
{
if (substr($attribute, 0, 4 ) === '@var')
$searchAttribute = $attribute;
else if (substr($attribute, 0, 7 ) === '@format')
$formatAttribute = $attribute;
}
if ($searchAttribute != null)
{
$t = explode(" ", $searchAttribute);
$c = ($formatAttribute != null) ? explode(" ", $formatAttribute) : array();
if (isset($c[1]))
{
array_shift($c);
$c = implode(' ', $c);
}
else
$c = null;
if (isset($t[0]) && trim($t[0]) == '@var' ) {
$annotations[] = array('name' => $pr->getName(), 'typage' => trim($t[1]), 'format' => $c );
}
}
}
$this->cache->save($cacheName, $annotations);
return $annotations;
}
/**
* Retourne récursivement un objet en tableau, sans cast
* Méthode récupérée sur la doc PHP
*
* !! from PHP DOC, not use in this class form moment !!
*
* @param $object
* @param bool $assoc
* @param string $empty
* @return array
*/
public function toArrayRecursive($object, $assoc = true, $empty='')
{
$out_arr = array();
$assoc = (!empty($assoc));
if (!empty($object)) {
$arrObj = is_object($object) ? get_object_vars($object) : $object;
$i=0;
foreach ($arrObj as $key => $val) {
$akey = ($assoc !== FALSE) ? $key : $i;
if (is_array($val) || is_object($val)) {
$out_arr[$key] = (empty($val)) ? $empty : $this->toArrayRecursive($val);
}
else {
$out_arr[$key] = (empty($val)) ? $empty : (string)$val;
}
$i++;
}
}
return $out_arr;
}
}