Completed
Push — master ( 8a65c2...f8bd36 )
by Jean-Christophe
01:34
created

NormalizersManager::normalizeArray_()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Ubiquity\cache\CacheMan...ache->fetch(self::$key) of type * is incompatible with the declared type array of property $normalizers.

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..

Loading history...
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