Completed
Push — master ( b7cc2d...e2c111 )
by Peter
14:18 queued 10:45
created

RelatedOrderingAnnotation::init()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.5924

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 12
cts 18
cp 0.6667
rs 8.7972
cc 4
eloc 12
nc 8
nop 0
crap 4.5924
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Annotations;
10
11
use Maslosoft\Addendum\Helpers\ParamsExpander;
12
use Maslosoft\Mangan\Interfaces\SortInterface;
13
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
14
use Maslosoft\Mangan\Meta\RelatedMeta;
15
16
/**
17
 * RelatedOrderingAnnotation
18
 *
19
 * Use this annotation to store order of related documents.
20
 * This should be used on same field as Related or RelatedArray annotations are applied.
21
 * This will apply incrementing values on selected field.
22
 *
23
 * Will also apply sort if not set.
24
 *
25
 * Compact notation:
26
 *
27
 * RelatedOrdering('order')
28
 *
29
 * Extended notation:
30
 *
31
 * RelatedOrdering(orderField = 'order')
32
 *
33
 * @template RelatedOrdering('${orderField}')
34
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
35
 */
36
class RelatedOrderingAnnotation extends ManganPropertyAnnotation
37
{
38
39
	public $value;
40
	public $orderField;
41
42 1
	public function init()
43
	{
44 1
		$data = ParamsExpander::expand($this, ['orderField']);
45 1
		if (empty($this->_entity->related))
46 1
		{
47
			$relMeta = new RelatedMeta();
48
		}
49
		else
50
		{
51 1
			$relMeta = $this->_entity->related;
52
		}
53 1
		foreach ($data as $key => $val)
54
		{
55 1
			$relMeta->$key = $val;
56 1
		}
57 1
		if (empty($relMeta->sort))
58 1
		{
59
			$relMeta->sort = [
1 ignored issue
show
Documentation Bug introduced by
It seems like array($relMeta->orderFie...SortInterface::SortAsc) of type array<string,?> is incompatible with the declared type array<integer,integer> of property $sort.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
				$relMeta->orderField => SortInterface::SortAsc
61
			];
62
		}
63 1
		$this->_entity->related = $relMeta;
64 1
	}
65
66
}
67