PHP code example of arian-taylor / pw-components-php

1. Go to this page and download the library: Download arian-taylor/pw-components-php 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/ */

    

arian-taylor / pw-components-php example snippets


use Pw\Recaptcha\Recaptcha

$recaptcha = new Recaptcha([]);

namespace App\Service;

use Pw\Recaptcha\Recaptcha;

class ReCAPTCHAService {

	private $secret;
    private $recaptcha_key;
    private $recaptcha_score_threshold;

    public function __construct( 
        string $recaptcha_secret,
        string $recaptcha_key,
        string $RECAPTCHA_SCORE_THRESHOLD
    ){
    	$this->secret = $recaptcha_secret;
        $this->recaptcha_key = $recaptcha_key;
        $this->recaptcha_score_threshold = $RECAPTCHA_SCORE_THRESHOLD;
    }

    /**
     * @param string $recaptcha_token
     * @return boolean
     */
    public function isValidReCaptcha($recaptcha_token){
        if(!$recaptcha_token){
            return false;
        }

        $options = [
            "secret" => $this->secret,
            "recaptcha_key" => $this->recaptcha_key,
            "recaptcha_score_threshold" => $this->recaptcha_score_threshold,
        ];

        $recaptcha = new Recaptcha($options);
        $recaptchaData = null;

        $isValid = $recaptcha->isValidReCaptcha(
            $recaptcha_token,  
            $recaptchaData
        );

        return $isValid;
    }

}

use Pw\RateLimiter\RateLimiter

$rateLimiter = new RateLimiter()

namespace App\Service;

use Pw\RateLimiter\RateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory ;

class RateLimiterService {

    private $anonymousApiLimiter;

    public function __construct(
        RateLimiterFactory  $anonymousApiLimiter,
    ){
        $this->anonymousApiLimiter = $anonymousApiLimiter;
    }

    public function rateLimiter()
    {
        $rateLimiter = new RateLimiter($this->anonymousApiLimiter);
        $isLimit = $rateLimiter->isConsumeRateLimiter();

        return $isLimit;
    }
}

use Pw\DataTable\ApiDataTable

    return [
   ...
    Pw\DataTable\ApiDataTable::class => ['dev' => true, 'test' => true],
];

namespace App\Service;


use App\Entity\Utilisateur;
use Pw\DataTable\ApiDataTable;

class ApiDataTableService {
    private $em;
    private $apiDataTable;

    public function __construct(
        EntityManagerInterface $em,
        ApiDataTable  $apiDataTable
    ){
        $this->em = $em;
        $this->apiDataTable = $apiDataTable;
    }

    public function getDataTable()
    {
        $params = [
            "em" => $this->em,
            "query" => [
                "key" => "",
                "page" => "1",
                "limit" => "5",
                "filters" => [],
                "order" => "ASC",
                "order_by" => "name",
            ],
            "entity" => Utilisateur::class
        ];
        $apiDataTable = $this->apiDataTable;
        $result = $apiDataTable->get($params);

        return $result;
    }
}

use Pw\Params\Params

$pwParams = new Params()

namespace App\Service;

use Pw\Params\Params;

class FormulaireService {

    public function __construct(){
        $this->pwParams = new Params();
    }

    public function addFormulaire()
    {
        $pwParams = $this->pwParams;
        
        // retrieves $_POST variables 
        $lastname = $pwParams->post($request, "lastname");

        // retrieves  $_GET variables 
        $lastname = $pwParams->get($request, "lastname");

        // get value in associative array by key
        $array = [
            "lastname" => "Rakoto"
        ];
        $lastname = $pwParams->array($array, "lastname");

        return $pwParams->response("ok", "success", []);
    }
}

use Pw\Exports\ExportExcel


namespace App\Service;

use App\Entity\User;
use Pw\Exports\ExportExcel;
use Doctrine\ORM\EntityManagerInterface;

class UserExportExcelService {

    const DICTIONARY = [
        "is_active" => [
            "1" => "Oui",
            "0" => "Non",
            true => "Oui",
            false => "Non",
            null => "Non",
        ],
    ];

    const KEY_LABELS = [
        "id" => "Id",
        "lastname_firstname" => "Nom et Prénom",
        "email" => "Email",
        "phone" => "Téléphone",
        "is_active" => "Valider",
    ];

    public function __construct(
        private EntityManagerInterface $em,
        private ExportExcel $exportExcel
    ){}

    /**
     * public methode to generate an excel file
     * 
     * @return File || null
     */
    public function generateExcel(){

        $onglet_name = 'Liste des utilisateurs' ;
        $KEY_LABELS = self::KEY_LABELS;
        $DICTIONARY = self::DICTIONARY;
        $key_values = [];

        $users = $this->em->getRepository(User::class)->findByParams(["role" => "ROLE_USER"]) ;
        foreach($users as $user){
            $key_values[] = [
                "id" => $user->getId(),
                "lastname_firstname" => implode(" ", [$user->getLastname(), $user->getFirstname()]),
                "email" => $user->getEmail(),
                "phone" => $user->getPhone(),
                "is_active" => $user->isIsActive(),
            ];
        };

        $formated = $this->exportExcel->formatExport(
            $onglet_name,
            $key_values, 
            $KEY_LABELS, 
            $DICTIONARY
        );
        $spreadsheet = $this->exportExcel->setupSpreadSheet($formated, [
            "superHeadSize" => 15,
            "superHeadColor" => "ffffff",
            "superHeadBgColor" => "ff0000",
            "headSize" => 10,
            "headColor" => "000000",
            "headBgColor" => "00ff00",
            "contentSize" => 10,
        ]);

        $excel = $this->exportExcel->buildExcel("liste-utilisateurs.xlsx", $spreadsheet);

        if(is_file($excel)){
            return $excel;
        }

        return null;
    }
}



namespace App\Controller;

use App\Service\UserExportExcelService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ExportController extends AbstractController
{
    #[Route('/user/export/excel', name: 'user_export_excel')]
    public function userExportExcelAction(
        Request $request,
        UserExportExcelService $userExportExcelService
    ): Response
    {
        $excel = $userExportExcelService->generateExcel();

        $filename = "Liste-utilisateurs.xlsx";
        $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT;

        $response = new BinaryFileResponse($excel);
        $response->setStatusCode(Response::HTTP_OK);
        $response->setContentDisposition($disposition, $filename);
        return $response;
    }
}


namespace App\Service;

use App\Entity\User;
use Pw\Exports\ExportExcel;
use Doctrine\ORM\EntityManagerInterface;

class UserExportExcelService {

    const DICTIONARY = [
        "is_active" => [
            "1" => "Oui",
            "0" => "Non",
            true => "Oui",
            false => "Non",
            null => "Non",
        ],
    ];

    const KEY_LABELS = [
        "id" => "Id",
        "lastname_firstname" => "Nom et Prénom",
        "email" => "Email",
        "phone" => "Téléphone",
        "is_active" => "Valider",
    ];

    public function __construct(
        private EntityManagerInterface $em,
        private ExportExcel $exportExcel
    ){}

    /**
     * public methode to generate an excel file
     * 
     * @return File || null
     */
    public function generateExcel(){

        $onglet_name_user = 'Liste des utilisateurs' ;
        $onglet_name_admin = 'Liste des administrateur' ;
        $KEY_LABELS = self::KEY_LABELS;
        $DICTIONARY = self::DICTIONARY;
        $key_values_user = [];
        $key_values_admin = [];

        $users = $this->em->getRepository(User::class)->findByParams(["role" => "ROLE_USER"]) ;
        foreach($users as $user){
            $key_values_user[] = [
                "id" => $user->getId(),
                "lastname_firstname" => implode(" ", [$user->getLastname(), $user->getFirstname()]),
                "email" => $user->getEmail(),
                "phone" => $user->getPhone(),
                "is_active" => $user->isIsActive(),
            ];
        };
        
        $admins = $this->em->getRepository(User::class)->findByParams(["role" => "ROLE_ADMIN"]) ;
        foreach($admins as $admin){
            $key_values_admin[] = [
                "id" => $user->getId(),
                "lastname_firstname" => implode(" ", [$user->getLastname(), $user->getFirstname()]),
                "email" => $user->getEmail(),
                "phone" => $user->getPhone(),
                "is_active" => $user->isIsActive(),
            ];
        };

        // Onglet Liste des utilisateurs
        $formated = $this->exportExcel->formatExport(
            $onglet_name_user,
            $key_values_user, 
            $KEY_LABELS, 
            $DICTIONARY
        );
        $spreadsheet = $this->exportExcel->setupSpreadSheet($formated, [
            "superHeadSize" => 15,
            "superHeadColor" => "ffffff",
            "superHeadBgColor" => "ff0000",
            "headSize" => 10,
            "headColor" => "000000",
            "headBgColor" => "00ff00",
            "contentSize" => 10,
        ]);
        
        // Onglet Liste des administrateurs
        $formated = $this->exportExcel->formatExport(
            $onglet_name_admin,
            $key_values_admin, 
            $KEY_LABELS, 
            $DICTIONARY
        );
        $spreadsheet = $this->exportExcel->setupSpreadSheet($formated, [
            "superHeadSize" => 15,
            "superHeadColor" => "ff0000",
            "superHeadBgColor" => "00ff00",
            "headSize" => 10,
            "headColor" => "ffffff",
            "headBgColor" => "0000ff",
            "contentSize" => 10,
        ], $spreadsheet, 1);

        $excel = $this->exportExcel->buildExcel("liste-utilisateurs.xlsx", $spreadsheet);

        if(is_file($excel)){
            return $excel;
        }

        return null;
    }
}

use Pw\Exports\ExportPdf


namespace App\Service;

use Dompdf\Dompdf;
use Dompdf\Options;

use Twig\Environment;
use Pw\Exports\ExportPdf;
use Doctrine\ORM\EntityManagerInterface;

class UserExportPdfService {

    const DICTIONARY = [
        "is_active" => [
            "1" => "Oui",
            "0" => "Non",
            true => "Oui",
            false => "Non",
            null => "Non",
        ],
    ];

    const KEY_LABELS = [

        [
            "key" => "INFORMATION_GROUP",
            "label" => "Information Utilisateur",
        ],
        [
            "key" => "id",
            "label" => "Id",
        ],
        [
            "key" => "lastname_firstname",
            "label" => "Nom et Prénom",
        ],
        [
            "key" => "email",
            "label" => "Email",
        ],
        [
            "key" => "phone",
            "label" => "Téléphone",
        ],
        [
            "key" => "is_active",
            "label" => "Validation",
        ],


        [
            "key" => "teams",
            "label" => "Equipes",
            "key_labels" => [
                [
                    "key" => "lastname_firstname",
                    "label" => "Nom et Prénom",
                ],
                [
                    "key" => "email",
                    "label" => "Email",
                ],
                [
                    "key" => "phone",
                    "label" => "Téléphone",
                ],
                [
                    "key" => "is_active",
                    "label" => "Validation",
                ],
            ]
        ],


        [
            "key" => "formations",
            "label" => "Formations",
            "model" => "MODEL_1"
        ],


        [
            "key" => "etudes",
            "label" => "Etudes",
            "model" => "MODEL_1"
        ],

    ];

    public function __construct(
        private EntityManagerInterface $em,
        private Environment $twig,
        private ExportPdf $exportPdf
    ){}

    /**
     * public methode to generate an excel file
     * 
     * @return Object || null
     */
    public function generatePdf(){

        $KEY_LABELS = self::KEY_LABELS;
        $DICTIONARY = self::DICTIONARY;

        $key_values = [

            "id" => "ed925dc-700f-6e6e-a29b-0b5a8a095f25",
            "lastname_firstname" => implode(" ", ["Doe", "Jane"]),
            "email" => "[email protected]",
            "phone" => "01 01 01 01 01",
            "is_active" => true,

            "teams" => [
                [
                    "lastname_firstname" => "Equipe1 Equipe1",
                    "email" => "[email protected]",
                    "phone" => "02 02 02 02 02",
                    "is_active" => true,
                ],
                [
                    "lastname_firstname" => "Equipe2 Equipe2",
                    "email" => "[email protected]",
                    "phone" => "03 03 03 03 03",
                    "is_active" => false,
                ],
            ],

            "etudes" => [
                [
                    "date_start" => "2005",
                    "date_end" => "2009",
                    "description" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ",
                ],
                [
                    "date_start" => "2012",
                    "date_end" => "2015",
                    "description" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
                ],
            ],

            "formations" => [
                [
                    "date_start" => "2010",
                    "date_end" => "2012",
                    "description" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ",
                ],
                [
                    "date_start" => "2012",
                    "date_end" => "2015",
                    "description" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
                ],
            ],

        ];

        $formated_datas = $this->exportPdf->formatExport(
            $key_values, 
            $KEY_LABELS,
            $DICTIONARY,
        );

        $template = "pdf/template.html.twig";

        $content_html = $this->twig->render($template , [
            "formated_datas" => $formated_datas,
        ]);

        $dompdf = $this->exportPdf->generateDomPdf($content_html);

        return $dompdf;

    }

}


namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use App\Object\CustomTranslationObject;
use Twig\Environment;

class AppExtension extends AbstractExtension
{

    public function __construct(
        private Environment $twig
    ){
    }

    public function getFilters()
    {
        return [];
    }

    public function getFunctions()
    {
        return [
            new TwigFunction('renderPdfContent', [$this, 'renderPdfContent']),
        ];
    }

    /**
     * 
     * @param [] $formated_datas
     * @return string
     */
    function renderPdfContent($formated_datas)
    {
        $template = "pdf/content.html.twig";

        $content_html = $this->twig->render($template , [
            "formated_datas" => $formated_datas,
        ]);

        return $content_html ;
    }
}



namespace App\Controller;

use App\Service\UserExportPdfService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ExportController extends AbstractController
{
    #[Route('/user/export/pdf', name: 'user_export_pdf')]
    public function userExportPdfAction(
        UserExportPdfService $userExportPdfService
    ): Response
    {
        $dompdf = $userExportPdfService->generatePdf();

        $filename = "information-utilisateur-".date("d-m-Y-").uniqid();
        $dompdf->stream($filename, [
            "Attachment" => true
        ]);

        return new Response('', 200, [
          'Content-Type' => 'application/pdf',
        ]);
    }
}
 bin/console pw-generator:generate page front
 bin/console pw-generator:generate api front
 bin/console pw-generator:generate service front
 bin/console pw-generator:generate <type> <name> <method> [options]

use Pw\FileValidator\FileValidator

$fileValidator = new FileValidator([]);

use Pw\FileValidator\FileValidator

$allowedMimeTypes = ['image/jpeg', 'image/png'];
$allowedExtensions = ['jpg', 'jpeg', 'png'];
$maxFileSize = 1024 * 1024; // 1 MB

$fileValidator = new FileValidator(
    $allowedMimeTypes, 
    $allowedExtensions, 
    $maxFileSize
);

$file = [
    'name' => 'example.jpg',
    'type' => 'image/jpeg',
    'size' => 500000, // 500 KB
];

if ($fileValidator->validateFile($file)) {
    //Le fichier est valide.
} else {
    //Le fichier n'est pas valide.
}

use Pw\FileValidator\FileValidator

$fileValidator = new FileValidator();

$mimetype = $file->getMimeType();//Type de fichier

if ($fileValidator->isImage($mimetype)) {
    //Le fichier est un image.
} else {
    //Le fichier n'est un image.
}

//ou
if ($fileValidator->isImage($file)) {//Objet de fichier
    //Le fichier est un image.
} else {
    //Le fichier n'est un image.
}

uploadToPath($file, $dir, $file_name)
isImage($file)
isPdf($file)
isDocx($file)
isExcel($file)
isVideo($file)
getFileType($file)
validSize($file, $max_mb=1)
validateMimeType($file)
validateExtension($file)
validateFile($file)

php composer.phar 

php bin/console pw-generator:generate --help

php bin/console pw-generator:generate <type>