lekoala /
silverstripe-encrypt
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\Encrypt; |
||
| 4 | |||
| 5 | use SilverStripe\Model\ModelData; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * This trait allow encryption for fields that don't |
||
| 9 | * require a blind index |
||
| 10 | */ |
||
| 11 | trait HasEncryption |
||
| 12 | { |
||
| 13 | use HasBaseEncryption; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * prepValueForDB gets passed $this->value |
||
| 17 | * |
||
| 18 | * @param string $value |
||
| 19 | * @return string |
||
| 20 | */ |
||
| 21 | public function prepValueForDB(mixed $value): array|string|null |
||
| 22 | { |
||
| 23 | if (!$value) { |
||
| 24 | if ($this->getNullifyEmpty() || $value === null) { |
||
| 25 | return null; |
||
| 26 | } |
||
| 27 | return ''; |
||
| 28 | } |
||
| 29 | // Don't encrypt twice |
||
| 30 | if (EncryptHelper::isEncrypted($value)) { |
||
| 31 | return $value; |
||
| 32 | } |
||
| 33 | $aad = $this->encryptionAad; |
||
| 34 | $encryptedValue = $this->getEncryptedField()->encryptValue($value, $aad); |
||
| 35 | return $encryptedValue; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function setValue(mixed $value, null|array|ModelData $record = null, bool $markChanged = true): static |
||
|
0 ignored issues
–
show
|
|||
| 39 | { |
||
| 40 | $this->setEncryptionAad($record); |
||
| 41 | |||
| 42 | // Return early if we keep encrypted value in memory |
||
| 43 | if (!EncryptHelper::getAutomaticDecryption()) { |
||
| 44 | $this->value = $value; |
||
| 45 | return $this; |
||
| 46 | } |
||
| 47 | |||
| 48 | // $markChanged is not used |
||
| 49 | // The value might come encrypted from the database |
||
| 50 | if ($value && EncryptHelper::isEncrypted($value)) { |
||
| 51 | $this->value = $this->decryptValue($value); |
||
| 52 | } else { |
||
| 53 | $this->value = $value; |
||
| 54 | } |
||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | } |
||
| 58 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.