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';
40 public static function start() {
41 if (CacheManager::$cache->exists(self::$key)) {
42 self::$transformers = \array_merge(self::$transformers, CacheManager::$cache->fetch(self::$key));
44 if (\class_exists(
'\\Ubiquity\\security\\csrf\\CsrfManager')) {
45 self::$transformers [
'crypt'] =
'\\Ubiquity\\contents\\transformation\\transformers\\Crypt';
54 public static function startProd(?
string $op =
'transform'): void {
55 DAO::$useTransformers = true;
56 DAO::$transformerOp = $op;
66 public static function registerClass(
string $transformer,
string $classname): void {
67 self::$transformers [$transformer] = $classname;
79 self::registerClass($transformer, $classname);
90 foreach ($transformersAndClasses as $transformer => $class) {
91 self::registerClass($transformer, $class);
103 foreach ($transformersAndClasses as $transformer => $class) {
104 self::registerClass($transformer, $class);
116 if (isset (self::$transformers [$transformer])) {
117 return self::$transformers [$transformer];
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);
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);
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);
175 $instance->_rest [$member] = $value;
187 if(\count($instances)===0){
190 $instance = \current($instances);
191 $class = \get_class($instance);
192 $metas = OrmUtils::getModelMetadata($class);
193 $transformers = $metas [
'#transformers'] [$transform] ?? [];
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);
204 $instance->_rest [$member] = $value;
213 public static function store(): void {
214 CacheManager::$cache->store(self::$key, self::$transformers);
Manager for caches (Router, Rest, models).
Transform objects after loading.
static start()
Load all transformers.
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.
Make a string first character uppercase.
Gateway class between database and object model.
Object/relational mapping utilities.