Completed
Push — master ( d44dfe...95ea76 )
by Peter
08:19
created

IndexAnnotation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 66
ccs 20
cts 20
cp 1
rs 10

1 Method

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