PHP code example of odandb / doctrine-ciphersweet-encryption-bundle

1. Go to this page and download the library: Download odandb/doctrine-ciphersweet-encryption-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

odandb / doctrine-ciphersweet-encryption-bundle example snippets


// config/bundle.php

return [
    // ...
    Odandb\DoctrineCiphersweetEncryptionBundle\OdandbDoctrineCiphersweetEncryptionBundle::class => ['all' => true]
];

// .env

DOCTRINE_CIPHERSWEET_KEY=

#[ORM\Entity(repositoryClass: App\Repository\MySecretEntityRepository::class)
#[ORM\Index(name: 'anum_blind_idx', columns: ['account_number_bi'])]
class MySecretEntity
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private int $id;

    #[ORM\Column(type: 'string', length: 36)]
    private string $uuid;

    #[ORM\Column(type: 'string')]
    #[EncryptedField]
    #[IndexableField(indexesEntityClass: App\Entity\MySecretEntityIndexes::class, autoRefresh: false, indexesGenerationMethods: ['ValueStartingBy'], valuePreprocessMethod: 'cleanAccountNumber')]
    private string $accountNumber;

    #[ORM\Column(type: 'string', length: 10)]
    private string $accountNumberBi;
    
    private int $secretNumber;
    
    #[ORM\Column(type: 'string')]
    #[EncryptedField(mappedTypedProperty: 'secretNumber', indexable: false)]
    private string $secretNumberEncrypted;
    
    /**
     * @var Collection|null
     */
    #[ORM\OneToMany(targetEntity: MySecretEntityIndexes::class, mappedBy: 'targetEntity', cascade: ['persist'])]
    private ?Collection $indexes;
    
    /**
     * @param string $value
     * @return string
     */
    public function cleanAccountNumber(string $value): string
    {
        $value = trim($value);
        $value = ltrim($value, '0');

        return $value;
    }
    
    public function setMySecretEntityIndexes(array $indexes): self
    {
        $this->indexes = new ArrayCollection($indexes);
        return $this;
    }
    
    // ...
}

use Odandb\DoctrineCiphersweetEncryptionBundle\Entity\IndexedEntityInterface;
use Odandb\DoctrineCiphersweetEncryptionBundle\Entity\IndexedEntityTrait;

/**
 * Class storing indexes for MySecretEntity.
 */
#[ORM\Entity(repositoryClass: App\Repository\MySecretEntityIndexesRepository::class)
#[ORM\Index(name: 'blind_idx', columns: ['index_bi'])]
#[ORM\Index(name: 'field_and_blind_idx', columns: ['fieldname', 'index_bi'])]
class MySecretEntityIndexes implements IndexedEntityInterface
{
    use IndexedEntityTrait;

    /**
     * @var MySecretEntity|null
     */
    #[ORM\ManyToOne(targetEntity: App\Entity\MySecretEntity::class, inversedBy: 'indexes')]
    #[ORM\JoinColumn(name: 'target_entity_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
    protected object $targetEntity;
}