From f6c96783d423e45b5b950f2dee28cfdcae44d127 Mon Sep 17 00:00:00 2001 From: Antti Peisa Date: Sat, 16 Jul 2022 12:50:09 +0300 Subject: [PATCH 1/7] support stringable objects when sorting --- src/Query/Builder.php | 2 +- tests/QueryTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index f83bce3e2..3c60a071f 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -512,7 +512,7 @@ public function orderBy($column, $direction = 'asc') if ($column == 'natural') { $this->orders['$natural'] = $direction; } else { - $this->orders[$column] = $direction; + $this->orders[(string) $column] = $direction; } return $this; diff --git a/tests/QueryTest.php b/tests/QueryTest.php index b2716e178..db5c81787 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -239,6 +239,17 @@ public function testOrder(): void $this->assertEquals(35, $user->age); } + public function testStringableOrder(): void + { + $age = new stringableObject("age"); + + $user = User::whereNotNull('age')->orderBy($age, 'asc')->first(); + $this->assertEquals(13, $user->age); + + $user = User::whereNotNull('age')->orderBy($age, 'desc')->first(); + $this->assertEquals(37, $user->age); + } + public function testGroupBy(): void { $users = User::groupBy('title')->get(); @@ -470,3 +481,21 @@ public function testMultipleSortOrder(): void $this->assertEquals('Brett Boe', $subset[2]->name); } } + +/** + * Mockup class to test stringable objects + */ +class stringableObject implements Stringable { + + private $string; + + public function __construct($string) + { + $this->string = $string; + } + + public function __toString() + { + return $this->string; + } +} From 496f8a0ba7a04637ba6b3c52edb6feef6ffc3581 Mon Sep 17 00:00:00 2001 From: Antti Peisa Date: Sat, 16 Jul 2022 12:52:29 +0300 Subject: [PATCH 2/7] style code fixes --- tests/QueryTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index db5c81787..beaf3f408 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -241,7 +241,7 @@ public function testOrder(): void public function testStringableOrder(): void { - $age = new stringableObject("age"); + $age = new stringableObject('age'); $user = User::whereNotNull('age')->orderBy($age, 'asc')->first(); $this->assertEquals(13, $user->age); @@ -483,7 +483,7 @@ public function testMultipleSortOrder(): void } /** - * Mockup class to test stringable objects + * Mockup class to test stringable objects. */ class stringableObject implements Stringable { From 50221ef37edd448605d7b9686402aeb220b902a5 Mon Sep 17 00:00:00 2001 From: Antti Peisa Date: Sat, 16 Jul 2022 12:53:31 +0300 Subject: [PATCH 3/7] style code fixes pt. 2 --- tests/QueryTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index beaf3f408..45151ea1e 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -486,7 +486,6 @@ public function testMultipleSortOrder(): void * Mockup class to test stringable objects. */ class stringableObject implements Stringable { - private $string; public function __construct($string) From ed86610b85245653f8aa83f6deab7b71da92039a Mon Sep 17 00:00:00 2001 From: Antti Peisa Date: Sat, 16 Jul 2022 12:55:21 +0300 Subject: [PATCH 4/7] make the stringable object type safe --- tests/QueryTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 45151ea1e..b790c723b 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -486,9 +486,9 @@ public function testMultipleSortOrder(): void * Mockup class to test stringable objects. */ class stringableObject implements Stringable { - private $string; + private String $string; - public function __construct($string) + public function __construct(String $string) { $this->string = $string; } From 3a87b28aaaa352bcdffc3e106733f64caab3c5dd Mon Sep 17 00:00:00 2001 From: Antti Peisa Date: Sat, 16 Jul 2022 13:02:38 +0300 Subject: [PATCH 5/7] style code fixes pt. 3 --- tests/QueryTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index b790c723b..378da9d1d 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -486,9 +486,9 @@ public function testMultipleSortOrder(): void * Mockup class to test stringable objects. */ class stringableObject implements Stringable { - private String $string; + private string $string; - public function __construct(String $string) + public function __construct(string $string) { $this->string = $string; } From f7895bcfd5e57dbc22c9119efdda5850ba15292d Mon Sep 17 00:00:00 2001 From: Antti Peisa Date: Mon, 25 Jul 2022 13:58:59 +0300 Subject: [PATCH 6/7] Update tests/QueryTest.php Co-authored-by: Divine <48183131+divine@users.noreply.github.com> --- tests/QueryTest.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 378da9d1d..d4b1eef3f 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -481,20 +481,3 @@ public function testMultipleSortOrder(): void $this->assertEquals('Brett Boe', $subset[2]->name); } } - -/** - * Mockup class to test stringable objects. - */ -class stringableObject implements Stringable { - private string $string; - - public function __construct(string $string) - { - $this->string = $string; - } - - public function __toString() - { - return $this->string; - } -} From f670c5fe5ac8a6775d97e11632dc3e1de01aa054 Mon Sep 17 00:00:00 2001 From: Antti Peisa Date: Mon, 25 Jul 2022 13:59:05 +0300 Subject: [PATCH 7/7] Update tests/QueryTest.php Co-authored-by: Divine <48183131+divine@users.noreply.github.com> --- tests/QueryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index d4b1eef3f..c85cd2a21 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -241,7 +241,7 @@ public function testOrder(): void public function testStringableOrder(): void { - $age = new stringableObject('age'); + $age = str('age'); $user = User::whereNotNull('age')->orderBy($age, 'asc')->first(); $this->assertEquals(13, $user->age);