|
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
|
|
|
class NormalizersManager { |
|
10
|
|
|
private static $normalizers=[]; |
|
11
|
|
|
private static $key="contents/normalizers"; |
|
12
|
|
|
|
|
13
|
|
|
public static function start(){ |
|
14
|
|
|
if(CacheManager::$cache->exists(self::$key)){ |
|
15
|
|
|
self::$normalizers=CacheManager::$cache->fetch(self::$key); |
|
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public static function registerClass($classname,$normalizer,$constructorParameters=[]){ |
|
20
|
|
|
$reflect=new \ReflectionClass($normalizer); |
|
21
|
|
|
self::$normalizers[$classname]=$reflect->newInstanceArgs($constructorParameters); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function registerClasses($classesAndNormalizers,...$constructorParameters){ |
|
25
|
|
|
foreach ($classesAndNormalizers as $class=>$normalizer){ |
|
26
|
|
|
self::registerClass($class, $normalizer,$constructorParameters); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function getNormalizer($classname){ |
|
31
|
|
|
if(isset(self::$normalizers[$classname])){ |
|
32
|
|
|
return self::$normalizers[$classname]; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function normalizeArray(array $datas,NormalizerInterface $normalizer){ |
|
37
|
|
|
$result=[]; |
|
38
|
|
|
foreach ($datas as $object){ |
|
39
|
|
|
if($normalizer->supportsNormalization($object)){ |
|
40
|
|
|
$result[]=$normalizer->normalize($object); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
return $result; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public static function normalizeArray_(array $datas){ |
|
47
|
|
|
if(sizeof($datas)>0){ |
|
48
|
|
|
$normalizer=self::getNormalizer(get_class($datas[0])); |
|
49
|
|
|
return self::normalizeArray($datas, $normalizer); |
|
50
|
|
|
} |
|
51
|
|
|
return []; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function normalize($object,NormalizerInterface $normalizer){ |
|
55
|
|
|
if($normalizer->supportsNormalization($object)){ |
|
56
|
|
|
return $normalizer->normalize($object); |
|
57
|
|
|
} |
|
58
|
|
|
throw new NormalizerException(get_class($object)." does not supports ".get_class($normalizer)." normalization."); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function normalize_($object){ |
|
62
|
|
|
return self::normalize($object, self::getNormalizer(get_class($object))); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public static function store(){ |
|
66
|
|
|
CacheManager::$cache->store(self::$key, "return ".UArray::asPhpArray(self::$normalizers,'array').';'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..