use Respect\Validation\Validator as v; class User { public $name; public $email; public $age; public function __construct($name, $email, $age) { $this->name = $name; $this->email = $email; $this->age = $age; } } $user = new User('John', 'john@example.com', 25); $v = v::key('name', v::stringType()->notEmpty()) ->key('email', v::email()) ->key('age', v::intVal()->between(18, 60)); $result = $v->validate((array) $user); var_dump($result); // true
use Respect\Validation\Validator as v; $user1 = new User('John', 'john@example.com', 25); $user2 = new User('Jane', 'jane@example.com', 24); $v = v::arrayType() ->each( v::key('name', v::stringType()->notEmpty()) ->key('email', v::email()) ->key('age', v::intVal()->between(18, 60)) ); $users = [$user1, $user2]; $result = $v->validate($users); var_dump($result); // trueIn this example, we have an array of user objects that we want to validate. We use the `v::arrayType()` method to validate that the input is an array and the `v::each()` method to validate each element of the array. Inside the `each()` method, we define the validation rules for each property of the user object using `v::key()`. The result is `true` because both user objects pass the validation rules. The package library that provides the PHP object validate functionality is Respect\Validation.