Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: admin
  5.  * Date: 10.06.2018
  6.  * Time: 20:11
  7.  */
  8.  
  9. namespace App\Repository;
  10.  
  11.  
  12. class FastRepository
  13. {
  14.     protected $itemsById = [];
  15.  
  16.     protected $indexes = [];
  17.     /**
  18.      * @param string $indexField
  19.      */
  20.     function createIndex($indexField){
  21.         $this->indexes[$indexField] = [];
  22.         foreach ($this->itemsById as $item){
  23.             $this->addToIndex($indexField, $item);
  24.         }
  25.     }
  26.  
  27.     function removeIndex($indexField){
  28.         unset($this->indexes[$indexField]);
  29.     }
  30.  
  31.     function add(FastRepositoryEntity $entity){
  32.         $entityId = $entity->getId();
  33.         $this->itemsById[$entityId] = $entity;
  34.  
  35.         foreach ($this->getIndexes() as $indexField){
  36.             $this->addToIndex($indexField, $entity);
  37.         }
  38.     }
  39.  
  40.     /**
  41.      * @param $indexField
  42.      * @param $value
  43.      * @return FastRepositoryEntity[]|null
  44.      */
  45.     function get($indexField, $value){
  46.         if(!isset($this->indexes[$indexField])){
  47.             return null;
  48.         }
  49.         if(empty($this->indexes[$indexField][$value])){
  50.             return null;
  51.         }
  52.         return $this->indexes[$indexField][$value];
  53.     }
  54.  
  55.     function remove(FastRepositoryEntity $entity){
  56.         if(strpos($entity->getValue('name'), '802WK') !== false){
  57.             $breradsf = 1;
  58.         }
  59.  
  60.         $entityId = $entity->getId();
  61.         unset($this->itemsById[$entityId]);
  62.  
  63.         foreach ($this->indexes as $index => &$entities){
  64.             $indexValue = $entity->getValue($index);
  65.             unset($entities[$indexValue][$entityId]);
  66.         }
  67.     }
  68.  
  69.     function getItemsById(){
  70.         return $this->itemsById;
  71.     }
  72.  
  73.     function getIndexes(){
  74.         return array_keys($this->indexes);
  75.     }
  76.  
  77.     protected function addToIndex($indexField, FastRepositoryEntity $entity){
  78.         $key = $entity->getValue($indexField);
  79.         if(!isset($this->indexes[$indexField][$key])){
  80.             $this->indexes[$indexField][$key] = [];
  81.         }
  82.         $this->indexes[$indexField][$key][$entity->getId()] = $entity;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement