Skip to content

Commit 40069b9

Browse files
committed
[content][skip ci] add HidePassword transformer
1 parent ced9304 commit 40069b9

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

src/Ubiquity/contents/transformation/TransformersManager.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
namespace Ubiquity\contents\transformation;
44

55
use Ubiquity\cache\CacheManager;
6+
use Ubiquity\contents\transformation\transformers\Boolean;
67
use Ubiquity\contents\transformation\transformers\DateTime;
78
use Ubiquity\contents\transformation\transformers\FirstUpperCase;
9+
use Ubiquity\contents\transformation\transformers\HidePassword;
810
use Ubiquity\contents\transformation\transformers\LowerCase;
911
use Ubiquity\contents\transformation\transformers\Md5;
1012
use Ubiquity\contents\transformation\transformers\Password;
1113
use Ubiquity\contents\transformation\transformers\UpperCase;
1214
use Ubiquity\orm\DAO;
1315
use Ubiquity\orm\OrmUtils;
14-
use Ubiquity\utils\base\UArray;
15-
use Ubiquity\contents\transformation\transformers\Boolean;
1616

1717
/**
1818
* Transform objects after loading
@@ -25,23 +25,23 @@
2525
*
2626
*/
2727
class TransformersManager {
28-
const TRANSFORMER_TYPES = [ 'reverse' => TransformerInterface::class,'transform' => TransformerInterface::class,'toView' => TransformerViewInterface::class,'toForm' => TransformerFormInterface::class ];
28+
const TRANSFORMER_TYPES = ['reverse' => TransformerInterface::class, 'transform' => TransformerInterface::class, 'toView' => TransformerViewInterface::class, 'toForm' => TransformerFormInterface::class];
2929
/**
3030
*
3131
* @var array|mixed
3232
*/
33-
private static $transformers = [ 'md5' => Md5::class,'datetime' => DateTime::class,'upper' => UpperCase::class,'firstUpper' => FirstUpperCase::class,'lower' => LowerCase::class,'password' => Password::class,'boolean'=>Boolean::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];
3434
private static $key = 'contents/transformers';
3535

3636
/**
3737
* Load all transformers.
3838
* Do not use at runtime !
3939
*/
4040
public static function start() {
41-
if (CacheManager::$cache->exists ( self::$key )) {
42-
self::$transformers = \array_merge ( self::$transformers, CacheManager::$cache->fetch ( self::$key ) );
41+
if (CacheManager::$cache->exists(self::$key)) {
42+
self::$transformers = \array_merge(self::$transformers, CacheManager::$cache->fetch(self::$key));
4343
}
44-
if (\class_exists ( '\\Ubiquity\\security\\csrf\\CsrfManager' )) {
44+
if (\class_exists('\\Ubiquity\\security\\csrf\\CsrfManager')) {
4545
self::$transformers ['crypt'] = '\\Ubiquity\\contents\\transformation\\transformers\\Crypt';
4646
}
4747
}
@@ -75,9 +75,9 @@ public static function registerClass(string $transformer, string $classname): vo
7575
* @param string $classname
7676
*/
7777
public static function registerClassAndSave(string $transformer, string $classname): void {
78-
self::start ();
79-
self::registerClass ( $transformer, $classname );
80-
self::store ();
78+
self::start();
79+
self::registerClass($transformer, $classname);
80+
self::store();
8181
}
8282

8383
/**
@@ -87,8 +87,8 @@ public static function registerClassAndSave(string $transformer, string $classna
8787
* @param array $transformersAndClasses
8888
*/
8989
public static function registerClasses(array $transformersAndClasses): void {
90-
foreach ( $transformersAndClasses as $transformer => $class ) {
91-
self::registerClass ( $transformer, $class );
90+
foreach ($transformersAndClasses as $transformer => $class) {
91+
self::registerClass($transformer, $class);
9292
}
9393
}
9494

@@ -99,11 +99,11 @@ public static function registerClasses(array $transformersAndClasses): void {
9999
* @param array $transformersAndClasses
100100
*/
101101
public static function registerClassesAndSave(array $transformersAndClasses): void {
102-
self::start ();
103-
foreach ( $transformersAndClasses as $transformer => $class ) {
104-
self::registerClass ( $transformer, $class );
102+
self::start();
103+
foreach ($transformersAndClasses as $transformer => $class) {
104+
self::registerClass($transformer, $class);
105105
}
106-
self::store ();
106+
self::store();
107107
}
108108

109109
/**
@@ -113,7 +113,7 @@ public static function registerClassesAndSave(array $transformersAndClasses): vo
113113
* @return ?string
114114
*/
115115
public static function getTransformerClass(string $transformer): ?string {
116-
if (isset ( self::$transformers [$transformer] )) {
116+
if (isset (self::$transformers [$transformer])) {
117117
return self::$transformers [$transformer];
118118
}
119119
return null;
@@ -129,8 +129,8 @@ public static function getTransformerClass(string $transformer): ?string {
129129
*/
130130
public static function transform(object $instance, string $member, ?string $transform = 'transform') {
131131
$getter = 'get' . $member;
132-
if (\method_exists ( $instance, $getter )) {
133-
return self::applyTransformer ( $instance, $member, $instance->{$getter} (), $transform );
132+
if (\method_exists($instance, $getter)) {
133+
return self::applyTransformer($instance, $member, $instance->{$getter} (), $transform);
134134
}
135135
return null;
136136
}
@@ -145,11 +145,11 @@ public static function transform(object $instance, string $member, ?string $tran
145145
* @return mixed
146146
*/
147147
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] )) {
148+
$class = \get_class($instance);
149+
$metas = OrmUtils::getModelMetadata($class);
150+
if (isset ($metas ['#transformers'] [$transform] [$member])) {
151151
$transformer = $metas ['#transformers'] [$transform] [$member];
152-
return $transformer::$transform ( $value );
152+
return $transformer::$transform ($value);
153153
}
154154
return $value;
155155
}
@@ -161,16 +161,16 @@ public static function applyTransformer(object $instance, string $member, $value
161161
* @param string $transform
162162
*/
163163
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 );
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);
174174
}
175175
$instance->_rest [$member] = $value;
176176
}
@@ -181,7 +181,7 @@ public static function transformInstance(object $instance, $transform = 'transfo
181181
* Store the loaded transformers in cache.
182182
*/
183183
public static function store(): void {
184-
CacheManager::$cache->store ( self::$key, self::$transformers );
184+
CacheManager::$cache->store(self::$key, self::$transformers);
185185
}
186186
}
187187

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Ubiquity\contents\transformation\transformers;
4+
5+
use Ubiquity\contents\transformation\TransformerViewInterface;
6+
7+
/**
8+
* Hide a password.
9+
* Ubiquity\contents\transformation\transformers$HidePassword
10+
* This class is part of Ubiquity
11+
*
12+
* @author jcheron <[email protected]>
13+
* @version 1.0.0
14+
* @since 2.4.6
15+
*/
16+
class HidePassword implements TransformerViewInterface {
17+
18+
public static function toView($value) {
19+
return '';
20+
}
21+
}

0 commit comments

Comments
 (0)