Skip to content

Commit f49250f

Browse files
committed
Implement GetValue function via a trait
Reduces duplication
1 parent e148e60 commit f49250f

File tree

7 files changed

+46
-52
lines changed

7 files changed

+46
-52
lines changed

src/FieldType/EncryptedDatetime.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Madmatt\EncryptAtRest\FieldType;
44

55
use Exception;
6+
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
67
use SilverStripe\Core\Injector\Injector;
78
use SilverStripe\ORM\DB;
89
use SilverStripe\ORM\FieldType\DBDatetime;
@@ -17,6 +18,9 @@
1718
*/
1819
class EncryptedDatetime extends DBDatetime
1920
{
21+
22+
use EncryptedFieldGetValueTrait;
23+
2024
/**
2125
* @var AtRestCryptoService
2226
*/
@@ -40,10 +44,8 @@ public function setValue($value, $record = null, $markChanged = true)
4044
}
4145
}
4246

43-
public function getDecryptedValue($value)
47+
public function getDecryptedValue(string $value = '')
4448
{
45-
// Type hardening for PHP 8.1+
46-
$value = (string)$value;
4749
// Test if we're actually an encrypted value;
4850
if (ctype_xdigit($value) && strlen($value) > 130) {
4951
try {
@@ -56,11 +58,6 @@ public function getDecryptedValue($value)
5658
return $value;
5759
}
5860

59-
public function getValue()
60-
{
61-
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
62-
}
63-
6461
public function requireField()
6562
{
6663
$values = array(

src/FieldType/EncryptedDecimal.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Madmatt\EncryptAtRest\FieldType;
44

55
use Exception;
6+
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
67
use SilverStripe\Core\Injector\Injector;
78
use SilverStripe\ORM\DB;
89
use SilverStripe\ORM\FieldType\DBDecimal;
@@ -17,6 +18,8 @@
1718
*/
1819
class EncryptedDecimal extends DBDecimal
1920
{
21+
use EncryptedFieldGetValueTrait;
22+
2023
/**
2124
* @var AtRestCryptoService
2225
*/
@@ -40,10 +43,8 @@ public function setValue($value, $record = null, $markChanged = true)
4043
}
4144
}
4245

43-
public function getDecryptedValue($value)
46+
public function getDecryptedValue(string $value = '')
4447
{
45-
// Type hardening for PHP 8.1+
46-
$value = (string)$value;
4748
// Test if we're actually an encrypted value;
4849
if (ctype_xdigit($value) && strlen($value) > 130) {
4950
try {
@@ -56,11 +57,6 @@ public function getDecryptedValue($value)
5657
return (float)$value;
5758
}
5859

59-
public function getValue()
60-
{
61-
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
62-
}
63-
6460
public function requireField()
6561
{
6662
$values = array(

src/FieldType/EncryptedEnum.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Madmatt\EncryptAtRest\FieldType;
44

55
use Exception;
6+
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
67
use SilverStripe\Core\Injector\Injector;
78
use SilverStripe\ORM\DB;
89
use SilverStripe\ORM\FieldType\DBEnum;
@@ -18,6 +19,8 @@
1819
*/
1920
class EncryptedEnum extends DBEnum
2021
{
22+
use EncryptedFieldGetValueTrait;
23+
2124
/**
2225
* @var AtRestCryptoService
2326
*/
@@ -41,10 +44,8 @@ public function setValue($value, $record = null, $markChanged = true)
4144
}
4245
}
4346

44-
public function getDecryptedValue($value)
47+
public function getDecryptedValue(string $value = '')
4548
{
46-
// Type hardening for PHP 8.1+
47-
$value = (string)$value;
4849
// Test if we're actually an encrypted value;
4950
if (ctype_xdigit($value) && strlen($value) > 130) {
5051
try {
@@ -57,11 +58,6 @@ public function getDecryptedValue($value)
5758
return $value;
5859
}
5960

60-
public function getValue()
61-
{
62-
return $this->getDecryptedValue($this->value);
63-
}
64-
6561
public function requireField()
6662
{
6763
$values = array(

src/FieldType/EncryptedInt.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Madmatt\EncryptAtRest\FieldType;
44

55
use Exception;
6+
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
67
use SilverStripe\Core\Injector\Injector;
78
use SilverStripe\ORM\DB;
89
use SilverStripe\ORM\FieldType\DBInt;
@@ -17,6 +18,8 @@
1718
*/
1819
class EncryptedInt extends DBInt
1920
{
21+
use EncryptedFieldGetValueTrait;
22+
2023
/**
2124
* @var AtRestCryptoService
2225
*/
@@ -40,10 +43,8 @@ public function setValue($value, $record = null, $markChanged = true)
4043
}
4144
}
4245

43-
public function getDecryptedValue($value)
46+
public function getDecryptedValue(string $value = '')
4447
{
45-
// Type hardening for PHP 8.1+
46-
$value = (string)$value;
4748
// Test if we're actually an encrypted value;
4849
if (ctype_xdigit($value) && strlen($value) > 130) {
4950
try {
@@ -56,11 +57,6 @@ public function getDecryptedValue($value)
5657
return $value;
5758
}
5859

59-
public function getValue()
60-
{
61-
return $this->getDecryptedValue($this->value);
62-
}
63-
6460
public function requireField()
6561
{
6662
$values = array(

src/FieldType/EncryptedText.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace Madmatt\EncryptAtRest\FieldType;
44

55
use Exception;
6+
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
67
use SilverStripe\Core\Injector\Injector;
78
use SilverStripe\ORM\DB;
89
use SilverStripe\ORM\FieldType\DBText;
910
use Madmatt\EncryptAtRest\AtRestCryptoService;
1011

1112
class EncryptedText extends DBText
1213
{
14+
use EncryptedFieldGetValueTrait;
15+
1316
/**
1417
* @var AtRestCryptoService
1518
*/
@@ -33,10 +36,8 @@ public function setValue($value, $record = null, $markChanged = true)
3336
}
3437
}
3538

36-
public function getDecryptedValue($value)
39+
public function getDecryptedValue(string $value = '')
3740
{
38-
// Type hardening for PHP 8.1+
39-
$value = (string)$value;
4041
// Test if we're actually an encrypted value;
4142
if (ctype_xdigit($value) && strlen($value) > 130) {
4243
try {
@@ -49,11 +50,6 @@ public function getDecryptedValue($value)
4950
return $value;
5051
}
5152

52-
public function getValue()
53-
{
54-
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
55-
}
56-
5753
public function requireField()
5854
{
5955
$values = array(

src/FieldType/EncryptedVarchar.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace Madmatt\EncryptAtRest\FieldType;
44

55
use Exception;
6+
use Madmatt\EncryptAtRest\AtRestCryptoService;
7+
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
68
use SilverStripe\Core\Injector\Injector;
79
use SilverStripe\ORM\DB;
810
use SilverStripe\ORM\FieldType\DBVarchar;
9-
use Madmatt\EncryptAtRest\AtRestCryptoService;
1011

1112
/**
1213
* Class EncryptedVarchar
@@ -17,6 +18,9 @@
1718
*/
1819
class EncryptedVarchar extends DBVarchar
1920
{
21+
22+
use EncryptedFieldGetValueTrait;
23+
2024
/**
2125
* @var AtRestCryptoService
2226
*/
@@ -40,10 +44,8 @@ public function setValue($value, $record = null, $markChanged = true)
4044
}
4145
}
4246

43-
public function getDecryptedValue($value)
47+
public function getDecryptedValue(string $value = '')
4448
{
45-
// Type hardening for PHP 8.1+
46-
$value = (string)$value;
4749
// Test if we're actually an encrypted value;
4850
if (ctype_xdigit($value) && strlen($value) > 130) {
4951
try {
@@ -56,18 +58,13 @@ public function getDecryptedValue($value)
5658
return $value;
5759
}
5860

59-
public function getValue()
60-
{
61-
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
62-
}
63-
6461
public function requireField()
6562
{
6663
$values = array(
67-
'type' => 'text',
64+
'type' => 'text',
6865
'parts' => array(
69-
'datatype' => 'text',
70-
'null' => 'not null',
66+
'datatype' => 'text',
67+
'null' => 'not null',
7168
'arrayValue' => $this->arrayValue
7269
)
7370
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Madmatt\EncryptAtRest\Traits;
4+
5+
trait EncryptedFieldGetValueTrait
6+
{
7+
8+
public function getValue()
9+
{
10+
// Type hardening for PHP 8.1+
11+
$value = (string)$this->value;
12+
13+
return $this->getDecryptedValue($value);
14+
}
15+
16+
}

0 commit comments

Comments
 (0)