Test Failed
Pull Request — master (#23)
by Jean-Christophe
12:28
created

TransformersManager::registerClassesAndSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Ubiquity\contents\transformation;
4
5
use Ubiquity\cache\CacheManager;
6
use Ubiquity\contents\transformation\transformers\DateTime;
7
use Ubiquity\contents\transformation\transformers\FirstUpperCase;
8
use Ubiquity\contents\transformation\transformers\LowerCase;
9
use Ubiquity\contents\transformation\transformers\Md5;
10
use Ubiquity\contents\transformation\transformers\Password;
11
use Ubiquity\contents\transformation\transformers\UpperCase;
12
use Ubiquity\orm\DAO;
13
use Ubiquity\orm\OrmUtils;
14
use Ubiquity\utils\base\UArray;
15
16
/**
17
 * Transform objects after loading
18
 *
19
 * Ubiquity\contents\transformation\transformers$TransformersManager
20
 * This class is part of Ubiquity
21
 *
22
 * @author jcheron <[email protected]>
23
 * @version 1.0.1
24
 *
25
 */
26
class TransformersManager {
27
	const TRANSFORMER_TYPES = [ 'transform' => TransformerInterface::class,'toView' => TransformerViewInterface::class,'toForm' => TransformerFormInterface::class ];
28
	/**
29
	 *
30
	 * @var array|mixed
31
	 */
32
	private static $transformers = [ 'md5' => Md5::class,'datetime' => DateTime::class,'upper' => UpperCase::class,'firstUpper' => FirstUpperCase::class,'lower' => LowerCase::class,'password' => Password::class ];
33
	private static $key = "contents/transformers";
34
35
	/**
36
	 * Do not use at runtime !
37
	 */
38
	public static function start() {
39
		$transformers = [ ];
40
		if (CacheManager::$cache->exists ( self::$key )) {
41
			$transformers = CacheManager::$cache->fetch ( self::$key );
42
		}
43
		self::$transformers = array_merge ( self::$transformers, $transformers );
44
	}
45
46
	public static function startProd() {
47
		DAO::$useTransformers = true;
48
	}
49
50
	/**
51
	 * Do not used at runtime !
52
	 *
53
	 * @param string $transformer
54
	 * @param string $classname
55
	 */
56
	public static function registerClass($transformer, $classname) {
57
		self::$transformers [$transformer] = $classname;
58
	}
59
60
	/**
61
	 * Do not used at runtime !
62
	 *
63
	 * @param string $transformer
64
	 * @param string $classname
65
	 */
66
	public static function registerClassAndSave($transformer, $classname) {
67
		self::start ();
68
		self::registerClass ( $transformer, $classname );
69
		self::store ();
70
	}
71
72
	/**
73
	 * Do not used at runtime !
74
	 *
75
	 * @param array $transformersAndClasses
76
	 */
77
	public static function registerClasses($transformersAndClasses) {
78
		foreach ( $transformersAndClasses as $transformer => $class ) {
79
			self::registerClass ( $transformer, $class );
80
		}
81
	}
82
83
	/**
84
	 * Do not used at runtime !
85
	 *
86
	 * @param array $transformersAndClasses
87
	 */
88
	public static function registerClassesAndSave($transformersAndClasses) {
89
		self::start ();
90
		foreach ( $transformersAndClasses as $transformer => $class ) {
91
			self::registerClass ( $transformer, $class );
92
		}
93
		self::store ();
94
	}
95
96
	public static function getTransformerClass($transformer) {
97
		if (isset ( self::$transformers [$transformer] )) {
98
			return self::$transformers [$transformer];
99
		}
100
		return null;
101
	}
102
103
	public static function transform($instance, $member, $transform = 'transform') {
104
		$getter = 'get' . $member;
105
		if (method_exists ( $instance, $getter )) {
106
			return self::applyTransformer ( $instance, $member, $instance->{$getter} (), $transform );
107
		}
108
		return null;
109
	}
110
111
	public static function applyTransformer($instance, $member, $value, $transform = 'transform') {
112
		$class = get_class ( $instance );
113
		$metas = OrmUtils::getModelMetadata ( $class );
114
		if (isset ( $metas ['#transformers'] [$transform] [$member] )) {
115
			$transformer = $metas ['#transformers'] [$transform] [$member];
116
			return $transformer::$transform ( $value );
117
		}
118
		return $value;
119
	}
120
121
	public static function transformInstance($instance, $transform = 'transform') {
122
		$class = get_class ( $instance );
123
		$metas = OrmUtils::getModelMetadata ( $class );
124
		$transformers = $metas ['#transformers'] [$transform] ?? [ ];
125
		foreach ( $transformers as $member => $transformer ) {
126
			$getter = 'get' . ucfirst ( $member );
127
			$setter = 'set' . ucfirst ( $member );
128
			if (method_exists ( $instance, $getter )) {
129
				$value = $transformer::$transform ( $instance->{$getter} () );
130
				if (method_exists ( $instance, $setter )) {
131
					$instance->{$setter} ( $value );
132
				}
133
				$instance->_rest [$member] = $value;
134
			}
135
		}
136
	}
137
138
	public static function store() {
139
		CacheManager::$cache->store ( self::$key, "return " . UArray::asPhpArray ( self::$transformers, 'array' ) . ';' );
140
	}
141
}
142
143