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/ */
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;
}
}
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 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',
]);
}
}
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.
}