PHP code example of compositephp / entity

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

    

compositephp / entity example snippets


use Composite\Entity\AbstractEntity;

class User extends AbstractEntity
{
    public function __construct(
        public readonly int $id,
        public string $email,
        public ?string $name = null,
        public bool $is_test = false,
        public array $languages = [],
        public Status $status = Status::ACTIVE,
        public readonly \DateTimeImmutable $created_at = new \DateTimeImmutable(),
    ) {}
}

enum Status
{
    case ACTIVE;
    case BLOCKED;
}

$user = new User(
    id: 123,
    email: '[email protected]',
    name: 'John',
    is_test: false,
    languages: ['en', 'fr'],
    status: Status::BLOCKED,
);

var_export($user->toArray());

//will output
array (
  'id' => 123,
  'email' => '[email protected]',
  'name' => 'John',
  'is_test' => false,
  'languages' => '["en","fr"]',
  'status' => 'BLOCKED',
  'created_at' => '2022-01-01 11:22:33.000000',
)

$user = User::fromArray([
  'id' => 123,
  'email' => '[email protected]',
  'name' => 'John',
  'is_test' => false,
  'languages' => '["en","fr"]',
  'status' => 'BLOCKED',
  'created_at' => '2022-01-01 11:22:33.000000',
]);

  use Composite\Entity\AbstractEntity;
  use Composite\Entity\Attributes\ListOf;
  
  class User extends AbstractEntity
  {
      public readonly int $id;
      
      public function __construct(
          public string $name,
          #[ListOf(Setting::class)]
          public array $settings = [],
      ) {}
  }
  
  class Setting extends AbstractEntity
  {
      public function __construct(
          public readonly string $name,
          public bool $isActive,
      ) {}
  }