Completed
Push — master ( e1638c...d62258 )
by Peter
07:20
created

ConditionDecorator::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 3
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 https://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 99
	public function __construct(AnnotatedInterface $model = null)
44
	{
45 99
		if (!$model || !$model instanceof AnnotatedInterface)
46
		{
47 97
			return;
48
		}
49
		// Clone is to prevent possible required constructor params issues
50 97
		$this->model = clone $model;
51 97
		$this->meta = ManganMeta::create($this->model);
52 97
	}
53
54 91
	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 91
		if (!$this->model || strstr($field, '.') || $this->meta->$field === false)
59
		{
60
			return [
61 77
				$field => $value
62
			];
63
		}
64
65 79
		$this->model->$field = $value;
66 79
		$data = CriteriaArray::fromModel($this->model, [$field]);
67
68 79
		return $this->_flatten($field, $this->model->$field, $data[$field]);
69
	}
70
71 79
	private function _flatten($field, $srcValue, $data)
72
	{
73 79
		$value = $data;
74 79
		while (is_array($value))
75
		{
76 4
			if(empty($value))
77
			{
78
				break;
79
			}
80
			// Flat value traverse
81 4
			foreach ($value as $key => $val)
82
			{
83 4
				if ($srcValue === $val)
84
				{
85 4
					$value = $value[$key];
86 4
					$field = "$field.$key";
87 4
					break 2;
88
				}
89
			}
90
91
			// Nested value
92
			$key = key($value);
93
			$value = $value[$key];
94
			$field = "$field.$key";
95
			if ($srcValue === $value)
96
			{
97
				break;
98
			}
99
		}
100
101
102
		return [
103 79
			$field => $value
104
		];
105
	}
106
107
}
108