Completed
Push — master ( 19dca0...fe7b81 )
by Peter
03:30
created

IndexAnnotation::init()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 44
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9.0468

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 11
cts 12
cp 0.9167
rs 4.909
c 0
b 0
f 0
cc 9
eloc 21
nc 24
nop 0
crap 9.0468
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\Indexes;
15
16
use function is_array;
17
use function is_string;
18
use Maslosoft\Addendum\Helpers\ParamsExpander;
19
use Maslosoft\Mangan\Meta\IndexMeta;
20
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
21
use Maslosoft\Mangan\Sort;
22
23
/**
24
 * IndexAnnotation
25
 *
26
 * @Target('property')
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
class IndexAnnotation extends ManganPropertyAnnotation
30
{
31
32
	const Ns = __NAMESPACE__;
33
34
	public $value;
35
36
	/**
37
	 * This can be either:
38
	 *
39
	 * * Empty - for simple ascending index
40
	 * * `Sort::SortAsc` - for simple ascending index
41
	 * * `Sort::SortDesc` - for simple descending index
42
	 * * `array` - for any other keys specification
43
	 *
44
	 * @var mixed
45
	 */
46 4
	public $keys;
47
	public $options;
48 4
49
	public function init()
50 4
	{
51 4
		if(isset($this->value['username']))
52 4
		{
53 4
			echo '';
54
		}
55 1
		$data = (object)ParamsExpander::expand($this, ['keys', 'options']);
56
57
		// Seems short notation for keys only
58
		if(empty($data->keys) && is_array($this->value))
59 3
		{
60
			foreach($this->value as $key => $sort)
61 3
			{
62
				if(is_string($key))
63
				{
64
					$data->keys[$key] = $sort;
65
				}
66
			}
67
		}
68 4
69
		$entity = $this->getEntity();
70 4
		$name = $entity->name;
71
		$keys = [];
72 4
		if(empty($data->keys))
73 4
		{
74
			$keys[$name] = Sort::SortAsc;
75
		}
76
		else
77
		{
78
			if(!is_array($data->keys))
79
			{
80
				$keys[$name] = $data->keys;
81
			}
82
			else
83
			{
84
				$keys = $data->keys;
85
			}
86
		}
87
		if(empty($data->options))
88
		{
89
			$data->options = [];
90
		}
91
		$entity->index[] = new IndexMeta($keys, $data->options);
92
	}
93
94
}
95