kingsquare/communibase-databag

Communibase dataBag

v2.1.0 2020-10-23 19:16 UTC

This package is auto-updated.

Last update: 2024-10-26 16:52:25 UTC


README

Build Status Scrutinizer Code Quality

It's a bag, for CB data. If we need to create a CB object from CB data (array) we can use this dataBag object as a private entity class property. The dataBag can contain one or more entities. For each entity we can get/set properties by path. If we need to persist the entity back into CB use getState to fetch the (updated) data array.

namespace Communibase;

/**
 * @author Kingsquare (source@kingsquare.nl)
 * @copyright Copyright (c) Kingsquare BV (http://www.kingsquare.nl)
 */
class ExamplePerson
{
    /**
     * @var DataBag
     */
    private $databag;

    /**
     * @param array<string,mixed> $personData
     */
    private function __construct(array $personData)
    {
        $this->databag = DataBag::fromEntityData('person', $personData);
    }

    /**
     * @param array<string,mixed> $personData
     */
    public static function fromPersonData(array $personData): ExamplePerson
    {
        return new self($personData);
    }

    public function getFirstName(): string
    {
        return (string)$this->databag->get('person.firstName');
    }

    public function setFirstName(string $firstName): void
    {
        $this->databag->set('person.firstName', $firstName);
    }

    /**
     * @return array<string,mixed>
     */
    public function getState(): array
    {
        return $this->databag->getState('person');
    }
}

// $person = $personRepository->findById($communibasePersonId); // returns ExamplePerson::fromPersonData($fetchedCbData)
// $person->setFirstName('John');
// $personRepository->persist($person); // does $cbConnector->update('Person', $person->getState('person'));