Skip to content

Commit f82bd4c

Browse files
Remove resolved partial
1 parent d122f0a commit f82bd4c

12 files changed

+438
-522
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"test" : "./vendor/bin/pest --no-coverage",
5757
"test-coverage" : "vendor/bin/pest --coverage-html coverage",
5858
"format" : "vendor/bin/php-cs-fixer fix --allow-risky=yes",
59-
"benchmark" : "vendor/bin/phpbench run --report=default",
59+
"benchmark" : "vendor/bin/phpbench run",
6060
"benchmark-profiled" : "vendor/bin/phpbench "
6161
},
6262
"config" : {

src/Resolvers/VisibleDataFieldsResolver.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected function performExcept(
131131

132132
if ($nested = $exceptPartial->getNested()) {
133133
try {
134-
$fields[$nested]->addExceptResolvedPartial($exceptPartial->next());
134+
$fields[$nested]->addExceptPartial($exceptPartial->next());
135135
} catch (ErrorException $exception) {
136136
$this->handleNonExistingNestedField($exception, PartialType::Except, $nested, $dataClass, $transformationContext);
137137
}
@@ -169,7 +169,7 @@ protected function performOnly(
169169

170170
if ($nested = $onlyPartial->getNested()) {
171171
try {
172-
$fields[$nested]->addOnlyResolvedPartial($onlyPartial->next());
172+
$fields[$nested]->addOnlyPartial($onlyPartial->next());
173173
$onlyFields[] = $nested;
174174
} catch (ErrorException $exception) {
175175
$this->handleNonExistingNestedField($exception, PartialType::Only, $nested, $dataClass, $transformationContext);
@@ -220,15 +220,15 @@ protected function resolveIncludedFields(
220220

221221
foreach ($includedFields as $includedField) {
222222
// can be null when field is a non data object/collectable or array
223-
$fields[$includedField]?->addIncludedResolvedPartial($includedPartial->next());
223+
$fields[$includedField]?->addIncludedPartial($includedPartial->next());
224224
}
225225

226226
break;
227227
}
228228

229229
if ($nested = $includedPartial->getNested()) {
230230
try {
231-
$fields[$nested]->addIncludedResolvedPartial($includedPartial->next());
231+
$fields[$nested]->addIncludedPartial($includedPartial->next());
232232
$includedFields[] = $nested;
233233
} catch (ErrorException $exception) {
234234
$this->handleNonExistingNestedField($exception, PartialType::Include, $nested, $dataClass, $transformationContext);
@@ -268,15 +268,15 @@ protected function resolveExcludedFields(
268268
->all();
269269

270270
foreach ($excludedFields as $excludedField) {
271-
$fields[$excludedField]?->addExcludedResolvedPartial($excludePartial->next());
271+
$fields[$excludedField]?->addExcludedPartial($excludePartial->next());
272272
}
273273

274274
break;
275275
}
276276

277277
if ($nested = $excludePartial->getNested()) {
278278
try {
279-
$fields[$nested]->addExcludedResolvedPartial($excludePartial->next());
279+
$fields[$nested]->addExcludedPartial($excludePartial->next());
280280
} catch (ErrorException $exception) {
281281
$this->handleNonExistingNestedField($exception, PartialType::Exclude, $nested, $dataClass, $transformationContext);
282282
}

src/Support/Partials/Partial.php

Lines changed: 118 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@
1313

1414
class Partial implements Stringable
1515
{
16-
protected readonly ResolvedPartial $resolvedPartial;
16+
protected int $segmentCount;
17+
18+
protected bool $endsInAll;
1719

1820
/**
1921
* @param array<int, PartialSegment> $segments
2022
*/
2123
public function __construct(
2224
public array $segments,
23-
public bool $permanent,
24-
public ?Closure $condition,
25+
public bool $permanent = false,
26+
public ?Closure $condition = null,
27+
public int $pointer = 0,
2528
) {
26-
$this->resolvedPartial = new ResolvedPartial($segments);
29+
$this->segmentCount = count($segments);
30+
$this->endsInAll = $this->segmentCount === 0
31+
? false
32+
: $this->segments[$this->segmentCount - 1] instanceof AllPartialSegment;
2733
}
2834

2935
public static function create(
@@ -107,19 +113,120 @@ protected static function resolveSegmentsFromPath(string $path): array
107113
return $segments;
108114
}
109115

110-
public function resolve(BaseData|BaseDataCollectable $data): ?ResolvedPartial
116+
public function isUndefined(): bool
111117
{
112-
if ($this->condition === null) {
113-
return $this->resolvedPartial->reset();
118+
return ! $this->endsInAll && $this->pointer >= $this->segmentCount;
119+
}
120+
121+
public function isAll(): bool
122+
{
123+
return $this->endsInAll && $this->pointer >= $this->segmentCount - 1;
124+
}
125+
126+
public function getNested(): ?string
127+
{
128+
$segment = $this->getCurrentSegment();
129+
130+
if ($segment === null) {
131+
return null;
132+
}
133+
134+
if (! $segment instanceof NestedPartialSegment) {
135+
return null;
136+
}
137+
138+
return $segment->field;
139+
}
140+
141+
public function getFields(): ?array
142+
{
143+
if ($this->isUndefined()) {
144+
return null;
145+
}
146+
147+
$segment = $this->getCurrentSegment();
148+
149+
if ($segment === null) {
150+
return null;
114151
}
115152

116-
if (($this->condition)($data)) {
117-
return $this->resolvedPartial->reset();
153+
if (! $segment instanceof FieldsPartialSegment) {
154+
return null;
118155
}
119156

120-
return null;
157+
return $segment->fields;
121158
}
122159

160+
/** @return string[] */
161+
public function toLaravel(): array
162+
{
163+
/** @var array<string> $segments */
164+
$segments = [];
165+
166+
for ($i = $this->pointer; $i < $this->segmentCount; $i++) {
167+
$segment = $this->segments[$i];
168+
169+
if ($segment instanceof AllPartialSegment) {
170+
$segments[] = '*';
171+
172+
continue;
173+
}
174+
175+
if ($segment instanceof NestedPartialSegment) {
176+
$segments[] = $segment->field;
177+
178+
continue;
179+
}
180+
181+
if ($segment instanceof FieldsPartialSegment) {
182+
$segmentsAsString = count($segments) === 0
183+
? ''
184+
: implode('.', $segments).'.';
185+
186+
return array_map(
187+
fn (string $field) => "{$segmentsAsString}{$field}",
188+
$segment->fields
189+
);
190+
}
191+
}
192+
193+
return [implode('.', $segments)];
194+
}
195+
196+
public function next(): self
197+
{
198+
$this->pointer++;
199+
200+
return $this;
201+
}
202+
203+
public function rollbackWhenRequired(): void
204+
{
205+
$this->pointer--;
206+
}
207+
208+
public function reset(): self
209+
{
210+
$this->pointer = 0;
211+
212+
return $this;
213+
}
214+
215+
protected function getCurrentSegment(): ?PartialSegment
216+
{
217+
return $this->segments[$this->pointer] ?? null;
218+
}
219+
220+
public function isRequired(BaseData|BaseDataCollectable $data): bool
221+
{
222+
if ($this->condition === null) {
223+
return true;
224+
}
225+
226+
return ($this->condition)($data);
227+
}
228+
229+
123230
public function toArray(): array
124231
{
125232
return [
@@ -131,6 +238,6 @@ public function toArray(): array
131238

132239
public function __toString(): string
133240
{
134-
return implode('.', $this->segments);
241+
return implode('.', $this->segments)." (current: {$this->pointer})";
135242
}
136243
}

src/Support/Partials/PartialsCollection.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace Spatie\LaravelData\Support\Partials;
44

55
use SplObjectStorage;
6+
use Stringable;
67

78
/**
89
* @extends SplObjectStorage<Partial, null>
910
*/
10-
class PartialsCollection extends SplObjectStorage
11+
class PartialsCollection extends SplObjectStorage implements Stringable
1112
{
1213
public static function create(Partial ...$partials): self
1314
{
@@ -30,4 +31,15 @@ public function toArray(): array
3031

3132
return $output;
3233
}
34+
35+
public function __toString(): string
36+
{
37+
$output = '';
38+
39+
foreach ($this as $partial) {
40+
$output .= " - {$partial}".PHP_EOL;
41+
}
42+
43+
return $output;
44+
}
3345
}

0 commit comments

Comments
 (0)