Skip to content

Commit

Permalink
Remove resolved partial
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jan 19, 2024
1 parent d122f0a commit f82bd4c
Show file tree
Hide file tree
Showing 12 changed files with 438 additions and 522 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"test" : "./vendor/bin/pest --no-coverage",
"test-coverage" : "vendor/bin/pest --coverage-html coverage",
"format" : "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"benchmark" : "vendor/bin/phpbench run --report=default",
"benchmark" : "vendor/bin/phpbench run",
"benchmark-profiled" : "vendor/bin/phpbench "
},
"config" : {
Expand Down
12 changes: 6 additions & 6 deletions src/Resolvers/VisibleDataFieldsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function performExcept(

if ($nested = $exceptPartial->getNested()) {
try {
$fields[$nested]->addExceptResolvedPartial($exceptPartial->next());
$fields[$nested]->addExceptPartial($exceptPartial->next());
} catch (ErrorException $exception) {
$this->handleNonExistingNestedField($exception, PartialType::Except, $nested, $dataClass, $transformationContext);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ protected function performOnly(

if ($nested = $onlyPartial->getNested()) {
try {
$fields[$nested]->addOnlyResolvedPartial($onlyPartial->next());
$fields[$nested]->addOnlyPartial($onlyPartial->next());
$onlyFields[] = $nested;
} catch (ErrorException $exception) {
$this->handleNonExistingNestedField($exception, PartialType::Only, $nested, $dataClass, $transformationContext);
Expand Down Expand Up @@ -220,15 +220,15 @@ protected function resolveIncludedFields(

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

break;
}

if ($nested = $includedPartial->getNested()) {
try {
$fields[$nested]->addIncludedResolvedPartial($includedPartial->next());
$fields[$nested]->addIncludedPartial($includedPartial->next());
$includedFields[] = $nested;
} catch (ErrorException $exception) {
$this->handleNonExistingNestedField($exception, PartialType::Include, $nested, $dataClass, $transformationContext);
Expand Down Expand Up @@ -268,15 +268,15 @@ protected function resolveExcludedFields(
->all();

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

break;
}

if ($nested = $excludePartial->getNested()) {
try {
$fields[$nested]->addExcludedResolvedPartial($excludePartial->next());
$fields[$nested]->addExcludedPartial($excludePartial->next());
} catch (ErrorException $exception) {
$this->handleNonExistingNestedField($exception, PartialType::Exclude, $nested, $dataClass, $transformationContext);
}
Expand Down
129 changes: 118 additions & 11 deletions src/Support/Partials/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@

class Partial implements Stringable
{
protected readonly ResolvedPartial $resolvedPartial;
protected int $segmentCount;

protected bool $endsInAll;

/**
* @param array<int, PartialSegment> $segments
*/
public function __construct(
public array $segments,
public bool $permanent,
public ?Closure $condition,
public bool $permanent = false,
public ?Closure $condition = null,
public int $pointer = 0,
) {
$this->resolvedPartial = new ResolvedPartial($segments);
$this->segmentCount = count($segments);
$this->endsInAll = $this->segmentCount === 0
? false
: $this->segments[$this->segmentCount - 1] instanceof AllPartialSegment;
}

public static function create(
Expand Down Expand Up @@ -107,19 +113,120 @@ protected static function resolveSegmentsFromPath(string $path): array
return $segments;
}

public function resolve(BaseData|BaseDataCollectable $data): ?ResolvedPartial
public function isUndefined(): bool
{
if ($this->condition === null) {
return $this->resolvedPartial->reset();
return ! $this->endsInAll && $this->pointer >= $this->segmentCount;
}

public function isAll(): bool
{
return $this->endsInAll && $this->pointer >= $this->segmentCount - 1;
}

public function getNested(): ?string
{
$segment = $this->getCurrentSegment();

if ($segment === null) {
return null;
}

if (! $segment instanceof NestedPartialSegment) {
return null;
}

return $segment->field;
}

public function getFields(): ?array
{
if ($this->isUndefined()) {
return null;
}

$segment = $this->getCurrentSegment();

if ($segment === null) {
return null;
}

if (($this->condition)($data)) {
return $this->resolvedPartial->reset();
if (! $segment instanceof FieldsPartialSegment) {
return null;
}

return null;
return $segment->fields;
}

/** @return string[] */
public function toLaravel(): array
{
/** @var array<string> $segments */
$segments = [];

for ($i = $this->pointer; $i < $this->segmentCount; $i++) {
$segment = $this->segments[$i];

if ($segment instanceof AllPartialSegment) {
$segments[] = '*';

continue;
}

if ($segment instanceof NestedPartialSegment) {
$segments[] = $segment->field;

continue;
}

if ($segment instanceof FieldsPartialSegment) {
$segmentsAsString = count($segments) === 0
? ''
: implode('.', $segments).'.';

return array_map(
fn (string $field) => "{$segmentsAsString}{$field}",
$segment->fields
);
}
}

return [implode('.', $segments)];
}

public function next(): self
{
$this->pointer++;

return $this;
}

public function rollbackWhenRequired(): void
{
$this->pointer--;
}

public function reset(): self
{
$this->pointer = 0;

return $this;
}

protected function getCurrentSegment(): ?PartialSegment
{
return $this->segments[$this->pointer] ?? null;
}

public function isRequired(BaseData|BaseDataCollectable $data): bool
{
if ($this->condition === null) {
return true;
}

return ($this->condition)($data);
}


public function toArray(): array
{
return [
Expand All @@ -131,6 +238,6 @@ public function toArray(): array

public function __toString(): string
{
return implode('.', $this->segments);
return implode('.', $this->segments)." (current: {$this->pointer})";
}
}
14 changes: 13 additions & 1 deletion src/Support/Partials/PartialsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace Spatie\LaravelData\Support\Partials;

use SplObjectStorage;
use Stringable;

/**
* @extends SplObjectStorage<Partial, null>
*/
class PartialsCollection extends SplObjectStorage
class PartialsCollection extends SplObjectStorage implements Stringable
{
public static function create(Partial ...$partials): self
{
Expand All @@ -30,4 +31,15 @@ public function toArray(): array

return $output;
}

public function __toString(): string
{
$output = '';

foreach ($this as $partial) {
$output .= " - {$partial}".PHP_EOL;
}

return $output;
}
}
Loading

0 comments on commit f82bd4c

Please sign in to comment.