OwneredTrait::getOwner()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Traits;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Interfaces\OwneredInterface;
18
19
/**
20
 * This trait provides basic implemention on ownering of documents. This allows
21
 * sub-document to get it's parent or root document.
22
 *
23
 * When using this trait, it is recommended that all classes in composition to
24
 * implement `OwneredInterface` or use this trait.
25
 *
26
 * **NOTE:** Currently it's implementation does **not work instantly**. Owner is
27
 * set when transforming objects, which include save/load.
28
 *
29
 * @see OwneredInterface
30
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
31
 */
32
trait OwneredTrait
33
{
34
35
	/**
36
	 * Owner reference or null if it's root object
37
	 * @var AnnotatedInterface|null
38
	 */
39
	private $_owner = null;
40
41
	/**
42
	 * Get document owner.
43
	 *
44
	 * This might return `null` if there is no owner.
45
	 *
46
	 * @return AnnotatedInterface Owner
47
	 * @Ignored
48
	 */
49
	public function getOwner()
50
	{
51
		return $this->_owner;
52
	}
53
54
	/**
55
	 * Get document root. This will traverse through objects
56
	 * hierarchy, untill owner-less document is found.
57
	 *
58
	 * This might return same object instance if there is no owner.
59
	 * 
60
	 * The return value is *guaranteed* to be document instance.
61
	 *
62
	 *
63
	 *
64
	 * @return AnnotatedInterface Root document
65
	 * @Ignored
66
	 */
67 6
	public function getRoot()
68
	{
69 6
		if ($this->_owner instanceof OwneredInterface && $this->_owner !== null)
70
		{
71 6
			return $this->_owner->getRoot();
72
		}
73
		else
74
		{
75 6
			return $this;
76
		}
77
	}
78
79
	/**
80
	 * Set class owner
81
	 *
82
	 * @param AnnotatedInterface|null $owner
83
	 * @Ignored
84
	 */
85 13
	public function setOwner(AnnotatedInterface $owner = null)
86
	{
87 13
		$this->_owner = $owner;
88 13
	}
89
90
}
91