Filter: remove duplicated code in combine() already assume by __construct()
This commit is contained in:
parent
6b61ecd6f2
commit
cd303a3a12
2 changed files with 13 additions and 33 deletions
|
@ -146,6 +146,9 @@ class Filter {
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param array<string|Filter|array<string|Filter>|bool> $args
|
* @param array<string|Filter|array<string|Filter>|bool> $args
|
||||||
|
* @throws CombineException
|
||||||
|
* @throws FilterException
|
||||||
|
* @throws ParserException
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(...$args) {
|
public function __construct(...$args) {
|
||||||
|
@ -181,7 +184,7 @@ class Filter {
|
||||||
|
|
||||||
// Check number of filters against logical operator
|
// Check number of filters against logical operator
|
||||||
if (self :: is_not_op($this -> log_op) && count($this -> sub_filters) != 1)
|
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');
|
'Invalid constructor arguments: NOT operator must be followed by exactly one filter');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -361,40 +364,17 @@ class Filter {
|
||||||
*
|
*
|
||||||
* @param string $log_op The locical operator. May be "and", "or", "not" or the subsequent logical
|
* @param string $log_op The locical operator. May be "and", "or", "not" or the subsequent logical
|
||||||
* equivalents "&", "|", "!"
|
* equivalents "&", "|", "!"
|
||||||
* @param array<string|Filter|array<string|Filter>> $args LDAP filters to combine: could be
|
* @param array<string|Filter|array<string|Filter>> $filters LDAP filters to combine: could be
|
||||||
* LDAP objects, LDAP strings or array of
|
* LDAP objects, LDAP strings or array
|
||||||
* LDAP objects or LDAP strings.
|
* of LDAP objects or LDAP strings.
|
||||||
* @throws CombineException
|
* @throws CombineException
|
||||||
|
* @throws FilterException
|
||||||
|
* @throws ParserException
|
||||||
* @return Filter
|
* @return Filter
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
public static function combine($log_op, ...$args) {
|
public static function combine($log_op, ...$filters) {
|
||||||
// Unalias & check logical operator
|
// @phpstan-ignore-next-line
|
||||||
$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!');
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Filter($log_op, ...$filters);
|
return new Filter($log_op, ...$filters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -434,7 +434,7 @@ final class FilterTest extends TestCase {
|
||||||
* @covers \EesyLDAP\Filter::combine
|
* @covers \EesyLDAP\Filter::combine
|
||||||
*/
|
*/
|
||||||
public function testCombineInvalidLogOp() {
|
public function testCombineInvalidLogOp() {
|
||||||
$this->expectException(CombineException::class);
|
$this->expectException(FilterException::class);
|
||||||
$a = Filter::combine('X', new Filter('a', '=', 'b'), new Filter('c', '>=', '2'));
|
$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
|
* @covers \EesyLDAP\Filter::combine
|
||||||
*/
|
*/
|
||||||
public function testCombineInvalidFilter() {
|
public function testCombineInvalidFilter() {
|
||||||
$this->expectException(CombineException::class);
|
$this->expectException(FilterException::class);
|
||||||
// @phpstan-ignore-next-line
|
// @phpstan-ignore-next-line
|
||||||
$a = Filter::combine('&', new Filter('a', '=', 'b'), new FilterException('test'));
|
$a = Filter::combine('&', new Filter('a', '=', 'b'), new FilterException('test'));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue