Criteria.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\persistence;
12 
14 
15 /**
16  * Criteria defines a condition on a PersistentObject's attribute
17  * used to select specific instances.
18  *
19  * @author ingo herwig <ingo@wemove.com>
20  */
21 class Criteria {
22 
23  const OPERATOR_AND = 'AND'; // the and operator
24  const OPERATOR_OR = 'OR'; // the or operator
25 
26  protected $type = null;
27  protected $attribute = null;
28  protected $operator = null;
29  protected $value = null;
31 
32  /**
33  * Constructor.
34  * @param $type The PersistentObject type that has the attribute
35  * @param $attribute The name of the attribute
36  * @param $operator The comparison operator used to compare the given value with
37  * the attribute's value
38  * @param $value The value to compare the object with
39  * @param $combineOperator The Criteria::OPERATOR to use, when this criteria is
40  * combined with other criteria (optional, default: _Criteria::OPERATOR_AND_)
41  */
43  $this->type = $type;
44  $this->attribute = $attribute;
45  $this->operator = $operator;
46  $this->value = $value;
47  $this->combineOperator = $combineOperator;
48  }
49 
50  /**
51  * Factory method for constructing a Criteria that may be used as value on
52  * a PersistentObject's attribute (no type, attribute parameter needed)
53  * @param $operator The comparison operator used to compare the given value with
54  * the attribute's value
55  * @param $value The value to compare the object with
56  * @return Criteria
57  */
58  public static function asValue($operator, $value) {
59  return new Criteria(null, null, $operator, $value);
60  }
61 
62  /**
63  * Set the PersistentObject type that has the attribute
64  * @param $type The type name
65  */
66  public function setType($type) {
67  $this->type = $type;
68  }
69 
70  /**
71  * Get the PersistentObject type that has the attribute
72  * @return String
73  */
74  public function getType() {
75  return $this->type;
76  }
77 
78  /**
79  * Set the name of the attribute
80  * @param $attribute The attribute name
81  */
82  public function setAttribute($attribute) {
83  $this->attribute = $attribute;
84  }
85 
86  /**
87  * Get the name of the attribute
88  * @return String
89  */
90  public function getAttribute() {
91  return $this->attribute;
92  }
93 
94  /**
95  * Set the comparison operator used to compare the given value with
96  * the attribute's value
97  * @param $operator The operator
98  */
99  public function setOperator($operator) {
100  $this->operator = $operator;
101  }
102 
103  /**
104  * Get the comparison operator used to compare the given value with
105  * the attribute's value
106  * @return String
107  */
108  public function getOperator() {
109  return $this->operator;
110  }
111 
112  /**
113  * Set the value to compare the object with
114  * @param $value The value
115  */
116  public function setValue($value) {
117  $this->value = $value;
118  }
119 
120  /**
121  * Get the value to compare the object with
122  * @return Mixed
123  */
124  public function getValue() {
125  return $this->value;
126  }
127 
128  /**
129  * Set the Criteria::OPERATOR to use, when this criteria is combined with other criteria
130  * @param $combineOperator One of the Criteria::OPERATOR constants
131  */
133  $this->combineOperator = $combineOperator;
134  }
135 
136  /**
137  * Get the Criteria::OPERATOR to use, when this criteria is combined with other criteria
138  * @return One of the Criteria::OPERATOR constants
139  */
140  public function getCombineOperator() {
141  return $this->combineOperator;
142  }
143 
144  /**
145  * Get an identifier for the instance
146  * @return String
147  */
148  public function getId() {
149  $str = "[".$this->combineOperator."] ".$this->type.".".$this->attribute.
150  " ".$this->operator.(is_array($this->value) ? sizeof($this->value) : "");
151  return $str;
152  }
153 
154  /**
155  * Get a string representation of the criteria
156  * @return String
157  */
158  public function __toString() {
159  return $this->getId()." ".print_r($this->value, true);
160  }
161 }
162 ?>
__toString()
Get a string representation of the criteria.
Definition: Criteria.php:158
getValue()
Get the value to compare the object with.
Definition: Criteria.php:124
Persistence layer related interfaces and classes.
Definition: namespaces.php:42
getAttribute()
Get the name of the attribute.
Definition: Criteria.php:90
static asValue($operator, $value)
Factory method for constructing a Criteria that may be used as value on a PersistentObject's attribut...
Definition: Criteria.php:58
Criteria defines a condition on a PersistentObject's attribute used to select specific instances.
Definition: Criteria.php:21
setCombineOperator($combineOperator)
Set the Criteria::OPERATOR to use, when this criteria is combined with other criteria.
Definition: Criteria.php:132
getType()
Get the PersistentObject type that has the attribute.
Definition: Criteria.php:74
getId()
Get an identifier for the instance.
Definition: Criteria.php:148
setValue($value)
Set the value to compare the object with.
Definition: Criteria.php:116
setOperator($operator)
Set the comparison operator used to compare the given value with the attribute's value.
Definition: Criteria.php:99
setAttribute($attribute)
Set the name of the attribute.
Definition: Criteria.php:82
setType($type)
Set the PersistentObject type that has the attribute.
Definition: Criteria.php:66
__construct($type, $attribute, $operator, $value, $combineOperator=Criteria::OPERATOR_AND)
Constructor.
Definition: Criteria.php:42
getOperator()
Get the comparison operator used to compare the given value with the attribute's value.
Definition: Criteria.php:108
getCombineOperator()
Get the Criteria::OPERATOR to use, when this criteria is combined with other criteria.
Definition: Criteria.php:140