Completed
Push — master ( b4f16a...a23940 )
by Peter
20:17
created

ConditionDecorator::_flatten()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.1941

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 31
ccs 10
cts 18
cp 0.5556
rs 8.439
cc 5
eloc 15
nc 6
nop 3
crap 7.1941
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link http://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Criteria;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Interfaces\ConditionDecoratorInterface;
18
use Maslosoft\Mangan\Meta\ManganMeta;
19
use Maslosoft\Mangan\Transformers\CriteriaArray;
20
21
/**
22
 * This class is used to decorate and sanitize conditions.
23
 * This should not be used directly. This is internal component of Criteria.
24
 *
25
 * @internal Criteria sub component
26
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
27
 */
28
class ConditionDecorator implements ConditionDecoratorInterface
29
{
30
31
	/**
32
	 * Model instance
33
	 * @var AnnotatedInterface
34
	 */
35
	private $model = null;
36
37
	/**
38
	 * Metadata
39
	 * @var ManganMeta
40
	 */
41
	private $meta = null;
42
43 83
	public function __construct(AnnotatedInterface $model = null)
44 1
	{
45 83
		if (!$model || !$model instanceof AnnotatedInterface)
46 83
		{
47 83
			return;
48
		}
49
		// Clone is to prevent possible required constructor params issues
50 64
		$this->model = clone $model;
51 64
		$this->meta = ManganMeta::create($this->model);
52 64
	}
53
54 79
	public function decorate($field, $value = null)
55
	{
56
		// 1. Do not decorate if empty model or dot notation
57
		// 2. Ignore non existing fields
58 79
		if (!$this->model || strstr($field, '.') || $this->meta->$field === false)
59 79
		{
60
			return [
61
				$field => $value
62 70
			];
63 1
		}
64
65 23
		$this->model->$field = $value;
66 23
		$data = CriteriaArray::fromModel($this->model, [$field]);
67
68 23
		return $this->_flatten($field, $this->model->$field, $data[$field]);
69
	}
70
71 23
	private function _flatten($field, $srcValue, $data)
72
	{
73 23
		$value = $data;
74 23
		while (is_array($value))
75
		{
76
			// Flat value traverse
77 3
			foreach ($value as $key => $val)
78
			{
79 3
				if ($srcValue === $val)
80 3
				{
81 3
					$value = $value[$key];
82 3
					$field = "$field.$key";
83 3
					break 2;
84
				}
85
			}
86
87
			// Nested value
88
			$key = key($value);
89
			$value = $value[$key];
90
			$field = "$field.$key";
91
			if ($srcValue === $value)
92
			{
93
				break;
94
			}
95
		}
96
97
98
		return [
99
			$field => $value
100 23
		];
101
	}
102
103
}
104