Skip to content

Commit bffba7d

Browse files
committed
Fixes
1 parent 3d9f433 commit bffba7d

File tree

12 files changed

+166
-61
lines changed

12 files changed

+166
-61
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
ci:
10-
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
10+
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v2
1111
with:
1212
# disable auto detection of JS tests (remove if any JS tests are added)
1313
js: false

src/Analytics/AnalyticsData.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace SilverStripe\Discoverer\Analytics;
44

5+
use JsonSerializable;
56
use SilverStripe\Core\Injector\Injectable;
67
use 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
}

src/Service/Results/Facet.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace SilverStripe\Discoverer\Service\Results;
44

5+
use JsonSerializable;
56
use SilverStripe\Model\List\ArrayList;
67
use 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
}

src/Service/Results/FacetData.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace SilverStripe\Discoverer\Service\Results;
44

5+
use JsonSerializable;
56
use 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
}

src/Service/Results/Facets.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
namespace SilverStripe\Discoverer\Service\Results;
44

5+
use JsonSerializable;
56
use 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
}

src/Service/Results/Field.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace SilverStripe\Discoverer\Service\Results;
44

55
use ArrayIterator;
6+
use JsonSerializable;
67
use SilverStripe\Model\ModelData;
78
use 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

src/Service/Results/Record.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
namespace SilverStripe\Discoverer\Service\Results;
44

55
use Exception;
6+
use JsonSerializable;
67
use SilverStripe\Control\Controller;
78
use SilverStripe\Discoverer\Analytics\AnalyticsData;
89
use 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
}

src/Service/Results/Records.php

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

33
namespace SilverStripe\Discoverer\Service\Results;
44

5+
use JsonSerializable;
56
use SilverStripe\Model\List\ArrayList;
67
use 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
}

src/Service/Results/Response.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

src/Service/Results/Results.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
use SilverStripe\Core\Injector\Injectable;
66
use SilverStripe\Discoverer\Query\Query;
77
use 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
}

0 commit comments

Comments
 (0)