Skip to content

Commit

Permalink
Add Condition helper class for more readable code (#30)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Aug 14, 2023
1 parent 94075ed commit 47d5979
Show file tree
Hide file tree
Showing 3 changed files with 556 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,60 @@ $cmdbObjects
->purge([1, 2, 3]);
~~~

#### Read object by condition

Allowed comparison are '=', '!=', 'like', 'not like', '>', '>=', '<', '<=', '<>'.

~~~ {.php}
use Idoit\APIClient\API;
use Idoit\APIClient\CMDBCondition;
$api = new API([/* … */]);
$condition = new CMDBCondition($api);
$result = $condition->read(
[
[
'property' => "C__CATG__ACCOUNTING-order_no",
'comparison' => "=",
'value' => "ORDER4711",
]
]
);
~~~

You can use more than one condition and add an operator to them.
Allowed operators are 'AND' and 'OR'.

~~~ {.php}
$result = $condition->read(
[
[
'property' => "C__CATG__ACCOUNTING-order_no",
'comparison' => "=",
'value' => "ORDER4711",
],
[
'property' => "C__CATG__ACCOUNTING-order_no",
'comparison' => "=",
'value' => "ORDER0815",
'operator' => 'OR',
]
]
);
~~~

For more readable code you can use the condition helper class.

~~~ {.php}
use Idoit\APIClient\Condition;
$conditions = [
new Condition("C__CATG__ACCOUNTING", "order_no", "=", "ORDER4711"),
new Condition("C__CATG__ACCOUNTING", "order_no", "=", "ORDER4711", Condition::OR),
];
$result = $condition->read($conditions);
~~~

#### Create category entries with attributes

~~~ {.php}
Expand Down
174 changes: 174 additions & 0 deletions src/Condition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

/**
* Copyright (C) 2022 synetics GmbH
* Copyright (C) 2016-2022 Benjamin Heisig
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @copyright Copyright (C) 2022 synetics GmbH
* @copyright Copyright (C) 2016-2022 Benjamin Heisig
* @license http://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License (AGPL)
* @link https://github.com/i-doit/api-client-php
*/

declare(strict_types=1);

namespace Idoit\APIClient;

use \Exception;

/**
* Conditional helper for more readable code
*/
class Condition
{

/**
* Operator: AND
*/
const AND = 'AND';

/**
* Operator: URL
*/
const OR = 'OR';

public string $property;

public string $comparison;

public string $value;

public string $operator;

public function where(string $const, string $property):self
{
$this->property = $const . "-" . $property;
return $this;
}

public function andWhere(string $const, string $property):self
{
$this->operator = self::AND;
$this->where($const, $property);
return $this;
}

public function orWhere(string $const, string $property):self
{
$this->operator = self::OR;
$this->where($const, $property);
return $this;
}

public function isLike(string $value):self
{
$this->comparison = 'like';
$this->value = $value;
return $this;
}

public function isNotLike(string $value):self
{
$this->comparison = 'not like';
$this->value = $value;
return $this;
}

public function isEqualTo(string $value):self
{
$this->comparison = '=';
$this->value = $value;
return $this;
}

public function isNotEqualTo(string $value):self
{
$this->comparison = '!=';
$this->value = $value;
return $this;
}

public function isGreaterThan(string $value):self
{
$this->comparison = '>';
$this->value = $value;
return $this;
}

public function isGreaterOrEqaulThan(string $value):self
{
$this->comparison = '>=';
$this->value = $value;
return $this;
}

public function isLowerThan(string $value):self
{
$this->comparison = '<';
$this->value = $value;
return $this;
}

public function isLowerOrEaqualThan(string $value):self
{
$this->comparison = '<=';
$this->value = $value;
return $this;
}

public function isLowerOrGreaterThan(string $value):self
{
$this->comparison = '<>';
$this->value = $value;
return $this;
}

public function __construct($const = null, $property = null, $comparison = null, $value = null, $operator = null)
{

if (!is_null($const) && !is_null($property)) {
$this->property = $const . "-" . $property;
}

$allowedComparison = ['=', '!=', 'like', 'not like', '>', '>=', '<', '<=', '<>'];
if (!is_null($comparison) && !is_null($value) && in_array($comparison, $allowedComparison)) {
$this->comparison = $comparison;
$this->value = $value;
}

$allowedOperators = [self::AND, self::OR];
if (!is_null($operator) && in_array(strtoupper($operator), $allowedOperators)) {
$this->operator = strtoupper($operator);
}
}

public function toArray(): array
{

$condition = [
'property' => $this->property,
'comparison' => $this->comparison,
'value' => $this->value
];

if (isset($this->operator)) {
$condition['operator'] = $this->operator;
}

return $condition;
}

}
Loading

0 comments on commit 47d5979

Please sign in to comment.