1. Go to this page and download the library: Download mcustiel/php-simple-request 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/ */
mcustiel / php-simple-request example snippets
namespace Your\Namespace;
class PersonRequest
{
private $firstName;
private $lastName;
private $age;
// getters and setters (setters are
namespace Your\Namespace;
use Mcustiel\SimpleRequest\Annotation\Filter\Trim;
use Mcustiel\SimpleRequest\Annotation\Filter\UpperCase;
class PersonRequest
{
/**
* @Trim
*/
private $firstName;
/**
* @Trim
* @UpperCase
*/
private $lastName;
private $age;
// getters and setters (setters are
namespace Your\Namespace;
use Mcustiel\SimpleRequest\Annotation\Filter\Trim;
use Mcustiel\SimpleRequest\Annotation\Filter\UpperCase;
use Mcustiel\SimpleRequest\Annotation\Validator\NotEmpty;
use Mcustiel\SimpleRequest\Annotation\Validator\MaxLength;
use Mcustiel\SimpleRequest\Annotation\Validator\Integer;
class PersonRequest
{
/**
* @Trim
* @NotEmpty
*/
private $firstName;
/**
* @Trim
* @UpperCase
* @NotEmpty
* @MaxLength(32)
*/
private $lastName;
/**
* @Integer
*/
private $age;
// getters and setters (setters are
use Mcustiel\SimpleRequest\RequestBuilder;
use Your\Namespace\PersonRequest;
use Mcustiel\SimpleRequest\Exceptions\InvalidRequestException;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Mcustiel\SimpleRequest\ParserGenerator;
use Mcustiel\SimpleRequest\Services\PhpReflectionService;
use Mcustiel\SimpleRequest\Services\DoctrineAnnotationService;
use Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory;
use Mcustiel\SimpleRequest\FirstErrorRequestParser;
$requestBuilder = new RequestBuilder(
new FilesystemAdapter(),
new ParserGenerator(
new DoctrineAnnotationService(),
new AnnotationParserFactory(),
new PhpReflectionService
)
);
try {
$personRequest = $requestBuilder->parseRequest($_POST, PersonRequest::class);
} catch (InvalidRequestException $e) {
die("The request is invalid: " . $e->getMessage());
}
// Now you can use the validated and filtered personRequest to access the requestData.
use Mcustiel\SimpleRequest\RequestBuilder;
use Your\Namespace\PersonRequest;
use Mcustiel\SimpleRequest\Exceptions\InvalidRequestException;
use Mcustiel\SimpleRequest\AllErrorsRequestParser;
$requestBuilder = new RequestBuilder();
try {
$personRequest = $requestBuilder->parseRequest(
$_POST,
PersonRequest::class,
RequestBuilder::RETURN_ALL_ERRORS_IN_EXCEPTION
);
} catch (InvalidRequestException $e) {
$listOfErrors = $e->getErrors(); // This call returns only one error for the default behavior
}
// Now you can use the validated and filtered personRequest to access the requestData.
use Mcustiel\SimpleRequest\Annotation as SRA;
class CoupleRequest
{
/**
* @SRA\Validator\DateTimeFormat("Y-m-d")
*/
private $togetherSince;
/**
* @SRA\ParseAs("\Your\Namespace\PersonRequest")
*/
private $person1;
/**
* @SRA\ParseAs("\Your\Namespace\PersonRequest")
*/
private $person2;
//... Getters and setters (setters are
$requestBuilder = new RequestBuilder(
new AnyPsr6PoolAdapter(),
new ParserGenerator(new AnnotationReader(), new AnnotationParserFactory())
);
/**
* @Capitalize
*/
private $name;
// Will convert, for instance, mariano to Mariano.
/**
* @Capitalize(true)
*/
private $fullName;
// Will convert, for instance, mariano custiel to Mariano Custiel.
/**
* @CustomFilter(class="Vendor\\App\\MyFilters\\MyFilter", value="yourSpecifier")
*/
private $somethingHardToFilter;
// Will call Vendor\\App\\MyFilters\\MyFilter::filter($value) using "yourSpecifier".
/**
* @DefaultValue("I am a default value")
*/
private $thisCanHaveADefault;
/**
* @RegexReplace(pattern="/[^a-z0-9_]/i", replacement="_")
*/
private $onlyAlnumAndUnderscores;
// Will replace all non alphanumeric characters with underscores.
/**
* @StringReplace(pattern="E", replacement="3")
*/
private $whyAmIChangingThis;
// Will replace all non E with 3.
/**
* @AllOf({@Integer, @Minimum(0), @Maximum(100)})
*/
private $percentage;
// Will match an integer between 0 and 100.
/**
* @Alpha
*/
private $onlyLetters;
// Will match a string containing only letters.
/**
* @AlphaNumeric
*/
private $lettersAndNumbers;
// Will match a string containing only alphanumeric characters.
/**
* @AnyOf({@Integer, @IPV6})
*/
private $integerOrIpv6;
// Will match an integer or an IPV6.
/**
* @CustomValidator(class="Vendor\\App\\MyValidators\\MyValidator", value="yourSpecifier")
*/
private $somethingHardToCheck;
// Will call Vendor\\App\\MyValidators\\MyValidator::validate($value) using "yourSpecifier".
/**
* @AnyOf(@Integer, @IPV6)
*/
private $integerOrIpv6;
// Will match an integer 'xor' an IPV6.
/**
* @Items(properties={"name", @NotEmpty, "age", @Numeric}, additionalProperties=true)
*/
private $person;
// accepts Arrays or objects containing a not empty name property and a numeric age property. Can contain other properties (because additionalProperties is true)