2020-09-15 09:30:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-16 07:54:02 +00:00
|
|
|
namespace BlaxSoftware\LaravelWebSockets;
|
2020-09-15 09:30:17 +00:00
|
|
|
|
2020-09-19 11:16:26 +00:00
|
|
|
use React\Promise\PromiseInterface;
|
|
|
|
|
|
2020-09-15 09:30:17 +00:00
|
|
|
class Helpers
|
|
|
|
|
{
|
2020-09-19 11:16:26 +00:00
|
|
|
/**
|
|
|
|
|
* The loop used to create the Fulfilled Promise.
|
|
|
|
|
*
|
|
|
|
|
* @var null|\React\EventLoop\LoopInterface
|
|
|
|
|
*/
|
|
|
|
|
public static $loop = null;
|
|
|
|
|
|
2020-09-15 09:30:17 +00:00
|
|
|
/**
|
|
|
|
|
* Transform the Redis' list of key after value
|
|
|
|
|
* to key-value pairs.
|
|
|
|
|
*
|
|
|
|
|
* @param array $list
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function redisListToArray(array $list)
|
|
|
|
|
{
|
|
|
|
|
// Redis lists come into a format where the keys are on even indexes
|
|
|
|
|
// and the values are on odd indexes. This way, we know which
|
|
|
|
|
// ones are keys and which ones are values and their get combined
|
|
|
|
|
// later to form the key => value array.
|
|
|
|
|
[$keys, $values] = collect($list)->partition(function ($value, $key) {
|
|
|
|
|
return $key % 2 === 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return array_combine($keys->all(), $values->all());
|
|
|
|
|
}
|
2020-09-19 11:16:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new fulfilled promise with a value.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @return \React\Promise\PromiseInterface
|
|
|
|
|
*/
|
|
|
|
|
public static function createFulfilledPromise($value): PromiseInterface
|
|
|
|
|
{
|
|
|
|
|
$resolver = config(
|
|
|
|
|
'websockets.promise_resolver', \React\Promise\FulfilledPromise::class
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return new $resolver($value, static::$loop);
|
|
|
|
|
}
|
2020-09-15 09:30:17 +00:00
|
|
|
}
|