55use Exception ;
66use Madmatt \EncryptAtRest \Traits \EncryptedFieldGetValueTrait ;
77use SilverStripe \Core \Injector \Injector ;
8+ use SilverStripe \Core \Validation \FieldValidation \DatetimeFieldValidator ;
9+ use SilverStripe \Core \Validation \FieldValidation \OptionFieldValidator ;
10+ use SilverStripe \Model \ModelData ;
811use SilverStripe \ORM \DB ;
912use SilverStripe \ORM \FieldType \DBEnum ;
10- use SilverStripe \ORM \ArrayLib ;
13+ use SilverStripe \Core \ArrayLib ;
1114use Madmatt \EncryptAtRest \AtRestCryptoService ;
1215
1316/**
@@ -26,13 +29,21 @@ class EncryptedEnum extends DBEnum
2629 */
2730 protected $ service ;
2831
32+ /**
33+ * Disable validation added in CMS6 but todo in future release
34+ */
35+ private static array $ field_validators = [
36+ OptionFieldValidator::class => null ,
37+ ];
38+
2939 public function __construct ($ name = null , $ enum = null , $ default = 0 , $ options = [])
3040 {
3141 parent ::__construct ($ name , $ enum , $ default , $ options );
42+
3243 $ this ->service = Injector::inst ()->get (AtRestCryptoService::class);
3344 }
3445
35- public function setValue ($ value , $ record = null , $ markChanged = true )
46+ public function setValue (mixed $ value , null | array | ModelData $ record = null , bool $ markChanged = true ): static
3647 {
3748 if (is_array ($ record ) && array_key_exists ($ this ->name , $ record ) && $ value === null ) {
3849 $ this ->value = $ record [$ this ->name ];
@@ -42,6 +53,8 @@ public function setValue($value, $record = null, $markChanged = true)
4253 } else {
4354 $ this ->value = $ value ;
4455 }
56+
57+ return $ this ;
4558 }
4659
4760 public function getDecryptedValue (string $ value = '' )
@@ -58,7 +71,7 @@ public function getDecryptedValue(string $value = '')
5871 return $ value ;
5972 }
6073
61- public function requireField ()
74+ public function requireField (): void
6275 {
6376 $ values = array (
6477 'type ' => 'text ' ,
@@ -72,26 +85,11 @@ public function requireField()
7285 DB ::require_field ($ this ->tableName , $ this ->name , $ values );
7386 }
7487
75- public function prepValueForDB ($ value )
88+ public function prepValueForDB (mixed $ value ): array | string | null
7689 {
7790 $ value = parent ::prepValueForDB ($ value );
7891 $ ciphertext = $ this ->service ->encrypt ($ value );
7992 $ this ->value = $ ciphertext ;
8093 return $ ciphertext ;
8194 }
82-
83- /**
84- * Returns the values of this enum as an array, suitable for insertion into
85- * a {@link DropdownField}
86- *
87- * @param boolean
88- *
89- * @return array
90- */
91- public function enumValues ($ hasEmpty = true ) {
92- $ this ->enum = array ();
93- return ($ hasEmpty )
94- ? array_merge (array ('' => '' ), ArrayLib::valuekey ($ this ->enum ))
95- : ArrayLib::valuekey ($ this ->enum );
96- }
9795}
0 commit comments