Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: admin
- * Date: 10.06.2018
- * Time: 20:11
- */
- namespace App\Repository;
- class FastRepository
- {
- protected $itemsById = [];
- protected $indexes = [];
- /**
- * @param string $indexField
- */
- function createIndex($indexField){
- $this->indexes[$indexField] = [];
- foreach ($this->itemsById as $item){
- $this->addToIndex($indexField, $item);
- }
- }
- function removeIndex($indexField){
- unset($this->indexes[$indexField]);
- }
- function add(FastRepositoryEntity $entity){
- $entityId = $entity->getId();
- $this->itemsById[$entityId] = $entity;
- foreach ($this->getIndexes() as $indexField){
- $this->addToIndex($indexField, $entity);
- }
- }
- /**
- * @param $indexField
- * @param $value
- * @return FastRepositoryEntity[]|null
- */
- function get($indexField, $value){
- if(!isset($this->indexes[$indexField])){
- return null;
- }
- if(empty($this->indexes[$indexField][$value])){
- return null;
- }
- return $this->indexes[$indexField][$value];
- }
- function remove(FastRepositoryEntity $entity){
- if(strpos($entity->getValue('name'), '802WK') !== false){
- $breradsf = 1;
- }
- $entityId = $entity->getId();
- unset($this->itemsById[$entityId]);
- foreach ($this->indexes as $index => &$entities){
- $indexValue = $entity->getValue($index);
- unset($entities[$indexValue][$entityId]);
- }
- }
- function getItemsById(){
- return $this->itemsById;
- }
- function getIndexes(){
- return array_keys($this->indexes);
- }
- protected function addToIndex($indexField, FastRepositoryEntity $entity){
- $key = $entity->getValue($indexField);
- if(!isset($this->indexes[$indexField][$key])){
- $this->indexes[$indexField][$key] = [];
- }
- $this->indexes[$indexField][$key][$entity->getId()] = $entity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement