RangeDecorator::useWith()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.9765

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 3
cts 8
cp 0.375
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.9765
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Decorators\QueryBuilder\Operators;
14
15
16
use Maslosoft\Manganel\Interfaces\QueryBuilder\OperatorDecoratorInterface;
17
18
class RangeDecorator implements OperatorDecoratorInterface
19
{
20
21
	/**
22
	 * Whether to use this operator with current key
23
	 * @param string $key
24
	 * @param mixed $value
25
	 */
26 3
	public function useWith($key, $value)
27
	{
28 3
		if (!is_array($value))
29
		{
30 3
			return false;
31
		}
32
		$hasOps = [
33
			array_key_exists('$gt', $value),
34
			array_key_exists('$gte', $value),
35
			array_key_exists('$lt', $value),
36
			array_key_exists('$lte', $value)
37
		];
38
		return array_sum($hasOps) > 0;
39
	}
40
41
	public function decorate(&$condition, $name, $value)
42
	{
43
		$range = [];
44
		foreach ($value as $op => $val)
45
		{
46
			$newOp = str_replace('$', '', $op);
47
			$range[$newOp] = $val;
48
		}
49
		$condition = [
50
			'range' => [
51
				$name => $range
52
			]
53
		];
54
	}
55
}