I18NDecorator::read()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 28.2113

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 8
cts 21
cp 0.381
rs 7.6764
c 0
b 0
f 0
cc 9
nc 9
nop 4
crap 28.2113
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\Decorators\Property;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Exceptions\ManganException;
18
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
19
use Maslosoft\Mangan\Interfaces\InternationalInterface;
20
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
21
use Maslosoft\Mangan\Meta\ManganMeta;
22
23
/**
24
 * This creates i18n fields
25
 *
26
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
27
 */
28
class I18NDecorator implements DecoratorInterface
29
{
30
31
	/**
32
	 * This will be called when getting value.
33
	 * This should return end user value.
34
	 * @param AnnotatedInterface $model Document model which will be decorated
35
	 * @param string $name Field name
36
	 * @param mixed $dbValue
37
	 * @return bool Return true if value should be assigned to model
38
	 */
39 14
	public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
40
	{
41 14
		if (!$model instanceof InternationalInterface)
42
		{
43
			throw new ManganException(sprintf('Model class %s must implement interface %s to support I18N fields. You can use trait I18NAbleTrait as default implementation.', get_class($model), InternationalInterface::class));
44
		}
45 14
		$lang = $model->getLang();
46
47 14
		if (!is_array($dbValue))
48
		{
49
			$value = $dbValue;
50
			$dbValue = [];
51
			$dbValue[$lang] = $value;
52
		}
53
54 14
		$model->setRawI18N(array_merge($model->getRawI18N(), [$name => $dbValue]));
55 14
		if (array_key_exists($lang, $dbValue))
56
		{
57 14
			$model->$name = $dbValue[$lang];
58
		}
59
		else
60
		{
61
			$defaultLang = $model->getDefaultLanguage();
62
			$i18nMeta = ManganMeta::create($model)->field($name)->i18n;
63
			if ($i18nMeta->allowDefault && array_key_exists($defaultLang, $dbValue))
64
			{
65
				$model->$name = $dbValue[$defaultLang];
66
				return true;
67
			}
68
			if ($i18nMeta->allowAny)
69
			{
70
				foreach ($dbValue as $value)
71
				{
72
					if ($value)
73
					{
74
						$model->$name = $value;
75
					}
76
				}
77
			}
78
		}
79
80 14
		return true;
81
	}
82
83
	/**
84
	 * This will be called when setting value.
85
	 * This should return db acceptable value
86
	 * @param AnnotatedInterface $model Document model which will be decorated
87
	 * @param string $name Field name
88
	 * @param mixed $dbValue
89
	 * @return bool Return true to store value to database
90
	 */
91 29
	public function write($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
92
	{
93 29
		if (!$model instanceof InternationalInterface)
94
		{
95
			throw new ManganException(sprintf('Model class %s must implement interface %s to support I18N fields. You can use trait I18NAbleTrait as default implementation.', get_class($model), InternationalInterface::class));
96
		}
97 29
		$languages = $model->getLanguages();
98 29
		foreach ($model->getRawI18N() as $field => $value)
99
		{
100
			// Skip non-18n field
101 29
			if ($field !== $name)
102
			{
103 26
				continue;
104
			}
105 29
			foreach ($value as $code => $string)
106
			{
107
				// Don't set values not available in all languages
108 29
				if(in_array($code, $languages))
109
				{
110 29
					$dbValue[$name][$code] = $string;
111
				}
112
			}
113
		}
114 29
		$dbValue[$name][$model->getLang()] = $model->$name;
115 29
		return true;
116
	}
117
118
}
119