Skip to content
This repository has been archived by the owner on Jan 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #7 from salesupply/feature/allow-different-types
Browse files Browse the repository at this point in the history
Feature/allow different types
  • Loading branch information
rkeet-salesupply authored Apr 3, 2018
2 parents 70993f2 + 92fdc64 commit 25b5fe3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/Annotation/Encrypted.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class Encrypted
*/
public $pepper;

/**
* @var string type that the encrypted/decrypted string should be
*/
public $type = 'string';

/**
* @return string
*/
Expand Down Expand Up @@ -84,4 +89,22 @@ public function setPepper(?string $pepper): Encrypted
return $this;
}

/**
* @return null|string
*/
public function getType(): ?string
{
return $this->type;
}

/**
* @param null|string $type
* @return Encrypted
*/
public function setType(string $type): Encrypted
{
$this->type = $type;
return $this;
}

}
33 changes: 30 additions & 3 deletions src/Subscriber/DoctrineEncryptSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ public static function capitalize(string $word): string
/**
* Process (encrypt/decrypt) entities fields
*
* @param object $entity Some doctrine entity
* @param $entity
* @param EntityManager $em
* @param bool $isEncryptOperation
* @return bool
* @throws \Doctrine\ORM\Mapping\MappingException
* @throws \Exception
*/
private function processFields($entity, EntityManager $em, $isEncryptOperation = true): bool
{
Expand All @@ -216,14 +218,37 @@ private function processFields($entity, EntityManager $em, $isEncryptOperation =
$refProperty = $property['reflection'];
/** @var Encrypted $annotationOptions */
$annotationOptions = $property['options'];
/** @var boolean $nullable */
$nullable = $property['nullable'];

$value = $refProperty->getValue($entity);
$value = $value === null ? '' : $value;
// If the value is 'null' && is nullable, don't do anything, just skip it.
if (is_null($value) && $nullable) {

continue;
}

$value = $isEncryptOperation ?
$this->encrypt($entity, $value, $annotationOptions) :
$this->decrypt($entity, $value, $annotationOptions);

$type = $annotationOptions->getType();

// If NOT encrypting, type know to PHP and the value does not match the type. Else error
if (
!$isEncryptOperation
// We're going to try a cast using settype. Array of types defined at: https://php.net/settype
&& in_array($type, ['boolean', 'bool', 'integer', 'int', 'float', 'double', 'string', 'array', 'object', 'null'])
&& gettype($value) !== $type
) {
settype($value, $type);
} else {

throw new \Exception(
'Could not convert encrypted value back to mapped value in ' . __CLASS__ . '::' . __FUNCTION__ . PHP_EOL
);
}

$refProperty->setValue($entity, $value);

if (! $isEncryptOperation) {
Expand Down Expand Up @@ -394,7 +419,8 @@ private function addToDecodedRegistry($entity)
/**
* @param object $entity
* @param EntityManager $em
* @return array|mixed|\ReflectionProperty[]
* @return array|mixed
* @throws \Doctrine\ORM\Mapping\MappingException
*/
private function getEncryptedFields(object $entity, EntityManager $em)
{
Expand All @@ -419,6 +445,7 @@ private function getEncryptedFields(object $entity, EntityManager $em)
$encryptedFields[] = [
'reflection' => $refProperty,
'options' => $annotationOptions,
'nullable' => $meta->getFieldMapping($refProperty->getName())['nullable'],
];
}
}
Expand Down

0 comments on commit 25b5fe3

Please sign in to comment.