1. Go to this page and download the library: Download gabrielberthier/pieck 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/ */
gabrielberthier / pieck example snippets
use Pieck\Validator;
use Pieck\Attributes\Field;
class Other
{
public function __construct(public readonly string $key) {}
}
class Example
{
public string $defaultSignIn = 'Google';
public function __construct(
public readonly string $name,
public string $occupation,
#[Field(collectionOf: Other::class)]
public array $otherStuff = [],
) {}
}
$result = Validator::validate(Example::class, [
'name' => 'Pieck',
'occupation' => 'Warrior',
'otherStuff' => [['key' => null]],
]);
if ($result->isError()) {
var_dump(($result->getError()->describe()));
} else {
var_dump($result->getSucess());
}
use Pieck\DataClass;
class PieckRequest extends DataClass
{
public function __construct(
public readonly string $name,
public string $occupation,
) {
}
}
try {
$pieckRequest = PieckRequest::modelValidate([
'name' => 'Pieck',
'occupation' => 'Warrior',
]);
dump($pieckRequest->modelDump());
dump($pieckRequest->toJson());
} catch (\Throwable $th) {
dump($th);
}
use Pieck\Validator;
use Pieck\Attributes\Validate;
class Validatable
{
public int $score = 100;
#[Validate]
public function validate(): bool
{
return $this->score > 0;
}
}
use Pieck\Validator;
use Pieck\Attributes\Validate;
class Validatable
{
public int $score = 100;
#[Validate('score')]
public function validate(int $score): bool
{
return $score > 0;
}
}
$validated = Validator::validate(Validatable::class, ['score'=> 100])
use Pieck\Attributes\Validate;
class PieckRequest
{
public function __construct(
public readonly string $name,
) {}
#[Validate(field: 'name')]
public function validateName(string $name): void
{
echo $name.PHP_EOL;
throw new \AssertionError('I wish a different name');
}
}
$result = Validator::validate(PieckRequest::class, [
'name' => 'Pieck',
]);
use Pieck\Rules\RuleInterface;
use Pieck\Attributes\Field;
class NotEmpty implements RuleInterface
{
public function passes($attribute, $value): bool
{
return !empty($value);
}
public function message(): string
{
return 'Message case this fails';
}
}
class PieckRequest
{
#[Field(
rules: [NotEmpty::class]
// OR rules: [new NotEmpty()]
)]
public readonly string $name;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.