public function encryptEntity(EntityInterface $entity): EntityInterface
    {
        if (!$this->isEncryptable($entity)) {
            return $entity;
        }

        $fields = $this->getFields();
        $encryptionKey = $this->getConfig('encryptionKey');
        $base64 = $this->getConfig('base64');
        $table = $this->getTable();

        $patch = [];
        foreach ($fields as $name => $field) {
            if (!$table->hasField($name)) {
                continue;
            }
            $value = $entity->get($name);
            if ($value === null || is_resource($value)) {
                continue;
            }
            $encrypted = Security::encrypt($value, $encryptionKey);
            if ($base64 === true) {
                $encrypted = base64_encode($encrypted);
            }
            $patch[$name] = $encrypted;
        }

        if (!empty($patch)) {
            $accessible = array_fill_keys(array_keys($patch), true);
            $entity = $table->patchEntity($entity, $patch, [
                'accessibleFields' => $accessible,
            ]);
        }

        return $entity;
    }
public function encryptEntity(EntityInterface $entity): EntityInterface
    {
        if (!$this->isEncryptable($entity)) {
            return $entity;
        }

        $fields = $this->getFields();
        $encryptionKey = $this->getConfig('encryptionKey');
        $base64 = $this->getConfig('base64');
        $table = $this->getTable();

        $patch = [];
        foreach ($fields as $name => $field) {
            if (!$table->hasField($name)) {
                continue;
            }
            $value = $entity->get($name);
            if ($value === null || is_resource($value)) {
                continue;
            }
            $encrypted = Security::encrypt($value, $encryptionKey);
            if ($base64 === true) {
                $encrypted = base64_encode($encrypted);
            }
            $patch[$name] = $encrypted;
        }

        if (!empty($patch)) {
            $accessible = array_fill_keys(array_keys($patch), true);
            $entity = $table->patchEntity($entity, $patch, [
                'accessibleFields' => $accessible,
            ]);
        }

        return $entity;
    }
public function encryptEntity ( EntityInterface $ entity ) : EntityInterface { if ( ! $ this -> isEncryptable ( $ entity ) ) { return $ entity ; } $ fields = $ this -> getFields ( ) ; $ encryptionKey = $ this -> getConfig ( 'encryptionKey' ) ; $ base64 = $ this -> getConfig ( 'base64' ) ; $ table = $ this -> getTable ( ) ; $ patch = [ ] ; foreach ( $ fields as $ name => $ field ) { if ( ! $ table -> hasField ( $ name ) ) { continue ; } $ value = $ entity -> get ( $ name ) ; if ( $ value === null || is_resource ( $ value ) ) { continue ; } $ encrypted = Security :: encrypt ( $ value , $ encryptionKey ) ; if ( $ base64 === true ) { $ encrypted = base64_encode ( $ encrypted ) ; } $ patch [ $ name ] = $ encrypted ; } if ( ! empty ( $ patch ) ) { $ accessible = array_fill_keys ( array_keys ( $ patch ) , true ) ; $ entity = $ table -> patchEntity ( $ entity , $ patch , [ 'accessibleFields' => $ accessible , ] ) ; } return $ entity ; }
Encrypts the fields.

If the value of the fields is null or a resource the encryption process
is skipped for this field.

@param \Cake\Datasource\EntityInterface $entity Entity object.
@return \Cake\Datasource\EntityInterface Entity object.
Encrypts the fields .