1. Go to this page and download the library: Download thepublicgood/yerp 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/ */
thepublicgood / yerp example snippets
use TPG\Yerp\Rules;
class User
{
#[Rules\Required]
public string $firstName;
#[Rules\Nullable]
public ?string $lastName = null;
#[Rules\Required, Rules\Email]
public string $emailAddress;
#[Rules\Boolean(true)]
public boolean $active;
}
class UserController
{
public function create()
{
// Create a new object instance
$user = new User();
$user->firstName = 'John';
$user->lastName = 'Doe';
$user->emailAddress = '[email protected]';
$user->active = false;
// Pass to the validator
$validated = (new Validator($user))->validate();
}
}
$validated = (new Validator($user))->validate();
$validated->property('firstName')->passed();
namespace CustomRules;
use Attribute;
use TPG\Yerp\Rules\AbstractRule;
use TPG\Yerp\Result;
#[Attribute]
class HyphenatedRule extends AbstractRule
{
public function validate(mixed $value): Result
{
return $this->getResult(str_contains((string)$value, '-'));
}
}
use CustomRules\Hyphenated;
class Article
{
public string $title;
#[HyphenatedRule]
public string $slug;
}
namespace CustomRules;
use Attribute;
use TPG\Yerp\Rules\AbstractRule;
use TPG\Yerp\Result;
use DelimitedRule extends AbstractRule
{
public function __construct(protected string $delimiter = ',')
{
}
public function validate(string $value): Result
{
return $this->getResult(str_contains($value, $this->delimited));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.