PHP code example of try-again-later / pup

1. Go to this page and download the library: Download try-again-later/pup 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/ */

    

try-again-later / pup example snippets


use TryAgainLater\Pup\Schema;

$userSchema = Schema::associativeArray([
    'name' => Schema::string()
        ->' => Schema::string()
        ->    ->oneOf('male', 'female'),

    'allowSendingEmails' => Schema::bool()
        ->default(false)
        ->test(
            name: 'Is string bool',
            check: fn ($string) => in_array($string, ['true', 'false'], strict: true),
            message: 'Only "true" or "false" strings are allowed.',
            shortCircuit: true,
        )
        ->transform(fn ($string) => match ($string) {
            'true', => true,
            'false' => false,
        }),
]);

$user = [
    'name' => 'John',
    'age' => 42,
    'email' => '[email protected]',
    'sex' => 'male',
    'allowSendingEmails' => 'true',
];

// validatedUser: [
//   name               => 'NAME = John',
//   age                => -42,
//   email              => '[email protected]',
//   website            => 'No website',
//   sex                => 'male',
//   allowSendingEmails => bool(true)
// ]

// errors: [
//   ['age', 'The number must be greater than 0']
// ]
[$validatedUser, $errors] = $userSchema->validate($user)->tryGet();

use TryAgainLater\Pup\Attributes\{FromAssociativeArray, MakeParsed};
use TryAgainLater\Pup\Attributes\Generic\{OneOf, ParsedProperty, Required, Transform, Test};
use TryAgainLater\Pup\Attributes\Number\Positive;
use TryAgainLater\Pup\Attributes\String\MaxLength;

#[FromAssociativeArray]
class User
{
    public static function transformName(string $name): string
    {
        return "NAME = $name";
    }

    #[ParsedProperty]
    #[Required]
    #[Transform([self::class, 'transformName'])]
    private string $name;

    #[ParsedProperty]
    #[Positive]
    private int $age;

    #[ParsedProperty]
    #[Required]
    #[MaxLength(255)]
    private string $email;

    #[ParsedProperty]
    private string $website = 'No website';

    #[ParsedProperty]
    #[OneOf('male', 'female')]
    private ?string $sex = null;

    public static function validateBoolString(string $string): bool
    {
        return in_array($string, ['true', 'false'], strict: true);
    }

    public static function stringToBool(string $string): bool
    {
        return match ($string) {
            'true', => true,
            'false' => false,
        };
    }

    #[ParsedProperty('emails')]
    #[Test(
        name: 'Is string bool',
        check: [self::class, 'validateBoolString'],
        message: 'Only "true" or "false" strings are allowed.',
        shortCircuit: true,
    )]
    #[Transform([self::class, 'stringToBool'])]
    private bool $allowSendingEmails = false;

    use MakeParsed;
}

// Throws InvalidArgumentException with the following message:
// "[age] => The number must be greater than 0."
$user = User::from([
    'name' => 'John',
    'age' => -42,
    'email' => '[email protected]',
    'sex' => 'male',
    'emails' => 'true',
]);