Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
NormalizersManager.php
Go to the documentation of this file.
1<?php
2
4
7use Ubiquity\utils\base\UArray;
8
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 public static function registerClass($classname, $normalizer, $constructorParameters = [ ]) {
34 $reflect = new \ReflectionClass ( $normalizer );
35 self::$normalizers [$classname] = $reflect->newInstanceArgs ( $constructorParameters );
36 }
37
38 public static function registerClasses($classesAndNormalizers, ...$constructorParameters) {
39 foreach ( $classesAndNormalizers as $class => $normalizer ) {
40 self::registerClass ( $class, $normalizer, $constructorParameters );
41 }
42 }
43
44 public static function getNormalizer($classname) {
45 if (isset ( self::$normalizers [$classname] )) {
46 return self::$normalizers [$classname];
47 }
48 throw new NormalizerException ( $classname . "has no serializer. Use NormalizersManager::registerClass to associate a new serializer." );
49 }
50
51 public static function normalizeArray(array $datas, NormalizerInterface $normalizer) {
52 $result = [ ];
53 foreach ( $datas as $object ) {
54 if ($normalizer->supportsNormalization ( $object )) {
55 $result [] = $normalizer->normalize ( $object );
56 }
57 }
58 return $result;
59 }
60
61 public static function normalizeArray_(array $datas) {
62 if (count ( $datas ) > 0) {
63 $normalizer = self::getNormalizer ( get_class ( current ( $datas ) ) );
64 if (isset ( $normalizer )) {
65 return self::normalizeArray ( $datas, $normalizer );
66 }
67 }
68 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, self::$normalizers );
87 }
88}
89
Manager for caches (Router, Rest, models).
Normalize objects and arrays of objects.
static normalizeArray(array $datas, NormalizerInterface $normalizer)
static registerClass($classname, $normalizer, $constructorParameters=[])
static registerClasses($classesAndNormalizers,... $constructorParameters)
static normalize($object, NormalizerInterface $normalizer)
Defines the default behavior of a Normalizer Ubiquity\contents\normalizers$NormalizerInterface This c...