1: <?php
2: /**
3: * PHP version 5
4: * @package generalDriver
5: * @author Christian Schiffler <c.schiffler@cyberspectrum.de>
6: * @author Stefan Heimes <stefan_heimes@hotmail.com>
7: * @author Tristan Lins <tristan.lins@bit3.de>
8: * @copyright The MetaModels team.
9: * @license LGPL.
10: * @filesource
11: */
12:
13: namespace DcGeneral\DataDefinition;
14:
15: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
16:
17: /**
18: * An abstract condition chain.
19: *
20: * @package DcGeneral\DataDefinition
21: */
22: abstract class AbstractConditionChain implements ConditionChainInterface
23: {
24: /**
25: * The list of conditions.
26: *
27: * @var ConditionInterface[]
28: */
29: protected $conditions = array();
30:
31: /**
32: * The conjunction mode.
33: *
34: * @var string
35: */
36: protected $conjunction = self::AND_CONJUNCTION;
37:
38: /**
39: * Create a new condition chain.
40: *
41: * @param array $conditions The conditions to initialize the chain with (optional).
42: *
43: * @param string $conjunction The conjunction this chain contains (defaults to AND).
44: */
45: public function __construct(array $conditions = array(), $conjunction = self::AND_CONJUNCTION)
46: {
47: $this->addConditions($conditions);
48: $this->setConjunction($conjunction);
49: }
50:
51: /**
52: * {@inheritdoc}
53: */
54: public function clearConditions()
55: {
56: $this->conditions = array();
57: return $this;
58: }
59:
60: /**
61: * {@inheritdoc}
62: */
63: public function setConditions(array $conditions)
64: {
65: $this->clearConditions();
66: $this->addConditions($conditions);
67: return $this;
68: }
69:
70: /**
71: * {@inheritdoc}
72: */
73: public function addConditions(array $conditions)
74: {
75: foreach ($conditions as $condition)
76: {
77: $this->addCondition($condition);
78: }
79: return $this;
80: }
81:
82: /**
83: * {@inheritdoc}
84: */
85: public function addCondition(ConditionInterface $condition)
86: {
87: $hash = spl_object_hash($condition);
88:
89: $this->conditions[$hash] = $condition;
90: return $this;
91: }
92:
93: /**
94: * {@inheritdoc}
95: */
96: public function removeCondition(ConditionInterface $condition)
97: {
98: $hash = spl_object_hash($condition);
99: unset($this->conditions[$hash]);
100: return $this;
101: }
102:
103: /**
104: * {@inheritdoc}
105: */
106: public function getConditions()
107: {
108: return array_values($this->conditions);
109: }
110:
111: /**
112: * {@inheritdoc}
113: *
114: * @throws DcGeneralInvalidArgumentException When the conjunction is neither AND nor OR.
115: */
116: public function setConjunction($conjunction)
117: {
118: if ($conjunction != static::AND_CONJUNCTION && $conjunction != static::OR_CONJUNCTION)
119: {
120: throw new DcGeneralInvalidArgumentException(
121: 'Conjunction must be ConditionChainInterface::AND_CONJUNCTION or ConditionChainInterface::OR_CONJUNCTION'
122: );
123: }
124:
125: $this->conjunction = (string)$conjunction;
126:
127: return $this;
128: }
129:
130: /**
131: * {@inheritdoc}
132: */
133: public function getConjunction()
134: {
135: return $this->conjunction;
136: }
137:
138: /**
139: * {@inheritdoc}
140: */
141: public function __clone()
142: {
143: $conditions = array();
144: foreach ($this->conditions as $condition)
145: {
146: $bobaFett = clone $condition;
147:
148: $conditions[spl_object_hash($bobaFett)] = $bobaFett;
149: }
150: $this->conditions = $conditions;
151: }
152: }
153: