Completed
Push — master ( 1470bc...ff889f )
by Peter
08:18
created

RelatedOrderingAnnotation::init()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 9
cts 12
cp 0.75
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 12
nc 8
nop 0
crap 4.25
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 Maslosoft\Addendum\Helpers\ParamsExpander;
17
use Maslosoft\Mangan\Interfaces\SortInterface;
18
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
19
use Maslosoft\Mangan\Meta\RelatedMeta;
20
21
/**
22
 * RelatedOrderingAnnotation
23
 *
24
 * Use this annotation to store order of related documents.
25
 * This should be used on same field as Related or RelatedArray annotations are applied.
26
 * This will apply incrementing values on selected field.
27
 *
28
 * Will also apply sort if not set.
29
 *
30
 * Compact notation:
31
 *
32
 * RelatedOrdering('order')
33
 *
34
 * Extended notation:
35
 *
36
 * RelatedOrdering(orderField = 'order')
37
 *
38
 *
39
 * @Conflicts('Embedded')
40
 * @Conflicts('EmbeddedArray')
41
 * @Conflicts('DbRef')
42
 * @Conflicts('DbRefArray')
43
 *
44
 * @template RelatedOrdering('${orderField}')
45
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
46
 */
47
class RelatedOrderingAnnotation extends ManganPropertyAnnotation
48
{
49
50
	public $value;
51
	public $orderField;
52
53 1
	public function init()
54
	{
55 1
		$data = ParamsExpander::expand($this, ['orderField']);
56 1
		if (empty($this->getEntity()->related))
57
		{
58
			$relMeta = new RelatedMeta();
59
		}
60
		else
61
		{
62 1
			$relMeta = $this->getEntity()->related;
63
		}
64 1
		foreach ($data as $key => $val)
65
		{
66 1
			$relMeta->$key = $val;
67
		}
68 1
		if (empty($relMeta->sort))
69
		{
70
			$relMeta->sort = [
71
				$relMeta->orderField => SortInterface::SortAsc
72
			];
73
		}
74 1
		$this->getEntity()->related = $relMeta;
75 1
	}
76
77
}
78