PHP code example of specshaper / gdpr-bundle

1. Go to this page and download the library: Download specshaper/gdpr-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/ */

    

specshaper / gdpr-bundle example snippets



// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new SpecShaper\GdprBundle\SpecShaperGdprBundle(),
            new SpecShaper\EncryptBundle\SpecShaperEncryptBundle(),
        );
        // ...
    }
    // ...
}


    // ...
    use Symfony\Component\Validator\Constraints as Assert;
    use SpecShaper\GdprBundle\Validator\Constraints as GdprAssert;
    use SpecShaper\GdprBundle\Model\PersonalData;
    // ...
    
    /**
     * Iban bank account number.
     * 
     * @GdprAssert\PersonalData({
     *     @Assert\NotBlank,
     *     @Assert\Iban
     * })
     *
     * @ORM\Column(type="personal_data", nullable=true, options={
     *     "format" = "STRING",
     *     "isSensitive"=false,
     *     "isEncrypted"=true,
     *     "idMethod"="INDIRECT",
     *     "basisOfCollection"="LEGITIMATE_INTEREST",
     *     "identifiableBy"="Can be used to identify an individual if compared with third party database",
     *     "providedBy"="The employee, the employer",
     *     "purposeFor"="Used to pay employees by SEPA",
     *     "retainFor"="P6Y",
     *     "disposeBy"="SET_NULL",
     *     "methodOfReceipt"={"HTTPS"},
     *     "receiptProtection"={"TSS"},
     *     "methodOfReturn"={"HTTPS", "PDF"},
     *     "returnProtection"={"TSS","ENCRYPTED_PDF"}
     * })
     */
    protected PersonalData $iban;
   


    #[GdprAssert\PersonalData(new IreAssert\ValidPPS(groups: ['revenue'])]
    #[ORM\Column(type: 'personal_data', nullable: true, options: [
        'format' => 'STRING',
        'isSensitive' => false,
        'isEncrypted' => true,
        'basisOfCollection' => 'LEGITIMATE_INTEREST',
        'identifiableBy' => 'Can be used to identify an individual with tax records',
        'providedBy' => 'The employee, revenue, the employer',
        'purposeFor' => 'Used to submit tax returns to revenue and to employee',
        'retainFor' => 'P6Y',
        'disposeBy' => 'SET_NULL',
        'methodOfReceipt' => ['HTTPS'],
        'receiptProtection' => ['TSS'],
        'methodOfReturn' => ['HTTPS', 'PDF'],
        'returnProtection' => ['TSS', 'ENCRYPTED_PDF'],
    ])]
    protected ?PersonalData $taxNumber;

    #[GdprAssert\PersonalData([
        new Assert\Iban(groups: ['bank_account']),
        new Assert\NotBlank(groups: ['bank_account']),
    ])]



// ...
use SpecShaper\GdprBundle\Form\Type\PersonalDataType;
    
    // ...
    $builder    
        ->add('iban', PersonalDataType::class, array(
            '      ))
        ;


// ...
use SpecShaper\GdprBundle\Form\Type\PersonalDataType;
    
    // ...
    $builder    
        ->add('iban', PersonalDataType::class, array(
            '           'constraints' => array(
                new Iban()
            )
        ))
        ;


// src/AppBundle/Repository/Traits/GdprTrait.php

namespace AppBundle\Repository\Traits;

use SpecShaper\EncryptBundle\Encryptors\EncryptorInterface;
use SpecShaper\GdprBundle\Utils\Sorter;

/**
 * Trait GdprTrait
 *
 * Trait to provide common functions for encrypted fields in a repository.
 * - Decrypt & concatenate first and last name
 * - Sort by two fields.
 *
 * @package AppBundle\Repository\Traits
 */
trait GdprTrait
{
    protected EncryptorInterface $encryptor;

    /**
     * Setter injection Encryptor into repository.
     */
    public function setEncryptor(EncryptorInterface $encryptor): EncryptorInterface
    {
        $this->encryptor = $encryptor;
        return $this;
    }

    /**
     * Get the Encryptor.
     */
    public function getEncryptor(): EncryptorInterface
    {
        return $this->encryptor;
    }

    /**
     * Function to concat two encrypted values into one new value.
     */
    public function concatToFullName(
         array &$collection, 
         ?string $firstNameField = 'firstName',
         ?string $lastNameField = 'lastName',
         ?string $outputField = 'fullName'
         ): array
         {

        foreach($collection as $key => $entity){
            $firstName = $this->getEncryptor()->decrypt($entity[$firstNameField]->getData());
            $lastName = $this->getEncryptor()->decrypt($entity[$lastNameField]->getData());
            $collection[$key][$firstNameField]->setData($firstName);
            $collection[$key][$lastNameField]->setData($lastName);
            $collection[$key][$outputField] = $firstName . ' ' . $lastName;
        }

        return $collection;
    }

    /**
     * Sort a array hydrated query result by two columns.
     */
    public function sortByTwoColumns(
        array &$result,
        ?string $firstOrder = 'employeeId',
        ?string$secondOrder = 'lastName'
        ): array
        {
            // Use SpecShaper\ThemeBundle\Util\Sorter:sortByTwoColumnsCallback as a callback
            usort($result, array(new Sorter($firstOrder, $secondOrder),'sortByTwoColumnsCallback'));
            return $result;
        }
  
}



namespace AppBundle\Repository;

use AppBundle\Repository\Traits\GdprTrait;

/**
 * EmployeeRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class EmployeeRepository extends \Doctrine\ORM\EntityRepository
{
    use GdprTrait;
    //....
}



namespace App\Repository;

use App\Entity\Organisation;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use SpecShaper\EncryptBundle\Encryptors\EncryptorInterface;

/**
 * OrganisationRepository
 */
class OrganisationRepository extends ServiceEntityRepository
{
    private EncryptorInterface $encryptor;
    
    public function __construct(ManagerRegistry $registry, EncryptorInterface $encryptor)
    {
        parent::__construct($registry, Organisation::class);
        $this->encryptor = $encryptor;
    }
    //....    
}



namespace AppBundle\Controller;

use AppBundle\Entity\Employee;
use SpecShaper\EncryptBundle\Encryptors\EncryptorInterface;

/**
 * Employee controller.
 *
 * @Route("/employee")
 */
class EmployeeController extends Controller
{
    /**
     * Lists all Employee entities.
     *
     * @Route("/{id}/all", name="employee_all")
     * @Method("GET")
     */
    public function allAction(EncryptorInterface $encryptor)
    {
        $employee = $this->getDoctrine()
            ->getRepository(Employee::class)
            ->setEncryptor($encryptor)
            ->findAll();
    }
}