1. Go to this page and download the library: Download marjask/object-validator 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/ */
marjask / object-validator example snippets
class UserQuery
{
protected string $username;
protected bool $activated;
protected ?DateTime $registerAt;
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function setActivated(bool $activated): self
{
$this->activated = $activated;
return $this;
}
public function setRegisterAt(DateTime $registerAt): self
{
$this->registerAt = $registerAt;
return $this;
}
}
use Marjask\ObjectValidator\AbstractValidator;
use Marjask\ObjectValidator\Constraints\AlsoRequired;
use Marjask\ObjectValidator\Constraints\Length;
use Marjask\ObjectValidator\Constraints\Option\OptionAlsoRequired;
use Marjask\ObjectValidator\Constraints\Option\OptionLength;
use Marjask\ObjectValidator\Constraints\Option\OptionType;
use Marjask\ObjectValidator\Constraints\Type;
use Marjask\ObjectValidator\Constraints\TypeOrNull;
final class UserQueryValidator extends AbstractValidator
{
public function loadConstraints(): void
{
$this->addConstraint(
'username',
new Type(
new OptionType('string')
),
new Length(
new OptionLength(
min: 3,
max: 16
)
)
)
->addConstraint(
'activated',
new Type(
new OptionType('bool')
),
new AlsoRequired(
new OptionAlsoRequired(['username'])
)
)
->addConstraint(
'registerAt',
new TypeOrNull(
new OptionType(DateTime::class)
)
);
}
}