File tree Expand file tree Collapse file tree 10 files changed +164
-55
lines changed
Expand file tree Collapse file tree 10 files changed +164
-55
lines changed Original file line number Diff line number Diff line change 22
33namespace SilverStripe \Discoverer \Analytics ;
44
5+ use JsonSerializable ;
56use SilverStripe \Core \Injector \Injectable ;
67use SilverStripe \Model \ModelData ;
8+ use stdClass ;
79
8- class AnalyticsData extends ModelData
10+ class AnalyticsData extends ModelData implements JsonSerializable
911{
1012
1113 use Injectable;
@@ -67,6 +69,33 @@ public function setRequestId(mixed $requestId): AnalyticsData
6769 }
6870
6971 public function forTemplate (): string
72+ {
73+ $ data = $ this ->getDataArray ();
74+
75+ if (!$ data ) {
76+ return '' ;
77+ }
78+
79+ $ query = [
80+ '_searchAnalytics ' => base64_encode (json_encode ($ data )),
81+ ];
82+
83+ return http_build_query ($ query );
84+ }
85+
86+ public function jsonSerialize (): array |stdClass
87+ {
88+ $ data = $ this ->getDataArray ();
89+
90+ if (!$ data ) {
91+ // Return an empty stdClass, so that json_encode provides an empty object (rather than an empty array)
92+ return new stdClass ();
93+ }
94+
95+ return $ data ;
96+ }
97+
98+ private function getDataArray (): array
7099 {
71100 $ data = [];
72101
@@ -91,15 +120,7 @@ public function forTemplate(): string
91120 $ data ['requestId ' ] = $ requestId ;
92121 }
93122
94- if (!$ data ) {
95- return '' ;
96- }
97-
98- $ query = [
99- '_searchAnalytics ' => base64_encode (json_encode ($ data )),
100- ];
101-
102- return http_build_query ($ query );
123+ return $ data ;
103124 }
104125
105126}
Original file line number Diff line number Diff line change 22
33namespace SilverStripe \Discoverer \Service \Results ;
44
5+ use JsonSerializable ;
56use SilverStripe \Model \List \ArrayList ;
67use SilverStripe \Model \ModelData ;
78
8- class Facet extends ModelData
9+ class Facet extends ModelData implements JsonSerializable
910{
1011
1112 /**
@@ -80,4 +81,20 @@ public function setType(?string $type): static
8081 return $ this ;
8182 }
8283
84+ public function jsonSerialize (): array
85+ {
86+ $ data = [];
87+
88+ foreach ($ this ->getData () as $ facetData ) {
89+ $ data [] = $ facetData ->jsonSerialize ();
90+ }
91+
92+ return [
93+ 'data ' => $ data ,
94+ 'name ' => $ this ->getName (),
95+ 'fieldName ' => $ this ->getFieldName (),
96+ 'type ' => $ this ->getType (),
97+ ];
98+ }
99+
83100}
Original file line number Diff line number Diff line change 22
33namespace SilverStripe \Discoverer \Service \Results ;
44
5+ use JsonSerializable ;
56use SilverStripe \Model \ModelData ;
67
7- class FacetData extends ModelData
8+ class FacetData extends ModelData implements JsonSerializable
89{
910
1011 private int $ count ;
@@ -77,4 +78,15 @@ public function setValue(string|int|float $value): static
7778 return $ this ;
7879 }
7980
81+ public function jsonSerialize (): array
82+ {
83+ return [
84+ 'count ' => $ this ->getCount (),
85+ 'from ' => $ this ->getFrom (),
86+ 'to ' => $ this ->getTo (),
87+ 'name ' => $ this ->getName (),
88+ 'value ' => $ this ->getValue (),
89+ ];
90+ }
91+
8092}
Original file line number Diff line number Diff line change 22
33namespace SilverStripe \Discoverer \Service \Results ;
44
5+ use JsonSerializable ;
56use SilverStripe \Model \List \ArrayList ;
67
78/**
89 * @extends ArrayList<Facet>
910 */
10- class Facets extends ArrayList
11+ class Facets extends ArrayList implements JsonSerializable
1112{
1213
1314 public function forTemplate (): string
1415 {
1516 return $ this ->renderWith (static ::class);
1617 }
1718
19+ public function jsonSerialize (): array
20+ {
21+ return $ this ->toArray ();
22+ }
23+
1824}
Original file line number Diff line number Diff line change 33namespace SilverStripe \Discoverer \Service \Results ;
44
55use ArrayIterator ;
6+ use JsonSerializable ;
67use SilverStripe \Model \ModelData ;
78use Traversable ;
89
9- class Field extends ModelData
10+ class Field extends ModelData implements JsonSerializable
1011{
1112
1213 public function __construct (private mixed $ raw = null , private mixed $ formatted = null )
@@ -60,6 +61,14 @@ public function getIterator(): Traversable
6061 return new ArrayIterator ($ arrayValue );
6162 }
6263
64+ public function jsonSerialize (): array
65+ {
66+ return [
67+ 'raw ' => $ this ->getRaw (),
68+ 'formatted ' => $ this ->getFormatted (),
69+ ];
70+ }
71+
6372 /**
6473 * Silverstripe 5.3 will have native support for looping primitives in templates:
6574 * https://github.com/silverstripe/silverstripe-framework/issues/11196
Original file line number Diff line number Diff line change 33namespace SilverStripe \Discoverer \Service \Results ;
44
55use Exception ;
6+ use JsonSerializable ;
67use SilverStripe \Control \Controller ;
78use SilverStripe \Discoverer \Analytics \AnalyticsData ;
89use SilverStripe \Model \ModelData ;
10+ use stdClass ;
911
10- class Record extends ModelData
12+ class Record extends ModelData implements JsonSerializable
1113{
1214
1315 private ?AnalyticsData $ analyticsData = null ;
1416
17+ private array $ fields = [];
18+
1519 public function forTemplate (): string
1620 {
1721 return $ this ->renderWith (static ::class);
@@ -60,7 +64,27 @@ public function __set($property, $value): void
6064 throw new Exception (sprintf ('Field value must be an instance of %s ' , Field::class));
6165 }
6266
67+ // Keep track of what fields have been added (atm, mostly just used for jsonSerialize, as we have no way of
68+ // retrieving dynamic data if we don't want what fields to look up)
69+ $ this ->fields [] = $ property ;
70+
6371 parent ::__set ($ property , $ value );
6472 }
6573
74+ public function jsonSerialize (): array |stdClass
75+ {
76+ $ data = [];
77+
78+ foreach ($ this ->fields as $ field ) {
79+ $ data [$ field ] = $ this ->__get ($ field );
80+ }
81+
82+ if (!$ data ) {
83+ // Return an empty stdClass, so that json_encode provides an empty object (rather than an empty array)
84+ return new stdClass ();
85+ }
86+
87+ return $ data ;
88+ }
89+
6690}
Original file line number Diff line number Diff line change 22
33namespace SilverStripe \Discoverer \Service \Results ;
44
5+ use JsonSerializable ;
56use SilverStripe \Model \List \ArrayList ;
67use SilverStripe \Model \List \PaginatedList ;
78
89/**
910 * @extends PaginatedList<ArrayList<Record>, Record>
1011 */
11- class Records extends PaginatedList
12+ class Records extends PaginatedList implements JsonSerializable
1213{
1314
1415 public function forTemplate (): string
1516 {
1617 return $ this ->renderWith (static ::class);
1718 }
1819
20+ public function jsonSerialize (): array
21+ {
22+ return $ this ->toArray ();
23+ }
24+
1925}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace SilverStripe \Discoverer \Service \Results ;
4+
5+ use JsonSerializable ;
6+ use SilverStripe \Model \ModelData ;
7+
8+ abstract class Response extends ModelData implements JsonSerializable
9+ {
10+
11+ private bool $ success = false ;
12+
13+ public function forTemplate (): string
14+ {
15+ return $ this ->renderWith (static ::class);
16+ }
17+
18+ public function isSuccess (): bool
19+ {
20+ return $ this ->success ;
21+ }
22+
23+ public function setSuccess (bool $ success ): static
24+ {
25+ $ this ->success = $ success ;
26+
27+ return $ this ;
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 55use SilverStripe \Core \Injector \Injectable ;
66use SilverStripe \Discoverer \Query \Query ;
77use SilverStripe \Model \List \ArrayList ;
8- use SilverStripe \Model \ModelData ;
98
10- class Results extends ModelData
9+ class Results extends Response
1110{
1211
1312 use Injectable;
@@ -18,8 +17,6 @@ class Results extends ModelData
1817
1918 private ?string $ indexName = null ;
2019
21- private bool $ success = false ;
22-
2320 public function __construct (private readonly Query $ query )
2421 {
2522 parent ::__construct ();
@@ -28,11 +25,6 @@ public function __construct(private readonly Query $query)
2825 $ this ->facets = Facets::create ();
2926 }
3027
31- public function forTemplate (): string
32- {
33- return $ this ->renderWith (static ::class);
34- }
35-
3628 public function getRecords (): ?Records
3729 {
3830 return $ this ->records ;
@@ -74,16 +66,12 @@ public function setIndexName(?string $indexName): static
7466 return $ this ;
7567 }
7668
77- public function isSuccess (): bool
69+ public function jsonSerialize (): array
7870 {
79- return $ this ->success ;
80- }
81-
82- public function setSuccess (bool $ success ): Results
83- {
84- $ this ->success = $ success ;
85-
86- return $ this ;
71+ return [
72+ 'records ' => $ this ->getRecords ()->jsonSerialize (),
73+ 'facets ' => $ this ->getFacets ()->jsonSerialize (),
74+ ];
8775 }
8876
8977}
You can’t perform that action at this time.
0 commit comments