Skip to content

Commit becc123

Browse files
committed
Refactor query filter parser
1 parent 052de90 commit becc123

17 files changed

+472
-208
lines changed

src/Query/Filter/AndGroup.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LdapRecord\Query\Filter;
44

5-
class AndGroup implements Filter
5+
class AndGroup implements GroupFilter
66
{
77
/**
88
* The filters in the group.
@@ -29,6 +29,22 @@ public function getFilters(): array
2929
return $this->filters;
3030
}
3131

32+
/**
33+
* Get the group's operator.
34+
*/
35+
public function getOperator(): string
36+
{
37+
return '&';
38+
}
39+
40+
/**
41+
* Get the raw filter string (without outer parentheses).
42+
*/
43+
public function getRaw(): string
44+
{
45+
return '&'.implode('', array_map(fn (Filter $filter) => (string) $filter, $this->filters));
46+
}
47+
3248
/**
3349
* Compile the filter to its LDAP string representation.
3450
*/
@@ -38,6 +54,6 @@ public function __toString(): string
3854
return '';
3955
}
4056

41-
return '(&'.implode('', array_map(fn (Filter $filter) => (string) $filter, $this->filters)).')';
57+
return '('.$this->getRaw().')';
4258
}
4359
}

src/Query/Filter/ApproximatelyEquals.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LdapRecord\Query\Filter;
44

5-
class ApproximatelyEquals implements Filter
5+
class ApproximatelyEquals implements ConditionFilter
66
{
77
/**
88
* Create a new approximately equals filter.
@@ -12,11 +12,43 @@ public function __construct(
1212
protected string $value
1313
) {}
1414

15+
/**
16+
* Get the filter's attribute.
17+
*/
18+
public function getAttribute(): string
19+
{
20+
return $this->attribute;
21+
}
22+
23+
/**
24+
* Get the filter's operator.
25+
*/
26+
public function getOperator(): string
27+
{
28+
return '~=';
29+
}
30+
31+
/**
32+
* Get the filter's value.
33+
*/
34+
public function getValue(): string
35+
{
36+
return $this->value;
37+
}
38+
39+
/**
40+
* Get the raw filter string (without parentheses).
41+
*/
42+
public function getRaw(): string
43+
{
44+
return "{$this->attribute}~={$this->value}";
45+
}
46+
1547
/**
1648
* Compile the filter to its LDAP string representation.
1749
*/
1850
public function __toString(): string
1951
{
20-
return "({$this->attribute}~={$this->value})";
52+
return "({$this->getRaw()})";
2153
}
2254
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace LdapRecord\Query\Filter;
4+
5+
interface ConditionFilter extends Filter
6+
{
7+
/**
8+
* Get the filter's attribute.
9+
*/
10+
public function getAttribute(): string;
11+
12+
/**
13+
* Get the filter's operator.
14+
*/
15+
public function getOperator(): string;
16+
17+
/**
18+
* Get the filter's value.
19+
*/
20+
public function getValue(): ?string;
21+
22+
/**
23+
* Get the raw filter string (without parentheses).
24+
*/
25+
public function getRaw(): string;
26+
}
27+

src/Query/Filter/ConditionNode.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/Query/Filter/Contains.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LdapRecord\Query\Filter;
44

5-
class Contains implements Filter
5+
class Contains implements ConditionFilter
66
{
77
/**
88
* Create a new contains filter.
@@ -12,11 +12,43 @@ public function __construct(
1212
protected string $value
1313
) {}
1414

15+
/**
16+
* Get the filter's attribute.
17+
*/
18+
public function getAttribute(): string
19+
{
20+
return $this->attribute;
21+
}
22+
23+
/**
24+
* Get the filter's operator.
25+
*/
26+
public function getOperator(): string
27+
{
28+
return '=';
29+
}
30+
31+
/**
32+
* Get the filter's value.
33+
*/
34+
public function getValue(): string
35+
{
36+
return $this->value;
37+
}
38+
39+
/**
40+
* Get the raw filter string (without parentheses).
41+
*/
42+
public function getRaw(): string
43+
{
44+
return "{$this->attribute}=*{$this->value}*";
45+
}
46+
1547
/**
1648
* Compile the filter to its LDAP string representation.
1749
*/
1850
public function __toString(): string
1951
{
20-
return "({$this->attribute}=*{$this->value}*)";
52+
return "({$this->getRaw()})";
2153
}
2254
}

src/Query/Filter/EndsWith.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LdapRecord\Query\Filter;
44

5-
class EndsWith implements Filter
5+
class EndsWith implements ConditionFilter
66
{
77
/**
88
* Create a new ends with filter.
@@ -12,11 +12,43 @@ public function __construct(
1212
protected string $value
1313
) {}
1414

15+
/**
16+
* Get the filter's attribute.
17+
*/
18+
public function getAttribute(): string
19+
{
20+
return $this->attribute;
21+
}
22+
23+
/**
24+
* Get the filter's operator.
25+
*/
26+
public function getOperator(): string
27+
{
28+
return '=';
29+
}
30+
31+
/**
32+
* Get the filter's value.
33+
*/
34+
public function getValue(): string
35+
{
36+
return $this->value;
37+
}
38+
39+
/**
40+
* Get the raw filter string (without parentheses).
41+
*/
42+
public function getRaw(): string
43+
{
44+
return "{$this->attribute}=*{$this->value}";
45+
}
46+
1547
/**
1648
* Compile the filter to its LDAP string representation.
1749
*/
1850
public function __toString(): string
1951
{
20-
return "({$this->attribute}=*{$this->value})";
52+
return "({$this->getRaw()})";
2153
}
2254
}

src/Query/Filter/Equals.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LdapRecord\Query\Filter;
44

5-
class Equals implements Filter
5+
class Equals implements ConditionFilter
66
{
77
/**
88
* Create a new equals filter.
@@ -12,11 +12,43 @@ public function __construct(
1212
protected string $value
1313
) {}
1414

15+
/**
16+
* Get the filter's attribute.
17+
*/
18+
public function getAttribute(): string
19+
{
20+
return $this->attribute;
21+
}
22+
23+
/**
24+
* Get the filter's operator.
25+
*/
26+
public function getOperator(): string
27+
{
28+
return '=';
29+
}
30+
31+
/**
32+
* Get the filter's value.
33+
*/
34+
public function getValue(): string
35+
{
36+
return $this->value;
37+
}
38+
39+
/**
40+
* Get the raw filter string (without parentheses).
41+
*/
42+
public function getRaw(): string
43+
{
44+
return "{$this->attribute}={$this->value}";
45+
}
46+
1547
/**
1648
* Compile the filter to its LDAP string representation.
1749
*/
1850
public function __toString(): string
1951
{
20-
return "({$this->attribute}={$this->value})";
52+
return "({$this->getRaw()})";
2153
}
2254
}

0 commit comments

Comments
 (0)