Completed
Push — master ( 0a999d...b4b35b )
by Peter
13:24 queued 02:45
created

I18NAnnotation::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 0
crap 3.009
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\Annotations;
15
16
use InvalidArgumentException;
17
use Maslosoft\Mangan\Decorators\Property\I18NDecorator;
18
use Maslosoft\Mangan\Meta\I18NMeta;
19
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
20
21
/**
22
 * This annotation indicates internationallized fields
23
 * @template I18N
24
 * @Target('property')
25
 * @author Piotr
26
 */
27
class I18NAnnotation extends ManganPropertyAnnotation
28
{
29
30
	public $value = true;
31
	public $allowDefault = false;
32
	public $allowAny = false;
33
34 7
	public function init()
35
	{
36 7
		if ($this->allowDefault && $this->allowAny)
37
		{
38
			throw new InvalidArgumentException(sprintf('Arguments "allowDefault" and "allowAny" for element "%s" in class "%s" cannot be both set true', $this->name, $this->getMeta()->type()->name));
39
		}
40 7
		$i18n = new I18NMeta();
41 7
		$i18n->enabled = (bool) $this->value;
42 7
		$i18n->allowDefault = $this->allowDefault;
43 7
		$i18n->allowAny = $this->allowAny;
44 7
		$this->getEntity()->i18n = $i18n;
45
46 7
		$this->getEntity()->decorators[] = I18NDecorator::class;
47 7
	}
48
49
}
50