Skip to content

Commit 47d5979

Browse files
rakoitdeKornbergerKornberger, Ralf
authored
Add Condition helper class for more readable code (#30)
* add Condition.php * Update Condition.php * add condition helper class * Fixed typo in README.md * remove author * added some tests * remove author and add type declarations * make phpcs happy * make phpcs happy with type declarations the characters in this lines exceeds 120. so i have to remove type declarations. * remove whitespace * correct use of method --------- Co-authored-by: Kornberger <k98kornr@kkh-services.local> Co-authored-by: Kornberger, Ralf <Ralf.Kornberger@kk-service.de>
1 parent 94075ed commit 47d5979

3 files changed

Lines changed: 556 additions & 0 deletions

File tree

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,60 @@ $cmdbObjects
477477
->purge([1, 2, 3]);
478478
~~~
479479

480+
#### Read object by condition
481+
482+
Allowed comparison are '=', '!=', 'like', 'not like', '>', '>=', '<', '<=', '<>'.
483+
484+
~~~ {.php}
485+
use Idoit\APIClient\API;
486+
use Idoit\APIClient\CMDBCondition;
487+
488+
$api = new API([/* … */]);
489+
$condition = new CMDBCondition($api);
490+
$result = $condition->read(
491+
[
492+
[
493+
'property' => "C__CATG__ACCOUNTING-order_no",
494+
'comparison' => "=",
495+
'value' => "ORDER4711",
496+
]
497+
]
498+
);
499+
~~~
500+
501+
You can use more than one condition and add an operator to them.
502+
Allowed operators are 'AND' and 'OR'.
503+
504+
~~~ {.php}
505+
$result = $condition->read(
506+
[
507+
[
508+
'property' => "C__CATG__ACCOUNTING-order_no",
509+
'comparison' => "=",
510+
'value' => "ORDER4711",
511+
],
512+
[
513+
'property' => "C__CATG__ACCOUNTING-order_no",
514+
'comparison' => "=",
515+
'value' => "ORDER0815",
516+
'operator' => 'OR',
517+
]
518+
]
519+
);
520+
~~~
521+
522+
For more readable code you can use the condition helper class.
523+
524+
~~~ {.php}
525+
use Idoit\APIClient\Condition;
526+
527+
$conditions = [
528+
new Condition("C__CATG__ACCOUNTING", "order_no", "=", "ORDER4711"),
529+
new Condition("C__CATG__ACCOUNTING", "order_no", "=", "ORDER4711", Condition::OR),
530+
];
531+
$result = $condition->read($conditions);
532+
~~~
533+
480534
#### Create category entries with attributes
481535

482536
~~~ {.php}

src/Condition.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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

Comments
 (0)