Test Failed
Push — master ( 28ea53...a47f4f )
by Jean-Christophe
23:07 queued 18s
created

TransformersManager::transformInstances()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 19
rs 9.2222
ccs 0
cts 0
cp 0
cc 6
nc 6
nop 2
crap 42
1
<?php
2
3
namespace Ubiquity\contents\transformation;
4
5
use Ubiquity\cache\CacheManager;
6
use Ubiquity\contents\transformation\transformers\Boolean;
7
use Ubiquity\contents\transformation\transformers\DateTime;
8
use Ubiquity\contents\transformation\transformers\FirstUpperCase;
9
use Ubiquity\contents\transformation\transformers\HidePassword;
10
use Ubiquity\contents\transformation\transformers\LowerCase;
11
use Ubiquity\contents\transformation\transformers\Md5;
12
use Ubiquity\contents\transformation\transformers\Password;
13
use Ubiquity\contents\transformation\transformers\UpperCase;
14
use Ubiquity\orm\DAO;
15
use Ubiquity\orm\OrmUtils;
16
17
/**
18
 * Transform objects after loading
19
 *
20
 * Ubiquity\contents\transformation\transformers$TransformersManager
21
 * This class is part of Ubiquity
22
 *
23
 * @author jcheron <[email protected]>
24
 * @version 1.0.5
25
 *
26
 */
27
class TransformersManager {
28
    const TRANSFORMER_TYPES = ['reverse' => TransformerInterface::class, 'transform' => TransformerInterface::class, 'toView' => TransformerViewInterface::class, 'toForm' => TransformerFormInterface::class];
29
    /**
30
     *
31
     * @var array|mixed
32
     */
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
36
    /**
37
     * Load all transformers.
38
     * Do not use at runtime !
39
     */
40 47
    public static function start() {
41 47
        if (CacheManager::$cache->exists(self::$key)) {
42
            self::$transformers = \array_merge(self::$transformers, CacheManager::$cache->fetch(self::$key));
43
        }
44 47
        if (\class_exists('\\Ubiquity\\security\\csrf\\CsrfManager')) {
45
            self::$transformers ['crypt'] = '\\Ubiquity\\contents\\transformation\\transformers\\Crypt';
46
        }
47
    }
48
49
    /**
50
     * Start the manager for production.
51
     *
52
     * @param ?string $op
53
     */
54 4
    public static function startProd(?string $op = 'transform'): void {
55 4
        DAO::$useTransformers = true;
56 4
        DAO::$transformerOp = $op;
57
    }
58
59
    /**
60
     * Register a new transformer class.
61
     * Do not used at runtime !
62
     *
63
     * @param string $transformer
64
     * @param string $classname
65
     */
66
    public static function registerClass(string $transformer, string $classname): void {
67
        self::$transformers [$transformer] = $classname;
68
    }
69
70
    /**
71
     * Register and save in cache a new Transformer.
72
     * Do not used at runtime !
73
     *
74
     * @param string $transformer
75
     * @param string $classname
76
     */
77
    public static function registerClassAndSave(string $transformer, string $classname): void {
78
        self::start();
79
        self::registerClass($transformer, $classname);
80
        self::store();
81
    }
82
83
    /**
84
     * Register an associative array of transformers based on ['name'=>'transformerClass'].
85
     * Do not used at runtime !
86
     *
87
     * @param array $transformersAndClasses
88
     */
89
    public static function registerClasses(array $transformersAndClasses): void {
90
        foreach ($transformersAndClasses as $transformer => $class) {
91
            self::registerClass($transformer, $class);
92
        }
93
    }
94
95
    /**
96
     * Register and save an associative array of transformers based on ['name'=>'transformerClass'].
97
     * Do not used at runtime !
98
     *
99
     * @param array $transformersAndClasses
100
     */
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
109
    /**
110
     * Return the class from a transformer name.
111
     *
112
     * @param string $transformer
113
     * @return ?string
114
     */
115 16
    public static function getTransformerClass(string $transformer): ?string {
116 16
        if (isset (self::$transformers [$transformer])) {
117 16
            return self::$transformers [$transformer];
118
        }
119
        return null;
120
    }
121
122
    /**
123
     * Transform a member of an instance.
124
     *
125
     * @param object $instance
126
     * @param string $member
127
     * @param ?string $transform
128
     * @return ?mixed
129
     */
130 1
    public static function transform(object $instance, string $member, ?string $transform = 'transform') {
131 1
        $getter = 'get' . $member;
132 1
        if (\method_exists($instance, $getter)) {
133 1
            return self::applyTransformer($instance, $member, $instance->{$getter} (), $transform);
134
        }
135
        return null;
136
    }
137
138
    /**
139
     * Apply a transformer using a member transformer(s) on a value.
140
     *
141
     * @param object $instance
142
     * @param string $member
143
     * @param mixed $value
144
     * @param ?string $transform
145
     * @return mixed
146
     */
147 1
    public static function applyTransformer(object $instance, string $member, $value, ?string $transform = 'transform') {
148 1
        $class = \get_class($instance);
149 1
        $metas = OrmUtils::getModelMetadata($class);
150 1
        if (isset ($metas ['#transformers'] [$transform] [$member])) {
151 1
            $transformer = $metas ['#transformers'] [$transform] [$member];
152 1
            return $transformer::$transform ($value);
153
        }
154
        return $value;
155
    }
156
157
    /**
158
     * Transform all the members of a model instance.
159
     *
160
     * @param object $instance
161
     * @param string $transform
162
     */
163 1
    public static function transformInstance(object $instance, $transform = 'transform'): void {
164 1
        $class = \get_class($instance);
165 1
        $metas = OrmUtils::getModelMetadata($class);
166 1
        $transformers = $metas ['#transformers'] [$transform] ?? [];
167 1
        foreach ($transformers as $member => $transformer) {
168 1
            $getter = 'get' . ucfirst($member);
169 1
            $setter = 'set' . ucfirst($member);
170 1
            if (\method_exists($instance, $getter)) {
171 1
                $value = $transformer::$transform ($instance->{$getter} ());
172 1
                if (\method_exists($instance, $setter)) {
173 1
                    $instance->{$setter} ($value);
174
                }
175 1
                $instance->_rest [$member] = $value;
176
            }
177
        }
178
    }
179
180
    /**
181
     * Transform all the members of a model instances.
182
     *
183
     * @param array $instances
184
     * @param string $transform
185
     */
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
210
    /**
211
     * Store the loaded transformers in cache.
212
     */
213
    public static function store(): void {
214
        CacheManager::$cache->store(self::$key, self::$transformers);
215
    }
216
}
217
218