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\Palette\Condition\Palette;
14:
15: use DcGeneral\Data\ModelInterface;
16: use DcGeneral\Data\PropertyValueBag;
17:
18: /**
19: * Condition checking that the value of a property is the same as a passed value.
20: */
21: class PropertyValueCondition extends AbstractWeightAwarePaletteCondition
22: {
23: /**
24: * The property name.
25: *
26: * @var string
27: */
28: protected $propertyName;
29:
30: /**
31: * The expected property value.
32: *
33: * @var mixed
34: */
35: protected $propertyValue;
36:
37: /**
38: * Use strict compare mode.
39: *
40: * @var bool
41: */
42: protected $strict;
43:
44: /**
45: * Create a new instance.
46: *
47: * @param string $propertyName The name of the property.
48: *
49: * @param mixed $propertyValue The value of the property to match.
50: *
51: * @param bool $strict Flag if the comparison shall be strict (type safe).
52: *
53: * @param int $weight The weight of this condition to apply.
54: */
55: public function __construct($propertyName = '', $propertyValue = null, $strict = false, $weight = 1)
56: {
57: $this->propertyName = (string)$propertyName;
58: $this->propertyValue = $propertyValue;
59: $this->strict = (bool)$strict;
60: $this->setWeight($weight);
61: }
62:
63: /**
64: * Set the property name.
65: *
66: * @param string $propertyName The property name.
67: *
68: * @return PropertyValueCondition
69: */
70: public function setPropertyName($propertyName)
71: {
72: $this->propertyName = (string)$propertyName;
73: return $this;
74: }
75:
76: /**
77: * Retrieve the property name.
78: *
79: * @return string
80: */
81: public function getPropertyName()
82: {
83: return $this->propertyName;
84: }
85:
86: /**
87: * Set the property value to match.
88: *
89: * @param mixed $propertyValue The value.
90: *
91: * @return PropertyValueCondition
92: */
93: public function setPropertyValue($propertyValue)
94: {
95: $this->propertyValue = $propertyValue;
96: return $this;
97: }
98:
99: /**
100: * Retrieve the property value to match.
101: *
102: * @return mixed
103: */
104: public function getPropertyValue()
105: {
106: return $this->propertyValue;
107: }
108:
109: /**
110: * Set the flag if the comparison shall be strict (type safe).
111: *
112: * @param boolean $strict The flag.
113: *
114: * @return PropertyValueCondition
115: */
116: public function setStrict($strict)
117: {
118: $this->strict = (bool)$strict;
119: return $this;
120: }
121:
122: /**
123: * Retrieve the flag if the comparison shall be strict (type safe).
124: *
125: * @return boolean
126: */
127: public function getStrict()
128: {
129: return $this->strict;
130: }
131:
132: /**
133: * {@inheritdoc}
134: */
135: public function getMatchCount(ModelInterface $model = null, PropertyValueBag $input = null)
136: {
137: if (!$this->propertyName)
138: {
139: return false;
140: }
141:
142: if ($input && $input->hasPropertyValue($this->propertyName))
143: {
144: $value = $input->getPropertyValue($this->propertyName);
145: }
146: elseif ($model)
147: {
148: $value = $model->getProperty($this->propertyName);
149: }
150: else
151: {
152: return false;
153: }
154:
155: return ($this->strict ? ($value === $this->propertyValue) : ($value == $this->propertyValue))
156: ? $this->getWeight()
157: : false;
158: }
159:
160: /**
161: * {@inheritdoc}
162: */
163: public function __clone()
164: {
165: }
166: }
167: