|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (C) 2022 synetics GmbH |
| 5 | + * Copyright (C) 2016-2022 Benjamin Heisig |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + * @copyright Copyright (C) 2022 synetics GmbH |
| 21 | + * @copyright Copyright (C) 2016-2022 Benjamin Heisig |
| 22 | + * @license http://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License (AGPL) |
| 23 | + * @link https://github.com/i-doit/api-client-php |
| 24 | + */ |
| 25 | + |
| 26 | +declare(strict_types=1); |
| 27 | + |
| 28 | +namespace Idoit\APIClient; |
| 29 | + |
| 30 | +use \Exception; |
| 31 | + |
| 32 | +/** |
| 33 | + * Conditional helper for more readable code |
| 34 | + */ |
| 35 | +class Condition |
| 36 | +{ |
| 37 | + |
| 38 | + /** |
| 39 | + * Operator: AND |
| 40 | + */ |
| 41 | + const AND = 'AND'; |
| 42 | + |
| 43 | + /** |
| 44 | + * Operator: URL |
| 45 | + */ |
| 46 | + const OR = 'OR'; |
| 47 | + |
| 48 | + public string $property; |
| 49 | + |
| 50 | + public string $comparison; |
| 51 | + |
| 52 | + public string $value; |
| 53 | + |
| 54 | + public string $operator; |
| 55 | + |
| 56 | + public function where(string $const, string $property):self |
| 57 | + { |
| 58 | + $this->property = $const . "-" . $property; |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + public function andWhere(string $const, string $property):self |
| 63 | + { |
| 64 | + $this->operator = self::AND; |
| 65 | + $this->where($const, $property); |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + public function orWhere(string $const, string $property):self |
| 70 | + { |
| 71 | + $this->operator = self::OR; |
| 72 | + $this->where($const, $property); |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + public function isLike(string $value):self |
| 77 | + { |
| 78 | + $this->comparison = 'like'; |
| 79 | + $this->value = $value; |
| 80 | + return $this; |
| 81 | + } |
| 82 | + |
| 83 | + public function isNotLike(string $value):self |
| 84 | + { |
| 85 | + $this->comparison = 'not like'; |
| 86 | + $this->value = $value; |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + public function isEqualTo(string $value):self |
| 91 | + { |
| 92 | + $this->comparison = '='; |
| 93 | + $this->value = $value; |
| 94 | + return $this; |
| 95 | + } |
| 96 | + |
| 97 | + public function isNotEqualTo(string $value):self |
| 98 | + { |
| 99 | + $this->comparison = '!='; |
| 100 | + $this->value = $value; |
| 101 | + return $this; |
| 102 | + } |
| 103 | + |
| 104 | + public function isGreaterThan(string $value):self |
| 105 | + { |
| 106 | + $this->comparison = '>'; |
| 107 | + $this->value = $value; |
| 108 | + return $this; |
| 109 | + } |
| 110 | + |
| 111 | + public function isGreaterOrEqaulThan(string $value):self |
| 112 | + { |
| 113 | + $this->comparison = '>='; |
| 114 | + $this->value = $value; |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + public function isLowerThan(string $value):self |
| 119 | + { |
| 120 | + $this->comparison = '<'; |
| 121 | + $this->value = $value; |
| 122 | + return $this; |
| 123 | + } |
| 124 | + |
| 125 | + public function isLowerOrEaqualThan(string $value):self |
| 126 | + { |
| 127 | + $this->comparison = '<='; |
| 128 | + $this->value = $value; |
| 129 | + return $this; |
| 130 | + } |
| 131 | + |
| 132 | + public function isLowerOrGreaterThan(string $value):self |
| 133 | + { |
| 134 | + $this->comparison = '<>'; |
| 135 | + $this->value = $value; |
| 136 | + return $this; |
| 137 | + } |
| 138 | + |
| 139 | + public function __construct($const = null, $property = null, $comparison = null, $value = null, $operator = null) |
| 140 | + { |
| 141 | + |
| 142 | + if (!is_null($const) && !is_null($property)) { |
| 143 | + $this->property = $const . "-" . $property; |
| 144 | + } |
| 145 | + |
| 146 | + $allowedComparison = ['=', '!=', 'like', 'not like', '>', '>=', '<', '<=', '<>']; |
| 147 | + if (!is_null($comparison) && !is_null($value) && in_array($comparison, $allowedComparison)) { |
| 148 | + $this->comparison = $comparison; |
| 149 | + $this->value = $value; |
| 150 | + } |
| 151 | + |
| 152 | + $allowedOperators = [self::AND, self::OR]; |
| 153 | + if (!is_null($operator) && in_array(strtoupper($operator), $allowedOperators)) { |
| 154 | + $this->operator = strtoupper($operator); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public function toArray(): array |
| 159 | + { |
| 160 | + |
| 161 | + $condition = [ |
| 162 | + 'property' => $this->property, |
| 163 | + 'comparison' => $this->comparison, |
| 164 | + 'value' => $this->value |
| 165 | + ]; |
| 166 | + |
| 167 | + if (isset($this->operator)) { |
| 168 | + $condition['operator'] = $this->operator; |
| 169 | + } |
| 170 | + |
| 171 | + return $condition; |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments