Completed
Push — master ( c8f19a...ddbfd2 )
by Peter
72:09 queued 69:12
created

RelatedOrderingAnnotation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 31
ccs 12
cts 18
cp 0.6667
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B init() 0 23 4
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 1
		{
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 1
		}
68 1
		if (empty($relMeta->sort))
69 1
		{
70
			$relMeta->sort = [
71
				$relMeta->orderField => SortInterface::SortAsc
72
			];
73
		}
74 1
		$this->getEntity()->related = $relMeta;
75 1
	}
76
77
}
78