MadelineProto/src/danog/MadelineProto/Db/MemoryArray.php

59 lines
1.4 KiB
PHP
Raw Normal View History

2020-04-25 21:57:55 +02:00
<?php
namespace danog\MadelineProto\Db;
2020-04-28 02:41:06 +02:00
use Amp\Producer;
use Amp\Promise;
use function Amp\call;
2020-04-27 02:21:18 +02:00
class MemoryArray extends \ArrayIterator implements DbArray
2020-04-25 21:57:55 +02:00
{
2020-04-27 02:21:18 +02:00
protected function __construct($array = [], $flags = 0)
{
parent::__construct((array) $array, $flags | self::STD_PROP_LIST);
}
2020-04-25 21:57:55 +02:00
2020-05-17 21:04:32 +02:00
public static function getInstance(string $name, $value = null, string $tablePrefix = '', array $settings = []): Promise
2020-04-25 21:57:55 +02:00
{
2020-05-17 21:04:32 +02:00
return call(function() use ($value) {
if ($value instanceof DbArray) {
$value = $value->getArrayCopy();
}
return new static($value);
});
2020-04-25 21:57:55 +02:00
}
2020-04-28 02:41:06 +02:00
2020-05-03 03:33:54 +02:00
public function offsetExists($offset): Promise
2020-04-28 02:41:06 +02:00
{
2020-05-03 03:33:54 +02:00
return call(fn() => parent::offsetExists($offset));
2020-04-28 02:41:06 +02:00
}
2020-05-03 03:33:54 +02:00
public function offsetGet($offset): Promise
2020-04-28 02:41:06 +02:00
{
2020-05-11 19:28:01 +02:00
return call(fn() => parent::offsetExists($offset) ? parent::offsetGet($offset) : null);
2020-04-28 02:41:06 +02:00
}
2020-05-03 03:33:54 +02:00
public function offsetUnset($offset): Promise
2020-04-28 02:41:06 +02:00
{
2020-05-03 03:33:54 +02:00
return call(fn() => parent::offsetUnset($offset));
}
public function count(): Promise
{
return call(fn() => parent::count());
}
public function getArrayCopy(): array
{
return parent::getArrayCopy();
2020-04-28 02:41:06 +02:00
}
public function getIterator(): Producer
{
return new Producer(function (callable $emit) {
2020-05-03 03:33:54 +02:00
foreach ($this as $key => $value) {
yield $emit([$key, $value]);
2020-04-28 02:41:06 +02:00
}
});
}
2020-04-25 21:57:55 +02:00
}