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\ModelRelationship;
14:
15: use DcGeneral\Data\ModelInterface;
16: use DcGeneral\Exception\DcGeneralRuntimeException;
17:
18: /**
19: * This class is an abstract base for defining model relationship conditions.
20: *
21: * It implements a basic condition check.
22: *
23: * @package DcGeneral\DataDefinition\ModelRelationship
24: */
25: abstract class AbstractCondition
26: {
27: /**
28: * Check if an AND condition filter matches.
29: *
30: * @param ModelInterface $model The model to check the condition against.
31: *
32: * @param array $filter The filter rules to be applied.
33: *
34: * @return bool
35: */
36: protected static function checkAndFilter($model, $filter)
37: {
38: foreach ($filter['children'] as $child)
39: {
40: // AND => first false means false.
41: if (!self::checkCondition($model, $child))
42: {
43: return false;
44: }
45: }
46: return true;
47: }
48:
49: /**
50: * Check if an AND condition filter matches.
51: *
52: * @param ModelInterface $model The model to check the condition against.
53: *
54: * @param array $filter The filter rules to be applied.
55: *
56: * @return bool
57: */
58: protected static function checkOrFilter($model, $filter)
59: {
60: foreach ($filter['children'] as $child)
61: {
62: // OR => first true means true.
63: if (self::checkCondition($model, $child))
64: {
65: return true;
66: }
67: }
68: return false;
69: }
70:
71: /**
72: * Check if the passed filter rules apply to the given model.
73: *
74: * @param ModelInterface $objParentModel The model to check the condition against.
75: *
76: * @param array $arrFilter The condition filter to be applied.
77: *
78: * @return bool
79: *
80: * @throws DcGeneralRuntimeException When an unknown filter operation is encountered.
81: */
82: public static function checkCondition(ModelInterface $objParentModel, $arrFilter)
83: {
84: switch ($arrFilter['operation'])
85: {
86: case 'AND':
87: case 'OR':
88: // FIXME: backwards compat - remove when done.
89: if (is_array($arrFilter['childs']))
90: {
91: trigger_error('Filter array uses deprecated entry "childs", please use "children" instead.', E_USER_DEPRECATED);
92: $arrFilter['children'] = $arrFilter['childs'];
93: }
94:
95: if ($arrFilter['operation'] == 'AND')
96: {
97: return self::checkAndFilter($objParentModel, $arrFilter['children']);
98: }
99: else
100: {
101: return self::checkOrFilter($objParentModel, $arrFilter['children']);
102: }
103: break;
104:
105: case '=':
106: return ($objParentModel->getProperty($arrFilter['property']) == $arrFilter['value']);
107:
108: case '>':
109: return ($objParentModel->getProperty($arrFilter['property']) > $arrFilter['value']);
110:
111: case '<':
112: return ($objParentModel->getProperty($arrFilter['property']) < $arrFilter['value']);
113:
114: case 'IN':
115: return in_array($objParentModel->getProperty($arrFilter['property']), $arrFilter['value']);
116:
117: default:
118: }
119:
120: throw new DcGeneralRuntimeException(
121: 'Error processing filter array - unknown operation ' . var_export($arrFilter, true),
122: 1
123: );
124: }
125: }
126: