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 true.
20: */
21: class PropertyTrueCondition extends AbstractWeightAwarePaletteCondition
22: {
23: /**
24: * The property name.
25: *
26: * @var string
27: */
28: protected $propertyName;
29:
30: /**
31: * Use strict compare mode.
32: *
33: * @var bool
34: */
35: protected $strict;
36:
37: /**
38: * Create a new instance.
39: *
40: * @param string $propertyName The name of the property.
41: *
42: * @param bool $strict Flag if the comparison shall be strict (type safe).
43: *
44: * @param int $weight The weight of this condition to apply.
45: */
46: public function __construct($propertyName = '', $strict = false, $weight = 1)
47: {
48: $this->propertyName = (string)$propertyName;
49: $this->strict = (bool)$strict;
50: $this->setWeight($weight);
51: }
52:
53: /**
54: * Set the property name.
55: *
56: * @param string $propertyName The property name.
57: *
58: * @return PropertyTrueCondition
59: */
60: public function setPropertyName($propertyName)
61: {
62: $this->propertyName = (string)$propertyName;
63:
64: return $this;
65: }
66:
67: /**
68: * Retrieve the property name.
69: *
70: * @return string
71: */
72: public function getPropertyName()
73: {
74: return $this->propertyName;
75: }
76:
77: /**
78: * Set the flag if the comparison shall be strict (type safe).
79: *
80: * @param boolean $strict The flag.
81: *
82: * @return PropertyTrueCondition
83: */
84: public function setStrict($strict)
85: {
86: $this->strict = (bool)$strict;
87:
88: return $this;
89: }
90:
91: /**
92: * Retrieve the flag if the comparison shall be strict (type safe).
93: *
94: * @return boolean
95: */
96: public function getStrict()
97: {
98: return $this->strict;
99: }
100:
101: /**
102: * {@inheritdoc}
103: */
104: public function getMatchCount(ModelInterface $model = null, PropertyValueBag $input = null)
105: {
106: if (!$this->propertyName)
107: {
108: return false;
109: }
110:
111: if ($input && $input->hasPropertyValue($this->propertyName))
112: {
113: $value = $input->getPropertyValue($this->propertyName);
114: }
115: elseif ($model)
116: {
117: $value = $model->getProperty($this->propertyName);
118: }
119: else
120: {
121: return false;
122: }
123:
124: return ($this->strict ? ($value === true) : $value) ? $this->getWeight() : false;
125: }
126:
127: /**
128: * {@inheritdoc}
129: */
130: public function __clone()
131: {
132: }
133: }
134: