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\Attributes\{FromAssociativeArray, MakeParsed};
use TryAgainLater\Pup\Attributes\Generic\{ParsedProperty, Required};
use TryAgainLater\Pup\Attributes\Number\Positive;
use TryAgainLater\Pup\Attributes\String\MaxLength;

#[FromAssociativeArray]
class User
{
    #[ParsedProperty]
    #[Required]
    #[MaxLength(8)]
    private string $name;

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

    use MakeParsed;
}

// Creates the object without any issues:
$user = User::from([
    'name' => 'John',
    'age' => 42,
]);

// Throws an exception because of negative age:
$user = User::from([
    'name' => 'John',
    'age' => -42,
]);

// Throws an exception because the name is too long:
$user = User::from([
    'name' => 'John Doe Blah Blah Blah',
    'age' => 42,
]);