PHP code example of ddn / jsonobject

1. Go to this page and download the library: Download ddn/jsonobject 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/ */

    

ddn / jsonobject example snippets


class User extends TypedObject {
    const ATTRIBUTES = [
        'id' => 'int',
        'name' => 'string',
        'age' => 'int',
        'emails' => 'list[string]',
        'address' => '?Address',
    ];
}

class Address extends TypedObject {
    const ATTRIBUTES = [
        'street' => 'string',
        'number' => 'int',
        'city' => 'string',
        'country' => 'string',
    ];
}

$user = User::fromObject(json_decode($json_text_definition));

echo($user->name);

$user->age = 50;
$user->emails = [ "[email protected]", "valid.email" ];
$user->address = [ 'street' => "My other street", 'number' => 10, 'city' => "Valencia", 'country' => "Spain" ];

class Address extends TypedObjectSimple {
    public string $street;
    public int $number;
    public string $city;
    public string $country;
}
class User extends TypedObjectSimple {
    public int $id;
    public string $name;
    public int $age;
    public array $emails;
    public ?Address $address;
}

$user = User::fromObject(json_decode($json_text_definition));

class User extends TypedObject {
    const ATTRIBUTES = [
        'id' => 'int',
        'name' => 'string',
        'age' => 'int',
        'emails' => 'list[string]',
        'address' => '?Address',
    ];
    public function isAdult() {
        return $this->age >= 18;
    }
    public function addEmail($email) {
        $this->emails[] = $email;
    }
    public function setAddress($street, $number, $city, $country) {
        $this->address = new Address(street: $street, number: $number, city: $city, country: $country);
    }
}

use ddn\typedobject\TypedObject;

 extends TypedObject {
    const ATTRIBUTES = [
        'id' => 'int',
        'name' => 'string',
        ...
    ];
}

class User extends TypedObject {
    const ATTRIBUTES = [
        'id' => 'int',
        'name' => 'string',
        'age' => 'int',
        'emails' => 'list[string]',
        'address' => '?Address',
    ];
}

$json_text_definition = '{
    "id": 0,
    "name": "John Doe",
    "age": 42,
    "emails": [],
    "address": null
}';
$user = User::fromObject(json_decode($json_text_definition));
$user = User::fromArray(json_decode($json_text_definition, true));
$user = new User(id: 0, name: "John Doe", age: 42, emails: [], address: null);  // PHP 8 and over
$user = new User([ "id" => 0, "name" => "John Doe", "age" => 42, "emails" => [], "address" => null ]);  // PHP 7

class User extends TypedObject {
    const ATTRIBUTES = [
        'id' => 'int',
        'name' => 'string',
        'age',
        'emails',
        'address',
    ];
}

class Address extends TypedObjectSimple {
    public string $street;
    public int $number;
    public string $city;
    public string $country;
}
class User extends TypedObjectSimple {
    public int $id;
    public string $name;
    public int $age;
    public array $emails;
    public ?Address $address;
}

$json_text_definition = '{
    "id": 0,
    "name": "John Doe",
    "age": 42,
    "emails": [],
    "address": {
        "street": "My street",
        "number": 40,
        "city": "Valencia",
        "country": "Spain"
    }
}';
$user = User::fromObject(json_decode($json_text_definition));

class User extends TypedObject {
    const ATTRIBUTES = [
        'id' => [ 'string', 'generate_id' ],
        'name' => [ 'string', 'John Doe' ],
    ];
    function generate_id() {
        return uniqid();
    }
}

class User extends TypedObject {
    const ATTRIBUTES = [
        'id' => 'string',
        'name' => 'string',
    ];
    public $name = "John Doe";
    public $id = "generate_id";
    function generate_id() {
        return uniqid();
    }
}

class User extends TypedObject {
    const ATTRIBUTES = [
        'name' => 'string',
    ];
}
$user = new User();
echo($user->name);

class User extends TypedObject {
    const ATTRIBUTES = [
        'name' => 'string',
    ];
}
$user = new User();
if (!isset($user->name)) {
    echo "Name has not been set";
}
echo $user->name??"Name has not been set";

define('ddn\\typedobject\\USE_UNINITIALIZED_STATE', true);
        'name' => 'string',
    ];
}
$user = new User();
$user->name = "John Doe";
echo($user->name);

define('ddn\\typedobject\\USE_UNINITIALIZED_STATE', false);
       'name' => 'string',
    ];
}
$user = new User();

public function is_initialized() : bool {...}
public function get_uninitialized_attributes() : array {...}

class User extends TypedObject {
    const ATTRIBUTES = [
        'name' => 'string',
        'age' => '?int',
    ];
}
$user = new User();
$user->age = null;

class User extends TypedObject {
    const ATTRIBUTES = [
        'name' => 'string',
        'age' => 'int',
    ];
}
$user = new User();
$user->name = 42;

class UserType extends EnumValues {
    const VALUES = [
        'admin', 'user', 'guest'
    ];
}

class User extends TypedObject {
    const ATTRIBUTES = [
        'name' => 'string',
        'type' => 'enum[UserType]',
    ];
}

$user = new User();
$user->name = "John Doe";
$user->type = "admin";

$user->type = "invalid"; // This will raise an exception

class UserType extends EnumValues {
    const VALUES = [
        0, "user", 1.0, true
    ];
}

$user = new User();
$user->name = "John Doe";
$user->type = 0; // This will work
$user->type = "user"; // This will work
$user->type = 1.0; // This will work
$user->type = true; // This will work

class Vehicle extends TypedObject {
    const ATTRIBUTES = [
        "brand" => "string",
        "color" => "string"
    ]
}
class Car extends Vehicle {
    const ATTRIBUTES = [
        "wheels" => "int"
    ]
}
class Boat extends Vehicle {
    const ATTRIBUTES = [
        "length" => "float"
    ]
}

$json = file_get_contents("car.json");
$vehicle = Vehicle::fromArray((array)json_decode($json, true));

$car = new Car(brand: "BMW", color: "black", wheels: 4);

$car = new Car([ "brand" => "BMW", "color" => "black", "wheels" => 4]);

class User extends TypedObject {
    const ATTRIBUTES = [
        ...
        'phones' => 'dict[string]'
    ];
}
$user = new User();
$user->phones = [ "home" => "123456789", "work" => "987654321" ];

$dict = new TypedDict("int");
$dict["key1"] = 1;
$dict["key2"] = 2;

class User extends TypedObject {
    const ATTRIBUTES = [
        ...
        'phones' => 'list[string]'
    ];
}
$user = new User();
$user->phones = [ "123456789", "987654321" ];
$user->phones[] = "000000000";
echo($user->phones[0]);
echo($user->phones[-1]);

define('ddn\\typedobject\\STRICT_TYPE_CHECKING', false);