Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
TransformersManager.php
Go to the documentation of this file.
1<?php
2
4
16
28 const TRANSFORMER_TYPES = ['reverse' => TransformerInterface::class, 'transform' => TransformerInterface::class, 'toView' => TransformerViewInterface::class, 'toForm' => TransformerFormInterface::class];
33 private static $transformers = ['md5' => Md5::class, 'datetime' => DateTime::class, 'upper' => UpperCase::class, 'firstUpper' => FirstUpperCase::class, 'lower' => LowerCase::class, 'password' => Password::class, 'hpassword' => HidePassword::class, 'boolean' => Boolean::class];
34 private static $key = 'contents/transformers';
35
40 public static function start() {
41 if (CacheManager::$cache->exists(self::$key)) {
42 self::$transformers = \array_merge(self::$transformers, CacheManager::$cache->fetch(self::$key));
43 }
44 if (\class_exists('\\Ubiquity\\security\\csrf\\CsrfManager')) {
45 self::$transformers ['crypt'] = '\\Ubiquity\\contents\\transformation\\transformers\\Crypt';
46 }
47 }
48
54 public static function startProd(?string $op = 'transform'): void {
55 DAO::$useTransformers = true;
56 DAO::$transformerOp = $op;
57 }
58
66 public static function registerClass(string $transformer, string $classname): void {
67 self::$transformers [$transformer] = $classname;
68 }
69
77 public static function registerClassAndSave(string $transformer, string $classname): void {
78 self::start();
79 self::registerClass($transformer, $classname);
80 self::store();
81 }
82
89 public static function registerClasses(array $transformersAndClasses): void {
90 foreach ($transformersAndClasses as $transformer => $class) {
91 self::registerClass($transformer, $class);
92 }
93 }
94
101 public static function registerClassesAndSave(array $transformersAndClasses): void {
102 self::start();
103 foreach ($transformersAndClasses as $transformer => $class) {
104 self::registerClass($transformer, $class);
105 }
106 self::store();
107 }
108
115 public static function getTransformerClass(string $transformer): ?string {
116 if (isset (self::$transformers [$transformer])) {
117 return self::$transformers [$transformer];
118 }
119 return null;
120 }
121
130 public static function transform(object $instance, string $member, ?string $transform = 'transform') {
131 $getter = 'get' . $member;
132 if (\method_exists($instance, $getter)) {
133 return self::applyTransformer($instance, $member, $instance->{$getter} (), $transform);
134 }
135 return null;
136 }
137
147 public static function applyTransformer(object $instance, string $member, $value, ?string $transform = 'transform') {
148 $class = \get_class($instance);
149 $metas = OrmUtils::getModelMetadata($class);
150 if (isset ($metas ['#transformers'] [$transform] [$member])) {
151 $transformer = $metas ['#transformers'] [$transform] [$member];
152 return $transformer::$transform ($value);
153 }
154 return $value;
155 }
156
163 public static function transformInstance(object $instance, $transform = 'transform'): void {
164 $class = \get_class($instance);
165 $metas = OrmUtils::getModelMetadata($class);
166 $transformers = $metas ['#transformers'] [$transform] ?? [];
167 foreach ($transformers as $member => $transformer) {
168 $getter = 'get' . ucfirst($member);
169 $setter = 'set' . ucfirst($member);
170 if (\method_exists($instance, $getter)) {
171 $value = $transformer::$transform ($instance->{$getter} ());
172 if (\method_exists($instance, $setter)) {
173 $instance->{$setter} ($value);
174 }
175 $instance->_rest [$member] = $value;
176 }
177 }
178 }
179
186 public static function transformInstances(array $instances, $transform = 'transform'): void {
187 if(\count($instances)===0){
188 return;
189 }
190 $instance = \current($instances);
191 $class = \get_class($instance);
192 $metas = OrmUtils::getModelMetadata($class);
193 $transformers = $metas ['#transformers'] [$transform] ?? [];
194
195 foreach ($transformers as $member => $transformer) {
196 $getter = 'get' . ucfirst($member);
197 $setter = 'set' . ucfirst($member);
198 foreach ($instances as $instance) {
199 if (\method_exists($instance, $getter)) {
200 $value = $transformer::$transform ($instance->{$getter} ());
201 if (\method_exists($instance, $setter)) {
202 $instance->{$setter} ($value);
203 }
204 $instance->_rest [$member] = $value;
205 }
206 }
207 }
208 }
209
213 public static function store(): void {
214 CacheManager::$cache->store(self::$key, self::$transformers);
215 }
216}
217
Manager for caches (Router, Rest, models).
static transformInstance(object $instance, $transform='transform')
Transform all the members of a model instance.
static registerClassAndSave(string $transformer, string $classname)
Register and save in cache a new Transformer.
static store()
Store the loaded transformers in cache.
static transformInstances(array $instances, $transform='transform')
Transform all the members of a model instances.
static getTransformerClass(string $transformer)
Return the class from a transformer name.
static applyTransformer(object $instance, string $member, $value, ?string $transform='transform')
Apply a transformer using a member transformer(s) on a value.
static registerClassesAndSave(array $transformersAndClasses)
Register and save an associative array of transformers based on ['name'=>'transformerClass'].
static registerClasses(array $transformersAndClasses)
Register an associative array of transformers based on ['name'=>'transformerClass'].
static transform(object $instance, string $member, ?string $transform='transform')
Transform a member of an instance.
static registerClass(string $transformer, string $classname)
Register a new transformer class.
static startProd(?string $op='transform')
Start the manager for production.
Transform a mysql date to a php DateTime.
Definition DateTime.php:18
Gateway class between database and object model.
Definition DAO.php:33
Object/relational mapping utilities.
Definition OrmUtils.php:17
Transformers managment.