SearchClassNameDecorator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 51
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 4 1
A write() 0 25 2
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;
14
15
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
16
use Maslosoft\Mangan\Interfaces\Decorators\Model\ModelDecoratorInterface;
17
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
18
use Maslosoft\Manganel\Meta\ManganelMeta;
19
20
/**
21
 * SearchClassNameDecorator
22
 *
23
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
24
 */
25
class SearchClassNameDecorator implements ModelDecoratorInterface
26
{
27
28
	/**
29
	 * This will be called when getting value.
30
	 * This should return end user value.
31
	 * @param AnnotatedInterface $model Document model which will be decorated
32
	 * @param mixed $dbValues
33
	 * @param string $transformatorClass Transformator class used
34
	 * @return bool Return true if value should be assigned to model
35
	 */
36 24
	public function read($model, &$dbValues, $transformatorClass = TransformatorInterface::class)
37
	{
38 24
		return true;
39
	}
40
41
	/**
42
	 * This will be called when setting value.
43
	 * This should return db acceptable value
44
	 * @param AnnotatedInterface $model Model which is about to be decorated
45
	 * @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.
46
	 * @param string $transformatorClass Transformator class used
47
	 * @return bool Return true to store value to database
48
	 */
49 53
	public function write($model, &$dbValues, $transformatorClass = TransformatorInterface::class)
50
	{
51
		// Do not store class names for homogenous collections
52
		/**
53
		 * TODO Below code conflicts with some features
54
		 */
55
//		if ($transformatorClass === RawArray::class)
56
//		{
57
//			$isHomogenous = ManganMeta::create($model)->type()->homogenous;
58
//			if ($isHomogenous)
59
//			{
60
//				return;
61
//			}
62
//		}
63 53
		$enforcedType = ManganelMeta::create($model)->type()->type;
64 53
		if (!empty($enforcedType))
65
		{
66 1
			$dbValues['_class'] = $enforcedType;
67
		}
68
		else
69
		{
70 52
			$dbValues['_class'] = get_class($model);
71
		}
72 53
		return true;
73
	}
74
75
}
76