33namespace FiveamCode \LaravelNotionApi \Query ;
44
55use FiveamCode \LaravelNotionApi \Exceptions \WrapperException ;
6+ use Illuminate \Support \Collection ;
67
78class Sorting extends QueryHelper
89{
910
10- private string $ timestamp ;
11+ private ? string $ timestamp = null ;
1112 private string $ direction ;
1213
13- public function __construct (string $ timestamp , string $ direction )
14+ public function __construct (string $ direction , string $ property = null , string $ timestamp = null )
1415 {
1516 parent ::__construct ();
1617
17- if ($ this ->validTimestamps ->contains ($ timestamp ))
18+ if ($ timestamp !== null && ! $ this ->validTimestamps ->contains ($ timestamp ))
1819 throw WrapperException::instance (
1920 "Invalid sorting timestamp provided. " , ["invalidTimestamp " => $ timestamp ]
2021 );
2122
22- if ($ this ->validDirections ->contains ($ direction ))
23+
24+ if (!$ this ->validDirections ->contains ($ direction ))
2325 throw WrapperException::instance (
2426 "Invalid sorting direction provided. " , ["invalidDirection " => $ direction ]
2527 );
2628
29+ $ this ->property = $ property ;
2730 $ this ->timestamp = $ timestamp ;
2831 $ this ->direction = $ direction ;
2932 }
3033
34+ public static function timestampSort (string $ timestampToSort , string $ direction )
35+ {
36+
37+ $ propertySort = new Sorting ($ direction , null , $ timestampToSort );
38+
39+ return $ propertySort ;
40+ }
41+
42+ public static function propertySort (string $ property , string $ direction )
43+ {
44+ $ propertySort = new Sorting ($ direction , $ property );
3145
46+ return $ propertySort ;
47+ }
48+
49+ public function toArray (): array
50+ {
51+ if ($ this ->timestamp !== null ) {
52+ return [
53+ "timestamp " => $ this ->timestamp ,
54+ "direction " => $ this ->direction
55+ ];
56+ }
57+
58+ return [
59+ "property " => $ this ->property ,
60+ "direction " => $ this ->direction
61+ ];
62+ }
63+
64+ public static function sortQuery (Collection $ sortings ): array
65+ {
66+
67+ $ querySortings = new Collection ();
68+
69+ $ sortings ->each (function (Sorting $ sorting ) use ($ querySortings ) {
70+ $ querySortings ->add ($ sorting ->toArray ());
71+ });
72+
73+ return $ querySortings ->toArray ();
74+
75+ }
3276
3377
3478}
0 commit comments