PHPackages                             passion-web/pw-components-php-dev - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. passion-web/pw-components-php-dev

ActiveLibrary

passion-web/pw-components-php-dev
=================================

PW Components

1.3.0(2y ago)011MITPHPPHP ^8.0

Since Feb 15Pushed 2y ago2 watchersCompare

[ Source](https://github.com/Flo976/pw-component-php-dev)[ Packagist](https://packagist.org/packages/passion-web/pw-components-php-dev)[ RSS](/packages/passion-web-pw-components-php-dev/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (8)Versions (29)Used By (0)

pw-components-php-dev
=====================

[](#pw-components-php-dev)

Requirements
------------

[](#requirements)

- PHP version 8.0 ou supérieure

Installation
------------

[](#installation)

- Avec composer:

```
composer require passion-web/pw-components-php-dev

```

- Avec composer.phar:

```
php composer.phar require passion-web/pw-components-php-dev

```

Utilisation
-----------

[](#utilisation)

### Recaptcha

[](#recaptcha)

#### Importation de la classe

[](#importation-de-la-classe)

```
use Pw\Recaptcha\Recaptcha
```

#### Instanciation

[](#instanciation)

```
$recaptcha = new Recaptcha([]);
```

#### Exemple d’utilisation

[](#exemple-dutilisation)

```
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;
    }

}
```

###### On peut utiliser partout la méthode `isValidReCaptcha` avec `recaptcha_token` en paramètre.

[](#on-peut-utiliser-partout-la-méthode-isvalidrecaptcha-avec-recaptcha_token-en-paramètre)

###### Si on veut récupérer le `data` retourné par `reCAPTCHA` en cas d’erreur ou du succès, on peut l’avoir avec `recaptchaData`

[](#si-on-veut-récupérer-le-data-retourné-par-recaptcha-en-cas-derreur-ou-du-succès-on-peut-lavoir-avec-recaptchadata)

### RateLimiter

[](#ratelimiter)

#### Importation de la classe

[](#importation-de-la-classe-1)

```
use Pw\RateLimiter\RateLimiter
```

#### Instanciation

[](#instanciation-1)

```
$rateLimiter = new RateLimiter()
```

#### Exemple d’utilisation

[](#exemple-dutilisation-1)

```
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;
    }
}
```

### DataTable

[](#datatable)

#### Importation de la classe

[](#importation-de-la-classe-2)

```
use Pw\DataTable\ApiDataTable
```

#### Configuration

[](#configuration)

Modification du fichier services.yaml pour autoriser le autowire de l’apiDataTable autowire: true

```
    Pw\DataTable\ApiDataTable:
        # redundant thanks to _defaults, but value is overridable on each service
        autowire: true
```

Modification du fichier bundle.php

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

#### Exemple d’utilisation

[](#exemple-dutilisation-2)

```
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;
    }
}
```

### Params

[](#params)

#### Importation de la classe

[](#importation-de-la-classe-3)

```
use Pw\Params\Params
```

#### Instanciation

[](#instanciation-2)

```
$pwParams = new Params()
```

#### Exemple d’utilisation

[](#exemple-dutilisation-3)

```
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", []);
    }
}
```

### Export Excel

[](#export-excel)

#### Importation de la classe

[](#importation-de-la-classe-4)

```
use Pw\Exports\ExportExcel
```

#### Configuration

[](#configuration-1)

Modification du fichier services.yaml pour autoriser le autowire de l’apiDataTable autowire: true

```
services:
    # ...
    Pw\Exports\ExportExcel:
        autowire: true
```

#### Exemple d’utilisation (simple onglet)

[](#exemple-dutilisation-simple-onglet)

1- Création d’un service d’export exemple : UserExportExcelService.php

```
