Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Condition helper class for more readable code #30

Merged
merged 12 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading