Completed
Push — master ( c8f19a...ddbfd2 )
by Peter
72:09 queued 69:12
created

DecoratableTrait::decorateWith()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2.0932
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
22
/**
23
 * DecoratableTrait
24
 * @see DecoratableInterface
25
 * @see CriteriaInterface
26
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
27
 */
28
trait DecoratableTrait
29
{
30
31
	/**
32
	 * Condition decorator instance
33
	 * @var ConditionDecoratorInterface
34
	 */
35
	private $cd = null;
36
37
	/**
38
	 * Get condition interface
39
	 * @return ConditionDecoratorInterface
40
	 */
41 98
	public function getCd()
42
	{
43 98
		return $this->cd;
44
	}
45
46
	/**
47
	 * Set condition decorator interface
48
	 * @param ConditionDecoratorInterface $cd
49
	 * @return CriteriaInterface
50
	 */
51 107
	public function setCd(ConditionDecoratorInterface $cd)
52
	{
53 107
		$this->cd = $cd;
54 107
		return $this;
55
	}
56
57
	/**
58
	 * Decorate and sanitize criteria with provided model.
59
	 * @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.
60
	 * @param ConditionDecoratorInterface $decorator
61
	 * @return CriteriaInterface
62
	 */
63 104
	public function decorateWith($model, ConditionDecoratorInterface $decorator = null)
64
	{
65 104
		if (null !== $decorator)
66 104
		{
67
			$this->cd = $decorator;
68
		}
69
		else
70
		{
71 104
			$this->cd = new ConditionDecorator($model);
72
		}
73 104
		return $this;
74
	}
75
76
}
77