Filter: remove duplicated code in combine() already assume by __construct()

This commit is contained in:
Benjamin Renard 2023-03-14 21:20:02 +01:00
parent 6b61ecd6f2
commit cd303a3a12
2 changed files with 13 additions and 33 deletions

View file

@ -146,6 +146,9 @@ class Filter {
/**
* Constructor
* @param array<string|Filter|array<string|Filter>|bool> $args
* @throws CombineException
* @throws FilterException
* @throws ParserException
* @return void
*/
public function __construct(...$args) {
@ -181,7 +184,7 @@ class Filter {
// Check number of filters against logical operator
if (self :: is_not_op($this -> log_op) && count($this -> sub_filters) != 1)
throw new FilterException(
throw new CombineException(
'Invalid constructor arguments: NOT operator must be followed by exactly one filter');
return;
}
@ -361,40 +364,17 @@ class Filter {
*
* @param string $log_op The locical operator. May be "and", "or", "not" or the subsequent logical
* equivalents "&", "|", "!"
* @param array<string|Filter|array<string|Filter>> $args LDAP filters to combine: could be
* LDAP objects, LDAP strings or array of
* LDAP objects or LDAP strings.
* @param array<string|Filter|array<string|Filter>> $filters LDAP filters to combine: could be
* LDAP objects, LDAP strings or array
* of LDAP objects or LDAP strings.
* @throws CombineException
* @throws FilterException
* @throws ParserException
* @return Filter
* @static
*/
public static function combine($log_op, ...$args) {
// Unalias & check logical operator
$log_op = self :: unalias_log_op($log_op);
if (!$log_op) throw new CombineException('Invalid logical operator provided!');
// Convert args as filters
$filters = array();
foreach ($args as $arg) {
if (!is_array($arg))
$arg = array($arg);
foreach ($arg as $a) {
if (is_string($a))
$a = self :: parse($a);
if (!$a instanceof Filter)
throw new CombineException(
'Invalid filter provided: must be a Filter object or a LDAP filter string!'
);
$filters[] = $a;
}
}
// Check number of filters against logical operator
if (self :: is_not_op($log_op)) {
if (count($filters) != 1)
throw new CombineException('NOT operator accept only one filter!');
}
public static function combine($log_op, ...$filters) {
// @phpstan-ignore-next-line
return new Filter($log_op, ...$filters);
}

View file

@ -434,7 +434,7 @@ final class FilterTest extends TestCase {
* @covers \EesyLDAP\Filter::combine
*/
public function testCombineInvalidLogOp() {
$this->expectException(CombineException::class);
$this->expectException(FilterException::class);
$a = Filter::combine('X', new Filter('a', '=', 'b'), new Filter('c', '>=', '2'));
}
@ -442,7 +442,7 @@ final class FilterTest extends TestCase {
* @covers \EesyLDAP\Filter::combine
*/
public function testCombineInvalidFilter() {
$this->expectException(CombineException::class);
$this->expectException(FilterException::class);
// @phpstan-ignore-next-line
$a = Filter::combine('&', new Filter('a', '=', 'b'), new FilterException('test'));
}