PHP code example of gnbit / alabra-entity

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

    

gnbit / alabra-entity example snippets


class UserProfile implements EntityInterface
{
    use EntityTrait;

    private string $username;
    private PersonalInfo $personalInfo;

    public function __construct(
        private string $firstName,
        private string $lastName,
        private string $email,
        private int $age,
        private bool $isSubscribed
    )
    {

    }

    public function setUsername(string $username): void
    {
        $this->username = $username;
    }

    public function setPersonalInfo(PersonalInfo $personalInfo): void
    {
        $this->personalInfo = $personalInfo;
    }
}

        $personalInfo = new PersonalInfo('123 Main St', '555-1234');
        $this->userProfile = new UserProfile('Jhon', 'Smith', '[email protected]', 40, true);
        $this->userProfile->setUsername('other');
        $this->userProfile->setPersonalInfo($personalInfo);

        //Methods 
        $this->userProfile->toArray();
        $this->userProfile->toJson();
        $this->userProfile->getIterator();


use Alabra\Entity\EntityCollection;

// Create an instance of EntityCollection
$collection = new EntityCollection();

// Add objects to the collection
$object1 = new YourEntityClass();
$object2 = new YourEntityClass();

$collection[] = $object1;
$collection[] = $object2;

// Access objects by key or iterate through the collection
foreach ($collection as $key => $object) {
    // Do something with each object
}

// Check if an offset exists
if (isset($collection['some_key'])) {
    // Object with key 'some_key' exists
}

// Remove an object by key
unset($collection['some_key']);

// Get the number of objects in the collection
$count = count($collection);

use Alabra\Entity\EntityCollection;

// Create an instance of EntityCollection
$collection = new EntityCollection();

// Set the key property name
$collection->setKeyPropertyName('id');

// Add objects to the collection
$object1 = new YourEntityClass('A', 'B', 1, true, 1.5, 'key1');
$object2 = new YourEntityClass('C', 'D', 2, false, 2.5, 'key2');

$collection[] = $object1;
$collection[] = $object2;

// Access objects by their key property
$objectByKey = $collection['key1']; // Returns $object1

use Alabra\Entity\EntityCollection;

// Create two instances of EntityCollection
$collection1 = new EntityCollection();
$collection2 = new EntityCollection();

// Merge collections
$collection1->merge($collection2->toArray());

use Alabra\Entity\EntityCollection;

// Create an instance of EntityCollection
$collection = new EntityCollection();

// Add objects to the collection
$object1 = new YourEntityClass('A', 'B', 1, true, 1.5);
$object2 = new YourEntityClass('C', 'D', 2, false, 2.5);

$collection[] = $object1;
$collection[] = $object2;

// Convert the collection to an array
$arrayRepresentation = $collection->toArray();