Skip to content

Commit a44b789

Browse files
authored
Merge pull request #31 from silverstripeltd/bugfix/PHP8.1-compatibility
2 parents 0c1240a + f49250f commit a44b789

File tree

7 files changed

+46
-42
lines changed

7 files changed

+46
-42
lines changed

src/FieldType/EncryptedDatetime.php

Lines changed: 5 additions & 6 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,7 +44,7 @@ public function setValue($value, $record = null, $markChanged = true)
4044
}
4145
}
4246

43-
public function getDecryptedValue($value)
47+
public function getDecryptedValue(string $value = '')
4448
{
4549
// Test if we're actually an encrypted value;
4650
if (ctype_xdigit($value) && strlen($value) > 130) {
@@ -54,11 +58,6 @@ public function getDecryptedValue($value)
5458
return $value;
5559
}
5660

57-
public function getValue()
58-
{
59-
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
60-
}
61-
6261
public function requireField()
6362
{
6463
$values = array(

src/FieldType/EncryptedDecimal.php

Lines changed: 4 additions & 6 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,7 +43,7 @@ public function setValue($value, $record = null, $markChanged = true)
4043
}
4144
}
4245

43-
public function getDecryptedValue($value)
46+
public function getDecryptedValue(string $value = '')
4447
{
4548
// Test if we're actually an encrypted value;
4649
if (ctype_xdigit($value) && strlen($value) > 130) {
@@ -54,11 +57,6 @@ public function getDecryptedValue($value)
5457
return (float)$value;
5558
}
5659

57-
public function getValue()
58-
{
59-
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
60-
}
61-
6260
public function requireField()
6361
{
6462
$values = array(

src/FieldType/EncryptedEnum.php

Lines changed: 4 additions & 6 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,7 +44,7 @@ public function setValue($value, $record = null, $markChanged = true)
4144
}
4245
}
4346

44-
public function getDecryptedValue($value)
47+
public function getDecryptedValue(string $value = '')
4548
{
4649
// Test if we're actually an encrypted value;
4750
if (ctype_xdigit($value) && strlen($value) > 130) {
@@ -55,11 +58,6 @@ public function getDecryptedValue($value)
5558
return $value;
5659
}
5760

58-
public function getValue()
59-
{
60-
return $this->getDecryptedValue($this->value);
61-
}
62-
6361
public function requireField()
6462
{
6563
$values = array(

src/FieldType/EncryptedInt.php

Lines changed: 4 additions & 6 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,7 +43,7 @@ public function setValue($value, $record = null, $markChanged = true)
4043
}
4144
}
4245

43-
public function getDecryptedValue($value)
46+
public function getDecryptedValue(string $value = '')
4447
{
4548
// Test if we're actually an encrypted value;
4649
if (ctype_xdigit($value) && strlen($value) > 130) {
@@ -54,11 +57,6 @@ public function getDecryptedValue($value)
5457
return $value;
5558
}
5659

57-
public function getValue()
58-
{
59-
return $this->getDecryptedValue($this->value);
60-
}
61-
6260
public function requireField()
6361
{
6462
$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 & 10 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,7 +44,7 @@ public function setValue($value, $record = null, $markChanged = true)
4044
}
4145
}
4246

43-
public function getDecryptedValue($value)
47+
public function getDecryptedValue(string $value = '')
4448
{
4549
// Test if we're actually an encrypted value;
4650
if (ctype_xdigit($value) && strlen($value) > 130) {
@@ -54,18 +58,13 @@ public function getDecryptedValue($value)
5458
return $value;
5559
}
5660

57-
public function getValue()
58-
{
59-
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
60-
}
61-
6261
public function requireField()
6362
{
6463
$values = array(
65-
'type' => 'text',
64+
'type' => 'text',
6665
'parts' => array(
67-
'datatype' => 'text',
68-
'null' => 'not null',
66+
'datatype' => 'text',
67+
'null' => 'not null',
6968
'arrayValue' => $this->arrayValue
7069
)
7170
);
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)