Created
January 15, 2016 07:20
-
-
Save Tom32i/54876b5236d477a31126 to your computer and use it in GitHub Desktop.
Doctrine Event Inventory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace EventBundle\Utils; | |
/** | |
* Inventory | |
*/ | |
class Inventory | |
{ | |
/** | |
* Entities | |
* | |
* @var array | |
*/ | |
private $entities; | |
/** | |
* Change sets | |
* | |
* @var array | |
*/ | |
private $changeSets; | |
/** | |
* Identifiers | |
* | |
* @var array | |
*/ | |
private $identifiers; | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
$this->entities = []; | |
$this->changeSets = []; | |
$this->identifiers = []; | |
} | |
/** | |
* Set change set for the given entity | |
* | |
* @param mixed $entity | |
* @param array $changeSet | |
*/ | |
public function setChangeSet($entity, array $changeSet) | |
{ | |
$this->changeSets[$this->index($entity)] = $changeSet; | |
} | |
/** | |
* Get change set for the given entity | |
* | |
* @param mixed $entity | |
* | |
* @return array | |
*/ | |
public function getChangeSet($entity) | |
{ | |
if (false !== $index = $this->search($entity)) { | |
return $this->changeSets[$index]; | |
} | |
return []; | |
} | |
/** | |
* Set identifier for the given entity | |
* | |
* @param mixed $entity | |
* @param array $identifier | |
*/ | |
public function setIdentifiers($entity, array $identifier) | |
{ | |
$this->identifiers[$this->index($entity)] = $identifier; | |
} | |
/** | |
* Get identifier for the given entity | |
* | |
* @param mixed $entity | |
* | |
* @return array | |
*/ | |
public function getIdentifiers($entity) | |
{ | |
if (false !== $index = $this->search($entity)) { | |
return $this->identifiers[$index]; | |
} | |
return []; | |
} | |
/** | |
* Store an entity in the list and return its index | |
* | |
* @param mixed $entity | |
* | |
* @return integer | |
*/ | |
private function index($entity) | |
{ | |
if (!in_array($entity, $this->entities)) { | |
$this->entities[] = $entity; | |
} | |
return $this->search($entity); | |
} | |
/** | |
* Get the index of the given entity in the list | |
* | |
* @param mixed $entity | |
* | |
* @return integer | |
*/ | |
private function search($entity) | |
{ | |
return array_search($entity, $this->entities); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment