Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Mar 13, 2024
1 parent 0881402 commit 3c9781f
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion config/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
* You can disable this behaviour by setting this option to true which will return an
* empty array.
*/
'fail_when_max_transformation_depth_reached' => true,
'throw_when_max_transformation_depth_reached' => true,

/**
* When using the `make:data` command, the package will use these settings to generate
Expand Down
4 changes: 2 additions & 2 deletions docs/as-a-resource/transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Setting the transformation depth to `null` will disable the transformation depth
It is also possible if a `MaxTransformationDepthReached` exception should be thrown or an empty array should be returned:

```php
'fail_when_max_transformation_depth_reached' => true,
'throw_when_max_transformation_depth_reached' => true,
```

It is also possible to set the transformation depth on a specific transformation by using a `TransformationContextFactory`:
Expand All @@ -190,6 +190,6 @@ By default, an exception will be thrown when the maximum transformation depth is

```php
ArtistData::from($artist)->transform(
TransformationContextFactory::create()->maxDepth(20, fail: false)
TransformationContextFactory::create()->maxDepth(20, throw: false)
);
```
2 changes: 1 addition & 1 deletion src/Concerns/TransformableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function transform(
$transformationContext instanceof TransformationContextFactory => $transformationContext->get($this),
$transformationContext === null => new TransformationContext(
maxDepth: config('data.max_transformation_depth'),
failWhenMaxDepthReached: config('data.fail_when_max_transformation_depth_reached')
throwWhenMaxDepthReached: config('data.throw_when_max_transformation_depth_reached')
)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Spatie\LaravelData\Exceptions\MaxTransformationDepthReached;
use Spatie\LaravelData\Support\Transformation\TransformationContext;

trait ChecksTransformationDepths
trait ChecksTransformationDepth
{
public function hasReachedMaxTransformationDepth(TransformationContext $context): bool
{
Expand All @@ -14,7 +14,7 @@ public function hasReachedMaxTransformationDepth(TransformationContext $context)

public function handleMaxDepthReached(TransformationContext $context): array
{
if ($context->failWhenMaxDepthReached) {
if ($context->throwWhenMaxDepthReached) {
throw MaxTransformationDepthReached::create($context->maxDepth);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Resolvers/TransformedDataCollectableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Spatie\LaravelData\CursorPaginatedDataCollection;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\PaginatedDataCollection;
use Spatie\LaravelData\Resolvers\Concerns\ChecksTransformationDepths;
use Spatie\LaravelData\Resolvers\Concerns\ChecksTransformationDepth;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Support\Transformation\TransformationContext;
use Spatie\LaravelData\Support\Wrapping\Wrap;
Expand All @@ -23,7 +23,7 @@

class TransformedDataCollectableResolver
{
use ChecksTransformationDepths;
use ChecksTransformationDepth;

public function __construct(
protected DataConfig $dataConfig
Expand Down
4 changes: 2 additions & 2 deletions src/Resolvers/TransformedDataResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Spatie\LaravelData\Contracts\WrappableData;
use Spatie\LaravelData\Lazy;
use Spatie\LaravelData\Optional;
use Spatie\LaravelData\Resolvers\Concerns\ChecksTransformationDepths;
use Spatie\LaravelData\Resolvers\Concerns\ChecksTransformationDepth;
use Spatie\LaravelData\Support\DataClass;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Support\DataContainer;
Expand All @@ -21,7 +21,7 @@

class TransformedDataResolver
{
use ChecksTransformationDepths;
use ChecksTransformationDepth;

public function __construct(
protected DataConfig $dataConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/Resolvers/VisibleDataFieldsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function execute(
$transformationContext->transformers,
depth: $transformationContext->depth + 1,
maxDepth: $transformationContext->maxDepth,
failWhenMaxDepthReached: $transformationContext->failWhenMaxDepthReached,
throwWhenMaxDepthReached: $transformationContext->throwWhenMaxDepthReached,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Transformation/TransformationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
public ?PartialsCollection $exceptPartials = null,
public int $depth = 0,
public ?int $maxDepth = null,
public bool $failWhenMaxDepthReached = true,
public bool $throwWhenMaxDepthReached = true,
) {
}

Expand Down
14 changes: 9 additions & 5 deletions src/Support/Transformation/TransformationContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class TransformationContextFactory
{
use ForwardsToPartialsDefinition;

public ?int $maxDepth;

public bool $throwWhenMaxDepthReached;

public static function create(): self
{
return new self();
Expand All @@ -27,9 +31,9 @@ protected function __construct(
public ?PartialsCollection $excludePartials = null,
public ?PartialsCollection $onlyPartials = null,
public ?PartialsCollection $exceptPartials = null,
public ?int $maxDepth = null,
public bool $failWhenMaxDepthReached = true,
) {
$this->maxDepth = config('data.max_transformation_depth', null);
$this->throwWhenMaxDepthReached = config('data.throw_when_max_transformation_depth_reached', true);
}

public function get(
Expand Down Expand Up @@ -94,7 +98,7 @@ public function get(
$exceptPartials,
depth: 0,
maxDepth: $this->maxDepth,
failWhenMaxDepthReached: $this->failWhenMaxDepthReached,
throwWhenMaxDepthReached: $this->throwWhenMaxDepthReached,
);
}

Expand Down Expand Up @@ -160,10 +164,10 @@ public function withTransformer(string $transformable, Transformer|string $trans
return $this;
}

public function maxDepth(?int $maxDepth, bool $fail = true): static
public function maxDepth(?int $maxDepth, bool $throw = true): static
{
$this->maxDepth = $maxDepth;
$this->failWhenMaxDepthReached = $fail;
$this->throwWhenMaxDepthReached = $throw;

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
expect($context->transformers)->toBeNull();
expect($context->depth)->toBe(0);
expect($context->maxDepth)->toBeNull();
expect($context->failWhenMaxDepthReached)->toBeTrue();
expect($context->throwWhenMaxDepthReached)->toBeTrue();
});

it('can disable value transformation', function () {
Expand Down Expand Up @@ -93,15 +93,15 @@

expect($context->maxDepth)->toBe(4);
expect($context->depth)->toBe(0);
expect($context->failWhenMaxDepthReached)->toBeTrue();
expect($context->throwWhenMaxDepthReached)->toBeTrue();
});

it('can set a max transformation depth without failing', function () {
$context = TransformationContextFactory::create()
->maxDepth(4, fail: false)
->maxDepth(4, throw: false)
->get(SimpleData::from('Hello World'));

expect($context->maxDepth)->toBe(4);
expect($context->depth)->toBe(0);
expect($context->failWhenMaxDepthReached)->toBeFalse();
expect($context->throwWhenMaxDepthReached)->toBeFalse();
});
4 changes: 2 additions & 2 deletions tests/TransformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public static function fromOther(stdClass $a): self
))->toThrow(MaxTransformationDepthReached::class);

expect(TestMaxDataObjectTransformationDepthB::fromOther($a)->transform(
TransformationContextFactory::create()->maxDepth(4, fail: false)
TransformationContextFactory::create()->maxDepth(4, throw: false)
))->toBe([
'dataA' => [
'dataB' => [
Expand Down Expand Up @@ -497,7 +497,7 @@ public static function fromOther(stdClass $a): self
))->toThrow(MaxTransformationDepthReached::class);

expect(TestMaxDatCollectionTransformationDepthB::fromOther($a)->transform(
TransformationContextFactory::create()->maxDepth(4, fail: false)
TransformationContextFactory::create()->maxDepth(4, throw: false)
))->toBe([
'cb' => [
[
Expand Down

0 comments on commit 3c9781f

Please sign in to comment.