Source of file WorkerCommandTrait.php
Size: 3,898 Bytes - Last Modified: 2021-01-12T22:04:13+00:00
C:/Users/MAKS/Code/_PROJECTS/amqp-agent/src/Worker/WorkerCommandTrait.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
Covered by 7 test(s):
4748
Covered by 7 test(s):
495051
Covered by 7 test(s):
52
Covered by 7 test(s):
53
Covered by 7 test(s):
5455
Covered by 7 test(s):
56
Covered by 2 test(s):
57585960
Covered by 7 test(s):
61626364656667686970
Covered by 6 test(s):
7172
Covered by 6 test(s):
7374
Covered by 6 test(s):
757677787980818283848586
Covered by 4 test(s):
87
Covered by 4 test(s):
8889
Covered by 4 test(s):
90
Covered by 4 test(s):
91
Covered by 4 test(s):
9293
Covered by 4 test(s):
94
Covered by 3 test(s):
959697
Covered by 4 test(s):
9899100101102103104105106107108109
Covered by 2 test(s):
110
Covered by 2 test(s):
111112
Covered by 2 test(s):
113
Covered by 2 test(s):
114115
Covered by 2 test(s):
116
Covered by 2 test(s):
117
Covered by 2 test(s):
118
Covered by 2 test(s):
119120
Covered by 2 test(s):
121
Covered by 1 test(s):
122
Covered by 1 test(s):
123
Covered by 1 test(s):
124125126
Covered by 2 test(s):
127128129
| <?php /** * @since 1.0.0 * @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com> * @copyright Marwan Al-Soltany 2020 * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ declare(strict_types=1); namespace MAKS\AmqpAgent\Worker; use MAKS\AmqpAgent\Config\AmqpAgentParameters; /** * A trait containing the implementation of the workers command interface/functions. * @since 1.0.0 */ trait WorkerCommandTrait { /** * The prefix that should be used to define an array as a command. * @var string */ public static $commandPrefix = AmqpAgentParameters::COMMAND_PREFIX; /** * The recommended way of defining a command array. * @var array */ public static $commandSyntax = AmqpAgentParameters::COMMAND_SYNTAX; /** * Constructs a command from passed data to a command array following the recommended pattern. * @param string $name The name of the command. * @param string $value The value of the command. * @param mixed $parameters [optional] Additional parameters to add to the command. * @param string $argument [optional] The key to use to store the parameters under. * @return array */ public static function makeCommand(string $name, string $value, $parameters = null, string $argument = 'params'): array { $prefix = static::$commandPrefix; $result = [ $prefix => [] ]; if ($name && $value) { $result[$prefix] = [ $name => $value ]; if ($parameters) { $result[$prefix][$argument] = $parameters; } } return $result; } /** * Checks whether an array is a command following the recommended pattern. * @param mixed $data The data that should be checked. * @return bool */ public static function isCommand($data): bool { $prefix = static::$commandPrefix; $result = $data && is_array($data) && array_key_exists($prefix, $data); return $result; } /** * Checks whether a specific command (command name) exists in the command array. * @param array $data The array that should be checked. * @param string|null $name The name of the command. * @param string|null $value The value of the command. * @return bool */ public static function hasCommand(array $data, string $name = null, ?string $value = null): bool { $prefix = static::$commandPrefix; $result = static::isCommand($data); $result = ($result && $name && array_key_exists($name, $data[$prefix])) ? true : $result; if ($result && $name && $value) { $result = isset($data[$prefix][$name]) && $data[$prefix][$name] === $value; } return $result; } /** * Returns the content of a specific key in the command array, used for example to get the additional parameters. * @param array $data The array that should be checked. * @param string $key [optional] The array key name. * @param string|null $sub [optional] The array nested array key name. * @return mixed */ public static function getCommand(array $data, string $key = 'params', ?string $sub = null) { $prefix = static::$commandPrefix; $result = static::isCommand($data); if ($result) { $result = $data[$prefix]; } if ($result && $key) { $result = array_key_exists($key, $data[$prefix]) ? $data[$prefix][$key] : null; } if ($result && $sub) { $result = array_key_exists($sub, $data[$prefix][$key]) ? $data[$prefix][$key][$sub] : null; } return $result; } } |