PrimaryKeyAnnotation::init()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 9
cts 9
cp 1
rs 9.488
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
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\Mangan\Meta\DocumentTypeMeta;
17
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
18
19
/**
20
 * To define simple primary key mark field with `PrimaryKey` annotation.
21
 * To define composite primary key, use this annotation on several fields.
22
 * 
23
 * @Target('property')
24
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
25
 */
26
class PrimaryKeyAnnotation extends ManganPropertyAnnotation
27
{
28
29 4
	public function init()
30
	{
31
		// Set primaryKey value on type
32 4
		$type = $this->getMeta()->type();
33
		/* @var $type DocumentTypeMeta */
34 4
		$name = $this->getEntity()->name;
35
		// If key is already defined it means that it is composite
36 4
		if ($type->primaryKey)
37
		{
38
			// If it is array then add field
39 3
			if (is_array($type->primaryKey))
40
			{
41 1
				$type->primaryKey[] = $name;
42
			}
43
			else
44
			{
45
				// it is not array so create composite from existing
46
				// field and add new field to definition
47 3
				$type->primaryKey = [$type->primaryKey, $name];
48
			}
49
		}
50
		else
51
		{
52
			// Simple primary key
53 4
			$type->primaryKey = $name;
54
		}
55 4
	}
56
57
}
58