Skip to content

Commit d66344a

Browse files
authored
Merge pull request #748 from DirectoryTree/FEATURE-744
Feature 744 - Add ability to provide LDAP control options to orderBy
2 parents c9a43cf + 365ae2e commit d66344a

File tree

2 files changed

+87
-8
lines changed

2 files changed

+87
-8
lines changed

src/Query/Builder.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -850,19 +850,23 @@ public function addSelect(array|string $column): static
850850
/**
851851
* Add an order by control to the query.
852852
*/
853-
public function orderBy(string $attribute, string $direction = 'asc'): static
853+
public function orderBy(string $attribute, string $direction = 'asc', array $options = []): static
854854
{
855855
return $this->addControl(LDAP_CONTROL_SORTREQUEST, true, [
856-
['attr' => $attribute, 'reverse' => $direction === 'desc'],
856+
[
857+
...$options,
858+
'attr' => $attribute,
859+
'reverse' => $direction === 'desc',
860+
],
857861
]);
858862
}
859863

860864
/**
861865
* Add an order by descending control to the query.
862866
*/
863-
public function orderByDesc(string $attribute): static
867+
public function orderByDesc(string $attribute, array $options = []): static
864868
{
865-
return $this->orderBy($attribute, 'desc');
869+
return $this->orderBy($attribute, 'desc', $options);
866870
}
867871

868872
/**

tests/Unit/Query/BuilderTest.php

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LdapRecord\Tests\Unit\Query;
44

55
use DateTime;
6+
use InvalidArgumentException;
67
use LdapRecord\Connection;
78
use LdapRecord\Container;
89
use LdapRecord\LdapRecordException;
@@ -109,7 +110,7 @@ public function test_add_filter()
109110

110111
public function test_adding_filter_with_invalid_bindings_throws_exception()
111112
{
112-
$this->expectException(\InvalidArgumentException::class);
113+
$this->expectException(InvalidArgumentException::class);
113114

114115
// Missing 'value' key.
115116
$this->newBuilder()->addFilter('and', [
@@ -120,7 +121,7 @@ public function test_adding_filter_with_invalid_bindings_throws_exception()
120121

121122
public function test_adding_invalid_filter_type_throws_exception()
122123
{
123-
$this->expectException(\InvalidArgumentException::class);
124+
$this->expectException(InvalidArgumentException::class);
124125

125126
$this->newBuilder()->addFilter('non-existent', [
126127
'field' => 'cn',
@@ -467,7 +468,7 @@ public function test_or_where_ends_with()
467468

468469
public function test_where_invalid_operator()
469470
{
470-
$this->expectException(\InvalidArgumentException::class);
471+
$this->expectException(InvalidArgumentException::class);
471472

472473
$b = $this->newBuilder();
473474

@@ -476,7 +477,7 @@ public function test_where_invalid_operator()
476477

477478
public function test_or_where_invalid_operator()
478479
{
479-
$this->expectException(\InvalidArgumentException::class);
480+
$this->expectException(InvalidArgumentException::class);
480481

481482
$b = $this->newBuilder();
482483

@@ -1025,6 +1026,80 @@ public function test_get()
10251026
$this->assertEquals($result, $b->get());
10261027
}
10271028

1029+
public function test_order_by()
1030+
{
1031+
$b = $this->newBuilder();
1032+
1033+
$b->orderBy('foo', 'asc');
1034+
1035+
$this->assertEquals($b->controls, [
1036+
LDAP_CONTROL_SORTREQUEST => [
1037+
'oid' => LDAP_CONTROL_SORTREQUEST,
1038+
'isCritical' => true,
1039+
'value' => [
1040+
[
1041+
'attr' => 'foo',
1042+
'reverse' => false,
1043+
],
1044+
],
1045+
],
1046+
]);
1047+
}
1048+
1049+
public function test_order_by_desc()
1050+
{
1051+
$b = $this->newBuilder();
1052+
1053+
$b->orderByDesc('foo');
1054+
1055+
$this->assertEquals($b->controls, [
1056+
LDAP_CONTROL_SORTREQUEST => [
1057+
'oid' => LDAP_CONTROL_SORTREQUEST,
1058+
'isCritical' => true,
1059+
'value' => [
1060+
[
1061+
'attr' => 'foo',
1062+
'reverse' => true,
1063+
],
1064+
],
1065+
],
1066+
]);
1067+
}
1068+
1069+
public function test_order_by_with_options()
1070+
{
1071+
$b = $this->newBuilder();
1072+
1073+
$b->orderBy('foo', 'asc', [
1074+
'oid' => $oid = '2.5.13.2',
1075+
]);
1076+
1077+
$this->assertEquals($b->controls, [
1078+
LDAP_CONTROL_SORTREQUEST => [
1079+
'oid' => LDAP_CONTROL_SORTREQUEST,
1080+
'isCritical' => true,
1081+
'value' => [
1082+
[
1083+
'attr' => 'foo',
1084+
'reverse' => false,
1085+
'oid' => $oid,
1086+
],
1087+
],
1088+
],
1089+
]);
1090+
}
1091+
1092+
public function test_has_order_by()
1093+
{
1094+
$b = $this->newBuilder();
1095+
1096+
$this->assertFalse($b->hasOrderBy());
1097+
1098+
$b->orderBy('foo', 'asc');
1099+
1100+
$this->assertTrue($b->hasOrderBy());
1101+
}
1102+
10281103
public function test_first()
10291104
{
10301105
$b = $this->newBuilder();

0 commit comments

Comments
 (0)