1. Go to this page and download the library: Download jasny/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/ */
jasny / entity example snippets
namespace App;
use Jasny\Entity\IdentifiableEntityInterface;
use Jasny\Entity\IdentifiableEntityTraits;
/**
* A user in our system
*/
class User extends AbstractIdentifiableEntity
{
/** @var string */
public $id;
/** @var string */
public $name;
/** @var string */
public $email;
/** @var string */
public $password_hash;
}
use App\User;
use Jasny\Entity\Event;
$data = $db->prepare("SELECT * FROM user WHERE id = ?")->execute($id)->fetch(PDO::FETCH_ASSOC);
$user = User::fromData($data);
$user->addEventListener(function(Event\ToJson $event): void {
$data = $event->getPayload();
unset($data['password_hash']);
$event->setPaypload($data);
});
header('Content-Type: application\json');
echo json_serialize($user);
class Color extends AbstractBasicEntity
{
/** @var int */
public $red;
/** @var int */
public $green;
/** @var int */
public $blue;
}
class User extends AbstractIdentifiableEntity
{
/** @var int */
public $id;
/** @var string */
public $name;
/** @var string */
public $email;
/** @var string */
public $password_hash;
}
class Invoice extends AbstractIdentifiableEntity
{
/** @var string */
public $number;
// ...
protected static function getIdProperty(): string
{
return 'number';
}
}
class User extends AbstractIdentifiableEntity implements DynamicEntity
{
// ...
}
$user = new User(); // This represents a new user in the system
$data = $db->prepare("SELECT * FROM user WHERE id = ?")->execute($id)->fetch(PDO::FETCH_ASSOC);
$user = User::fromData($data);