Completed
Push — master ( 0310e6...e3b97d )
by Peter
24:20 queued 21:09
created

DecoratableTrait::setCd()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 9.6666
cc 3
eloc 5
nc 2
nop 1
crap 3.2098
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\Criteria;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Criteria\ConditionDecorator;
18
use Maslosoft\Mangan\Interfaces\ConditionDecoratorInterface;
19
use Maslosoft\Mangan\Interfaces\Criteria\DecoratableInterface;
20
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
21
use Maslosoft\Mangan\Interfaces\Decorators\ConditionDecoratorTypeAwareInterface;
22
use Maslosoft\Mangan\Interfaces\Decorators\ConditionDecoratorTypeInterface;
23
24
/**
25
 * DecoratableTrait
26
 * @see DecoratableInterface
27
 * @see CriteriaInterface
28
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
29
 */
30
trait DecoratableTrait
31
{
32
33
	/**
34
	 * Condition decorator instance
35
	 * @var ConditionDecoratorInterface
36
	 */
37
	private $cd = null;
38
39
	/**
40
	 * Get condition interface
41
	 * @return ConditionDecoratorInterface
42
	 */
43 98
	public function getCd()
44
	{
45 98
		if($this instanceof ConditionDecoratorTypeAwareInterface && $this->cd instanceof ConditionDecoratorTypeInterface)
46 98
		{
47
			$this->cd->setDecoratorType($this->getDecoratorType());
48
		}
49 98
		return $this->cd;
50
	}
51
52
	/**
53
	 * Set condition decorator interface
54
	 * @param ConditionDecoratorInterface $cd
55
	 * @return CriteriaInterface
56
	 */
57 107
	public function setCd(ConditionDecoratorInterface $cd)
58
	{
59 107
		if($this instanceof ConditionDecoratorTypeAwareInterface && $cd instanceof ConditionDecoratorTypeInterface)
60 107
		{
61
			$cd->setDecoratorType($this->getDecoratorType());
62
		}
63 107
		$this->cd = $cd;
64 107
		return $this;
65
	}
66
67
	/**
68
	 * Decorate and sanitize criteria with provided model.
69
	 * @param AnnotatedInterface $model Model to use for decorators and sanitizer when creating conditions. If null no decorators will be used. If model is provided it's sanitizers and decorators will be used.
70
	 * @param ConditionDecoratorInterface $decorator
71
	 * @return CriteriaInterface
72
	 */
73 104
	public function decorateWith($model, ConditionDecoratorInterface $decorator = null)
74
	{
75 104
		if (null !== $decorator)
76 104
		{
77
			$this->cd = $decorator;
78
		}
79
		else
80
		{
81 104
			$this->cd = new ConditionDecorator($model);
82
		}
83 104
		if($this instanceof ConditionDecoratorTypeAwareInterface && $this->cd instanceof ConditionDecoratorTypeInterface)
84 104
		{
85
			$this->cd->setDecoratorType($this->getDecoratorType());
86
		}
87 104
		return $this;
88
	}
89
90
}
91