PHP code example of zeeloengineering / php-shared-kernel

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

    

zeeloengineering / php-shared-kernel example snippets




use StraTDeS\SharedKernel\Domain\Entity;
use StraTDeS\SharedKernel\Domain\Id;

class Person extends Entity
{
    private $name;
    
    private $surname;

    public function __construct(Id $id, string $name, string $surname)
    {
        parent::__construct($id);
        
        $this->name = $name;
        $this->surname = $surname;
    }
    
    public function getName(): string
    {
        return $this->name;
    }
    
    public function getSurname(): string
    {
        return $this->surname;
    }
}



use StraTDeS\SharedKernel\Domain\Entity;
use StraTDeS\SharedKernel\Domain\Id;
use StraTDeS\SharedKernel\Domain\UUIDV4;

class Person extends Entity
{
    private $name;
    
    private $surname;

    public function __construct(Id $id, string $name, string $surname)
    {
        parent::__construct($id);
        
        $this->name = $name;
        $this->surname = $surname;
        
        $this->recordThat(
            new PersonCreated(
                UUIDV4::generate(),
                $this->getId(),
                $name,
                $surname
            )
        );
    }
    
    public function getName(): string
    {
        return $this->name;
    }
    
    public function getSurname(): string
    {
        return $this->surname;
    }
}

$person = new Person(
    UUIDV4::generate(),
    'Alex',
    'Hernández'
);

$eventStream = $person->pullEventStream();



use StraTDeS\SharedKernel\Domain\EntityCollection;
use StraTDeS\SharedKernel\Domain\UUIDV4;

class PersonCollection extends EntityCollection
{
    
}

$personCollection = new PersonCollection([
    new Person(
        UUIDV4::generate(),
        'Alex',
        'Hernández'
    ),
    new Person(
        UUIDV4::generate(),
        'John',
        'Smith'
    )
]);

$entities = $personCollection->getEntities();




use StraTDeS\SharedKernel\Domain\DomainEvent;
use StraTDeS\SharedKernel\Domain\Id;

class PersonCreated extends DomainEvent
{
    private $name;
    
    private $surname;
    
    public function __construct(Id $id, Id $entityId, string $name, string $surname)
    {
        parent::__construct($id, $entityId);
        
        $this->name = $name;
        $this->surname = $surname;
    }
    
    public function getName(): string
    {
        return $this->name;
    }
    
    public function getSurname(): string
    {
        return $this->surname;
    }
}



use StraTDeS\SharedKernel\Application\UseCase\UseCase;
use StraTDeS\SharedKernel\Application\UseCase\Request;
use StraTDeS\SharedKernel\Application\UseCase\Response;
use StraTDeS\SharedKernel\Domain\Id;
use StraTDeS\SharedKernel\Application\DataTransformer;

class PersonCollectionToArrayDataTransformer implements DataTransformer
{
    /**
     * @param mixed|PersonCollection $data
     * @return array
     */
    public function transform(mixed $data): mixed
    {
        $persons = [];
        
        foreach($data as $person) {
            $persons[] = $person->toArray();
        }
        
        return $persons;
    }
}

class GetUserByIdRequest extends Request
{
    private $id;
    
    public function __construct(Id $id) 
    {
        $this->id = $id;
    }
    
    public function getId(): Id
    {
        return $this->id;
    }
}

class GetUserByIdResponse extends Response
{
    private $persons;
    
    public function __construct(mixed $persons) 
    {
        $this->persons = $persons;
    }
    
    public function getPersons(): mixed
    {
        return $this->persons;
    }
}

class GetUserByIdUseCase extends UseCase
{
    private $dataTransformer;
    
    public function __construct(DataTransformer $dataTransformer) 
    {
        $this->dataTransformer = $dataTransformer;
    }
    
    public function execute(Request $getUserByIdRequest): Response
    {
        $userCollection = //my repository query returns a PersonCollection
        
        return new GetUserByIdResponse($this->dataTransformer->transform($userCollection));
    }
}



use StraTDeS\SharedKernel\Infrastructure\DoctrinePersistentRepository;
use StraTDeS\SharedKernel\Domain\UUIDV4;

class DoctrinePersonRepository extends DoctrinePersistentRepository implements PersonRepository
{
    public function getEntityName(): string
    {
        return Person::class;
    }
}

// person repository creation ...

$person = $personRepository->get(UUIDV4::fromString('6238ec41-71d0-4482-97f5-4c5c4919e635'));
$person->changeName('John');
$personRepository->save($person);