PHP code example of 021 / api-entity

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

    

021 / api-entity example snippets


use O21\ApiEntity\BaseEntity;
use O21\ApiEntity\Casts\Getter;
use SDK\Entities\UserProfile; // Your custom class
use SDK\Entities\UserPet; // Your custom class

use function O21\ApiEntity\Response\json_props;

/**
 * Class User
 * @package SDK\Entities
 *
 * @property int $id
 * @property string $firstName
 * @property string $lastName
 * @property \Carbon\Carbon $registerAt
 * @property UserProfile $profile
 * @property \Illuminate\Support\Collection<UserPet> $pets
 * @property-read string $fullName
 */
class User extends BaseEntity
{
    public function fullName(): Getter
    {
        return Getter::make(fn() => $this->firstName.' '.$this->lastName);
    }
}

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $api->get('/user/1');

// Get decoded JSON array from response
// which is a PSR-7 response or JSON string
$props = json_props($response);
// Create User object from JSON props
$user = new User($props);

// Or just pass response to BaseEntity constructor
$user = new User($response);

echo $user->fullName; // John Doe
echo $user->full_name; // John Doe
echo $user->registerAt->format('Y-m-d H:i:s'); // 2022-01-01 00:00:00
echo $user->profile->phone; // +1234567890
echo $user->pets->first()->name; // Archy

use O21\ApiEntity\BaseEntity;
use O21\ApiEntity\Casts\Getter;
use SDK\Entities\UserProfile; // Your custom class
use SDK\Entities\UserPet; // Your custom class

use function O21\ApiEntity\Response\json_props;

/**
 * Class User
 * @package SDK\Entities
 *
 * @property int $id
 * @property string $firstName
 * @property string $lastName
 * @property \Carbon\Carbon $registerAt
 * @property UserProfile $profile
 * @property \Illuminate\Support\Collection<UserPet> $pets
 * @property-read string $fullName
 */
class User extends BaseEntity
{
    protected array $casts = [
        'registerAt' => 'datetime',
        'profile' => UserProfile::class,
        'pets' => 'collection:'.UserPet::class,
    ];

    public function fullName(): Getter
    {
        return Getter::make(fn() => $this->firstName.' '.$this->lastName);
    }
}

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $api->get('/user/1');

// Get decoded JSON array from response
// which is a PSR-7 response or JSON string
$props = json_props($response);
// Create User object from JSON props
$user = new User($props);

// Or just pass response to BaseEntity constructor
$user = new User($response);

echo $user->fullName; // John Doe
echo $user->full_name; // John Doe
echo $user->registerAt->format('Y-m-d H:i:s'); // 2022-01-01 00:00:00
echo $user->profile->phone; // +1234567890
echo $user->pets->first()->name; // Archy