PHP code example of abbasghasemi / easy-data-model
1. Go to this page and download the library: Download abbasghasemi/easy-data-model 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/ */
abbasghasemi / easy-data-model example snippets
include_once 'vendor/autoload.php';
use AG\Collection\ArrayList;
use AG\DataModel\EnumBuilder;
use AG\DataModel\Ignore;
use AG\DataModel\ModelBuilder;
use AG\DataModel\ModelBuilderException;
use AG\DataModel\PropertyNullable;
use AG\DataModel\Safe;
use AG\DataModel\ValueConvertor;
enum Number implements JsonSerializable
{
use EnumBuilder;
case one;
case two;
public function jsonSerialize(): string {
return match ($this) {
Number3::one => 'one',
Number3::two => 'two',
};
}
}
enum Number2: string
{
case one = "one";
case two = "two";
}
class SplitData
{
#[Safe(min: 3, max: 30)]
public string $name;
#[Safe(min: 1, max: 1)]
public int $count;
}
class Book
{
private int $id; // Ignore
#[Ignore]
public string $name; // Ignore
#[Safe(alternate: ['is_article'])]
public bool $isArticle; // non-null and searches for is_article key
public ?string $description; // allow null
#[Ignore]
private ?bool $isAvailable; // Ignore private
}
class Items extends ModelBuilder implements PropertyNullable, ValueConvertor
{
#[Safe(min: 3, max: 30)]
public string $name;
#[Safe(min: 1, max: 1)]
public int $count;
public Book $book;
#[Safe(alternate: [])]
public SplitData $splitData;
#[Safe(pattern: '/^\w+$/i', max: 6, overflow: true)]
public string $text;
#[Safe(max: 5, type: 'int')]
public mixed $meta;
/**
* @var ArrayList<Book>
*/
#[Safe(max: 3, type: Book::class)]
public ArrayList $books;
public int $id;
public int|string $flag;
#[Safe(alternate: ['count'], type: 'int', convertor: true)]
public Number2 $convertor;
public ?Number2 $number2;
protected Number $number;
public function onNullable(string $propertyName): bool
{
if ('number2' === $propertyName) {
return false; // can't be null
}
return true;
}
function onConvert(string $propertyName, mixed $propertyValue): mixed
{
if ($propertyName === 'convertor') {
return Number2::one;
}
return null;
}
}
// Valid sample
$data = [
'name' => 'test name',
'count' => '1', // try to convert to a number.
'book' => [
'is_article' => true,
'isAvailable' => '1', // try to convert to a boolean,
'description' => 48999696 // convert to '48999696'
],
'text' => 'test_text', // values greater than 6 are ignored
'meta' => [1, 2, 3, 4, 5], // allow null,empty and maxLength 5.
'books' => [
[
'is_article' => true,
'description' => 'Managed by ArrayList'
],
[
'is_article' => false,
]
],
'id' => 3.9, // convert to 3
'flag' => '1578788', // is string
'number2' => 'two',
'number' => 'one'
];
$item = new Items($data); // or ModelBuilder::fromArray($data, objectOrClass);
echo '<pre>';
echo $item->books->first()->description . '<br>';
echo json_encode($item, JSON_PRETTY_PRINT);
echo '<br>';
/*
* Output: Success
{
"name": "test name",
"count": 1,
"book": {
"isArticle": true,
"description": "48999696",
"isAvailable": true
},
"splitData": {
"name": "test name",
"count": 1
},
"text": "test_t",
"meta": [
1,
2,
3,
4,
5
],
"books": [
{
"isArticle": true,
"description": "Managed by ArrayList"
"isAvailable": null
},
{
"isArticle": false,
"description": null,
"isAvailable": null
}
],
"id": 3,
"flag": "1578788",
"convertor": "one",
"number2": "two"
}
*/
// Invalid sample
echo '<br>Invalid sample<br>';
$data = [
'name' => 'te',
'count' => '2',
'book' => [
'is_article' => 'fAlsE',
'isAvailable' => 'Falsee',
],
'text' => 'test^text',
'meta' => [1, 2, 3, 4, 5, 6],
'books' => [1, 2],
'number' => 'three'
];
try {
$item = new Items($data);
echo $item->name;
} catch (ModelBuilderException $e) {
// echo $e->class;
// echo $e->property;
// echo $e->propertyValue ?? 'Empty';
echo $e->getMessage();
// Output: Failed
// The value of 'Falsee' is invalid for parameter 'isAvailable' in `Book`.
// The value of 'te' is invalid for parameter 'name' in `Items`.
// The value of '2' is invalid for parameter 'count' in `Items`.
// The value of 'test^text' is invalid for parameter 'text' in `Items`.
// The value of 'Array' is invalid for parameter 'meta' in `Items`.
// The value of 'Array' is invalid for parameter 'books' in `Items`.
// The parameter 'id' is
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.