1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\contents\normalizers; |
4
|
|
|
|
5
|
|
|
use Ubiquity\exceptions\NormalizerException; |
6
|
|
|
use Ubiquity\cache\CacheManager; |
7
|
|
|
use Ubiquity\utils\base\UArray; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Normalize objects and arrays of objects |
11
|
|
|
* |
12
|
|
|
* Ubiquity\contents\normalizers$NormalizersManager |
13
|
|
|
* This class is part of Ubiquity |
14
|
|
|
* |
15
|
|
|
* @author jcheron <[email protected]> |
16
|
|
|
* @version 1.0.1 |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
class NormalizersManager { |
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @var array|mixed |
23
|
|
|
*/ |
24
|
|
|
private static $normalizers = [ ]; |
25
|
|
|
private static $key = "contents/normalizers"; |
26
|
|
|
|
27
|
|
|
public static function start() { |
28
|
|
|
if (CacheManager::$cache->exists ( self::$key )) { |
29
|
|
|
self::$normalizers = CacheManager::$cache->fetch ( self::$key ); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public static function registerClass($classname, $normalizer, $constructorParameters = []) { |
34
|
1 |
|
$reflect = new \ReflectionClass ( $normalizer ); |
35
|
1 |
|
self::$normalizers [$classname] = $reflect->newInstanceArgs ( $constructorParameters ); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
1 |
|
public static function registerClasses($classesAndNormalizers, ...$constructorParameters) { |
39
|
1 |
|
foreach ( $classesAndNormalizers as $class => $normalizer ) { |
40
|
1 |
|
self::registerClass ( $class, $normalizer, $constructorParameters ); |
41
|
|
|
} |
42
|
1 |
|
} |
43
|
|
|
|
44
|
1 |
|
public static function getNormalizer($classname) { |
45
|
1 |
|
if (isset ( self::$normalizers [$classname] )) { |
46
|
1 |
|
return self::$normalizers [$classname]; |
47
|
|
|
} |
48
|
|
|
throw new NormalizerException ( $classname . "has no serializer. Use NormalizersManager::registerClass to associate a new serializer." ); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public static function normalizeArray(array $datas, NormalizerInterface $normalizer) { |
52
|
1 |
|
$result = [ ]; |
53
|
1 |
|
foreach ( $datas as $object ) { |
54
|
1 |
|
if ($normalizer->supportsNormalization ( $object )) { |
55
|
1 |
|
$result [] = $normalizer->normalize ( $object ); |
56
|
|
|
} |
57
|
|
|
} |
58
|
1 |
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public static function normalizeArray_(array $datas) { |
62
|
1 |
|
if (sizeof ( $datas ) > 0) { |
63
|
1 |
|
$normalizer = self::getNormalizer ( get_class ( current ( $datas ) ) ); |
64
|
1 |
|
if (isset ( $normalizer )) { |
65
|
1 |
|
return self::normalizeArray ( $datas, $normalizer ); |
66
|
|
|
} |
67
|
|
|
} |
68
|
1 |
|
return [ ]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function normalize($object, NormalizerInterface $normalizer) { |
72
|
|
|
if ($normalizer->supportsNormalization ( $object )) { |
73
|
|
|
return $normalizer->normalize ( $object ); |
74
|
|
|
} |
75
|
|
|
throw new NormalizerException ( get_class ( $object ) . " does not supports " . get_class ( $normalizer ) . " normalization." ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function normalize_($object) { |
79
|
|
|
$normalizer = self::getNormalizer ( get_class ( $object ) ); |
80
|
|
|
if (isset ( $normalizer )) { |
81
|
|
|
return self::normalize ( $object, $normalizer ); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function store() { |
86
|
|
|
CacheManager::$cache->store ( self::$key, "return " . UArray::asPhpArray ( self::$normalizers, 'array' ) . ';' ); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|