Skip to content

Commit

Permalink
Implement GetValue function via a trait
Browse files Browse the repository at this point in the history
Reduces duplication
  • Loading branch information
scott-nz committed Oct 29, 2023
1 parent e148e60 commit f49250f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 52 deletions.
13 changes: 5 additions & 8 deletions src/FieldType/EncryptedDatetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime;
Expand All @@ -17,6 +18,9 @@
*/
class EncryptedDatetime extends DBDatetime
{

use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -40,10 +44,8 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Type hardening for PHP 8.1+
$value = (string)$value;
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
try {
Expand All @@ -56,11 +58,6 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
}

public function requireField()
{
$values = array(
Expand Down
12 changes: 4 additions & 8 deletions src/FieldType/EncryptedDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDecimal;
Expand All @@ -17,6 +18,8 @@
*/
class EncryptedDecimal extends DBDecimal
{
use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -40,10 +43,8 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Type hardening for PHP 8.1+
$value = (string)$value;
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
try {
Expand All @@ -56,11 +57,6 @@ public function getDecryptedValue($value)
return (float)$value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
}

public function requireField()
{
$values = array(
Expand Down
12 changes: 4 additions & 8 deletions src/FieldType/EncryptedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBEnum;
Expand All @@ -18,6 +19,8 @@
*/
class EncryptedEnum extends DBEnum
{
use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -41,10 +44,8 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Type hardening for PHP 8.1+
$value = (string)$value;
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
try {
Expand All @@ -57,11 +58,6 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value);
}

public function requireField()
{
$values = array(
Expand Down
12 changes: 4 additions & 8 deletions src/FieldType/EncryptedInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBInt;
Expand All @@ -17,6 +18,8 @@
*/
class EncryptedInt extends DBInt
{
use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -40,10 +43,8 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Type hardening for PHP 8.1+
$value = (string)$value;
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
try {
Expand All @@ -56,11 +57,6 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value);
}

public function requireField()
{
$values = array(
Expand Down
12 changes: 4 additions & 8 deletions src/FieldType/EncryptedText.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBText;
use Madmatt\EncryptAtRest\AtRestCryptoService;

class EncryptedText extends DBText
{
use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -33,10 +36,8 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Type hardening for PHP 8.1+
$value = (string)$value;
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
try {
Expand All @@ -49,11 +50,6 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
}

public function requireField()
{
$values = array(
Expand Down
21 changes: 9 additions & 12 deletions src/FieldType/EncryptedVarchar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\AtRestCryptoService;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBVarchar;
use Madmatt\EncryptAtRest\AtRestCryptoService;

/**
* Class EncryptedVarchar
Expand All @@ -17,6 +18,9 @@
*/
class EncryptedVarchar extends DBVarchar
{

use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -40,10 +44,8 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Type hardening for PHP 8.1+
$value = (string)$value;
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
try {
Expand All @@ -56,18 +58,13 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
}

public function requireField()
{
$values = array(
'type' => 'text',
'type' => 'text',
'parts' => array(
'datatype' => 'text',
'null' => 'not null',
'datatype' => 'text',
'null' => 'not null',
'arrayValue' => $this->arrayValue
)
);
Expand Down
16 changes: 16 additions & 0 deletions src/Traits/EncryptedFieldGetValueTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Madmatt\EncryptAtRest\Traits;

trait EncryptedFieldGetValueTrait
{

public function getValue()
{
// Type hardening for PHP 8.1+
$value = (string)$this->value;

return $this->getDecryptedValue($value);
}

}

0 comments on commit f49250f

Please sign in to comment.