Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pool.php
1<?php
3
8class Pool
9{
11 protected $poolSize = 0;
13 protected $items = [];
14
20 public function __construct(int $poolSize)
21 {
22 $this->poolSize = $poolSize;
23 }
24
28 public function getItems(): array
29 {
30 return $this->items;
31 }
32
33 public function cleanItems(): void
34 {
35 $this->items = [];
36 }
37
41 public function setItems(array $items): void
42 {
43 $this->items = $items;
44 }
45
50 public function getItem(string $index)
51 {
52 $result = null;
53
54 if(isset($this->items[$index]))
55 {
56 $result = $this->items[$index];
57 //come up used items
58 unset($this->items[$index]);
59 $this->items[$index] = $result;
60 }
61
62 return $result;
63 }
64
69 public function addItem(string $index, $value): void
70 {
71 $this->items[$index] = $value;
72 $delta = count($this->items) - $this->poolSize;
73
74 if($delta > 0)
75 {
76 $this->items = $this->decreaseSize($delta, $this->items);
77 }
78 }
79
83 public function getItemsCount(): int
84 {
85 return count($this->items);
86 }
87
91 public function deleteItem(string $index): void
92 {
93 if(isset($this->items[$index]))
94 {
95 unset($this->items[$index]);
96 }
97 }
98
104 protected function decreaseSize(int $delta, array $items): array
105 {
106 if($delta <= 0 || count($items) <= 0)
107 {
108 return $items;
109 }
110
111 do
112 {
113 reset($items);
114 unset($items[key($items)]);
115 $delta--;
116 }
117 while($delta > 0);
118
119 return $items;
120 }
121}
addItem(string $index, $value)
Definition pool.php:69
setItems(array $items)
Definition pool.php:41
getItem(string $index)
Definition pool.php:50
__construct(int $poolSize)
Definition pool.php:20
deleteItem(string $index)
Definition pool.php:91
decreaseSize(int $delta, array $items)
Definition pool.php:104