Completed
Push — master ( 5a683a...37dbab )
by Peter
09:05
created

SearchClassNameDecorator::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Maslosoft\Manganel\Decorators;
4
5
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
6
use Maslosoft\Mangan\Interfaces\Decorators\Model\ModelDecoratorInterface;
7
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
8
use Maslosoft\Manganel\Meta\ManganelMeta;
9
10
/**
11
 * SearchClassNameDecorator
12
 *
13
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
14
 */
15
class SearchClassNameDecorator implements ModelDecoratorInterface
16
{
17
18
	/**
19
	 * This will be called when getting value.
20
	 * This should return end user value.
21
	 * @param AnnotatedInterface $model Document model which will be decorated
22
	 * @param mixed $dbValues
23
	 * @param string $transformatorClass Transformator class used
24
	 * @return bool Return true if value should be assigned to model
25
	 */
26 36
	public function read($model, &$dbValues, $transformatorClass = TransformatorInterface::class)
27
	{
28
29 36
	}
30
31
	/**
32
	 * This will be called when setting value.
33
	 * This should return db acceptable value
34
	 * @param AnnotatedInterface $model Model which is about to be decorated
35
	 * @param mixed[] $dbValues Whole model values from database. This is associative array with keys same as model properties (use $name param to access value). This is passed by reference.
36
	 * @param string $transformatorClass Transformator class used
37
	 * @return bool Return true to store value to database
38
	 */
39 60
	public function write($model, &$dbValues, $transformatorClass = TransformatorInterface::class)
40
	{
41
		// Do not store class names for homogenous collections
42
		/**
43
		 * TODO Below code conflicts with some features
44
		 */
45
//		if ($transformatorClass === RawArray::class)
46
//		{
47
//			$isHomogenous = ManganMeta::create($model)->type()->homogenous;
48
//			if ($isHomogenous)
49
//			{
50
//				return;
51
//			}
52
//		}
53 60
		$enforcedType = ManganelMeta::create($model)->type()->type;
54 60
		if (!empty($enforcedType))
55
		{
56 1
			$dbValues['_class'] = $enforcedType;
57
		}
58
		else
59
		{
60 59
			$dbValues['_class'] = get_class($model);
61
		}
62 60
	}
63
64
}
65