55use Exception ;
66use Madmatt \EncryptAtRest \Traits \EncryptedFieldGetValueTrait ;
77use SilverStripe \Core \Injector \Injector ;
8+ use SilverStripe \Model \ModelData ;
89use SilverStripe \ORM \DB ;
910use SilverStripe \ORM \FieldType \DBEnum ;
10- use SilverStripe \ORM \ArrayLib ;
11+ use SilverStripe \Core \ArrayLib ;
1112use Madmatt \EncryptAtRest \AtRestCryptoService ;
1213
1314/**
@@ -29,10 +30,12 @@ class EncryptedEnum extends DBEnum
2930 public function __construct ($ name = null , $ enum = null , $ default = 0 , $ options = [])
3031 {
3132 parent ::__construct ($ name , $ enum , $ default , $ options );
33+ // disable date validation to allow for empty value
34+ self ::config ()->set ('field_validators ' , []);
3235 $ this ->service = Injector::inst ()->get (AtRestCryptoService::class);
3336 }
3437
35- public function setValue ($ value , $ record = null , $ markChanged = true )
38+ public function setValue (mixed $ value , null | array | ModelData $ record = null , bool $ markChanged = true ): static
3639 {
3740 if (is_array ($ record ) && array_key_exists ($ this ->name , $ record ) && $ value === null ) {
3841 $ this ->value = $ record [$ this ->name ];
@@ -42,6 +45,8 @@ public function setValue($value, $record = null, $markChanged = true)
4245 } else {
4346 $ this ->value = $ value ;
4447 }
48+
49+ return $ this ;
4550 }
4651
4752 public function getDecryptedValue (string $ value = '' )
@@ -58,7 +63,7 @@ public function getDecryptedValue(string $value = '')
5863 return $ value ;
5964 }
6065
61- public function requireField ()
66+ public function requireField (): void
6267 {
6368 $ values = array (
6469 'type ' => 'text ' ,
@@ -72,26 +77,11 @@ public function requireField()
7277 DB ::require_field ($ this ->tableName , $ this ->name , $ values );
7378 }
7479
75- public function prepValueForDB ($ value )
80+ public function prepValueForDB (mixed $ value ): array | string | null
7681 {
7782 $ value = parent ::prepValueForDB ($ value );
7883 $ ciphertext = $ this ->service ->encrypt ($ value );
7984 $ this ->value = $ ciphertext ;
8085 return $ ciphertext ;
8186 }
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- }
9787}
0 commit comments