PHP code example of kingsquare / communibase-databag
1. Go to this page and download the library: Download kingsquare/communibase-databag 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/ */
kingsquare / communibase-databag example snippets
namespace Communibase;
/**
* @author Kingsquare ([email protected])
* @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'));