Skip to content

Commit

Permalink
Move some interfaces aaround
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Sep 10, 2024
1 parent 1732519 commit f87b25a
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/Concerns/TransformableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Spatie\LaravelData\Contracts\BaseData as BaseDataContract;
use Spatie\LaravelData\Contracts\BaseDataCollectable as BaseDataCollectableContract;
use Spatie\LaravelData\Contracts\ContextableData as ContextableDataContract;
use Spatie\LaravelData\Contracts\IncludeableData as IncludeableDataContract;
use Spatie\LaravelData\Support\DataContainer;
use Spatie\LaravelData\Support\EloquentCasts\DataEloquentCast;
Expand All @@ -31,7 +32,7 @@ public function transform(
default => throw new Exception('Cannot transform data object')
};

if ($this instanceof IncludeableDataContract) {
if ($this instanceof IncludeableDataContract && $this instanceof ContextableDataContract) {
$transformationContext->mergePartialsFromDataContext($this);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/IncludeableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Closure;

interface IncludeableData extends ContextableData
interface IncludeableData
{
public function include(string ...$includes): object;

Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/ResponsableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Contracts\Support\Responsable;

interface ResponsableData extends TransformableData, Responsable
interface ResponsableData extends Responsable
{
/**
* @param \Illuminate\Http\Request $request
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/TransformableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Spatie\LaravelData\Support\Transformation\TransformationContext;
use Spatie\LaravelData\Support\Transformation\TransformationContextFactory;

interface TransformableData extends JsonSerializable, Jsonable, Arrayable, EloquentCastable, ContextableData
interface TransformableData extends JsonSerializable, Jsonable, Arrayable, EloquentCastable
{
public function transform(
null|TransformationContextFactory|TransformationContext $transformationContext = null,
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/WrappableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Spatie\LaravelData\Support\Wrapping\Wrap;

interface WrappableData extends ContextableData
interface WrappableData
{
public function withoutWrapping();

Expand Down
3 changes: 2 additions & 1 deletion src/CursorPaginatedDataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Spatie\LaravelData\Concerns\TransformableData;
use Spatie\LaravelData\Concerns\WrappableData;
use Spatie\LaravelData\Contracts\BaseDataCollectable as BaseDataCollectableContract;
use Spatie\LaravelData\Contracts\ContextableData as ContextableDataContract;
use Spatie\LaravelData\Contracts\IncludeableData as IncludeableDataContract;
use Spatie\LaravelData\Contracts\ResponsableData as ResponsableDataContract;
use Spatie\LaravelData\Contracts\TransformableData as TransformableDataContract;
Expand All @@ -28,7 +29,7 @@
*
* @implements IteratorAggregate<TKey, TValue>
*/
class CursorPaginatedDataCollection implements Responsable, BaseDataCollectableContract, TransformableDataContract, ResponsableDataContract, IncludeableDataContract, WrappableDataContract, IteratorAggregate, Countable
class CursorPaginatedDataCollection implements Responsable, BaseDataCollectableContract, TransformableDataContract, ContextableDataContract, ResponsableDataContract, IncludeableDataContract, WrappableDataContract, IteratorAggregate, Countable
{
use ResponsableData;
use IncludeableData;
Expand Down
3 changes: 2 additions & 1 deletion src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
use Spatie\LaravelData\Concerns\WrappableData;
use Spatie\LaravelData\Contracts\AppendableData as AppendableDataContract;
use Spatie\LaravelData\Contracts\BaseData as BaseDataContract;
use Spatie\LaravelData\Contracts\ContextableData as ContextableDataContract;
use Spatie\LaravelData\Contracts\EmptyData as EmptyDataContract;
use Spatie\LaravelData\Contracts\IncludeableData as IncludeableDataContract;
use Spatie\LaravelData\Contracts\ResponsableData as ResponsableDataContract;
use Spatie\LaravelData\Contracts\TransformableData as TransformableDataContract;
use Spatie\LaravelData\Contracts\ValidateableData as ValidateableDataContract;
use Spatie\LaravelData\Contracts\WrappableData as WrappableDataContract;

abstract class Data implements Responsable, AppendableDataContract, BaseDataContract, TransformableDataContract, IncludeableDataContract, ResponsableDataContract, ValidateableDataContract, WrappableDataContract, EmptyDataContract
abstract class Data implements Responsable, AppendableDataContract, BaseDataContract, TransformableDataContract, ContextableDataContract, IncludeableDataContract, ResponsableDataContract, ValidateableDataContract, WrappableDataContract, EmptyDataContract
{
use ResponsableData;
use IncludeableData;
Expand Down
7 changes: 5 additions & 2 deletions src/DataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Spatie\LaravelData\Concerns\WrappableData;
use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Contracts\BaseDataCollectable as BaseDataCollectableContract;
use Spatie\LaravelData\Contracts\ContextableData as ContextableDataContract;
use Spatie\LaravelData\Contracts\IncludeableData as IncludeableDataContract;
use Spatie\LaravelData\Contracts\ResponsableData as ResponsableDataContract;
use Spatie\LaravelData\Contracts\TransformableData as TransformableDataContract;
Expand All @@ -32,7 +33,7 @@
* @implements \ArrayAccess<TKey, TValue>
* @implements IteratorAggregate<TKey, TValue>
*/
class DataCollection implements Responsable, BaseDataCollectableContract, TransformableDataContract, ResponsableDataContract, IncludeableDataContract, WrappableDataContract, IteratorAggregate, Countable, ArrayAccess
class DataCollection implements Responsable, BaseDataCollectableContract, TransformableDataContract, ResponsableDataContract, IncludeableDataContract, WrappableDataContract, ContextableDataContract, IteratorAggregate, Countable, ArrayAccess
{
/** @use \Spatie\LaravelData\Concerns\BaseDataCollectable<TKey, TValue> */
use BaseDataCollectable;
Expand Down Expand Up @@ -112,7 +113,9 @@ public function offsetGet($offset): mixed

$data = $this->items->offsetGet($offset);

if ($data instanceof IncludeableDataContract) {
if ($data instanceof IncludeableDataContract
&& $data instanceof ContextableDataContract
) {
$data->getDataContext()->mergePartials($this->getDataContext());
}

Expand Down
3 changes: 2 additions & 1 deletion src/PaginatedDataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Spatie\LaravelData\Concerns\TransformableData;
use Spatie\LaravelData\Concerns\WrappableData;
use Spatie\LaravelData\Contracts\BaseDataCollectable as BaseDataCollectableContract;
use Spatie\LaravelData\Contracts\ContextableData as ContextableDataContract;
use Spatie\LaravelData\Contracts\IncludeableData as IncludeableDataContract;
use Spatie\LaravelData\Contracts\ResponsableData as ResponsableDataContract;
use Spatie\LaravelData\Contracts\TransformableData as TransformableDataContract;
Expand All @@ -28,7 +29,7 @@
*
* @implements IteratorAggregate<TKey, TValue>
*/
class PaginatedDataCollection implements Responsable, BaseDataCollectableContract, TransformableDataContract, ResponsableDataContract, IncludeableDataContract, WrappableDataContract, IteratorAggregate, Countable
class PaginatedDataCollection implements Responsable, BaseDataCollectableContract, TransformableDataContract, ContextableDataContract, ResponsableDataContract, IncludeableDataContract, WrappableDataContract, IteratorAggregate, Countable
{
use ResponsableData;
use IncludeableData;
Expand Down
6 changes: 5 additions & 1 deletion src/Resolvers/TransformedDataCollectableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Enumerable;
use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Contracts\ContextableData as ContextableDataContract;
use Spatie\LaravelData\Contracts\TransformableData;
use Spatie\LaravelData\Contracts\WrappableData;
use Spatie\LaravelData\CursorPaginatedDataCollection;
Expand Down Expand Up @@ -170,7 +171,10 @@ protected function transformationClosure(
return $data;
}

if ($nestedContext->transformValues === false && $nestedContext->hasPartials()) {
if ($nestedContext->transformValues === false
&& $nestedContext->hasPartials()
&& $data instanceof ContextableDataContract
) {
$data->getDataContext()->mergeTransformationContext($nestedContext);

return $data;
Expand Down
5 changes: 2 additions & 3 deletions src/Support/Partials/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use Closure;
use Laravel\SerializableClosure\SerializableClosure;
use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Contracts\BaseDataCollectable;
use Spatie\LaravelData\Contracts\IncludeableData;
use Spatie\LaravelData\Support\Partials\Segments\AllPartialSegment;
use Spatie\LaravelData\Support\Partials\Segments\FieldsPartialSegment;
use Spatie\LaravelData\Support\Partials\Segments\NestedPartialSegment;
Expand Down Expand Up @@ -218,7 +217,7 @@ protected function getCurrentSegment(): ?PartialSegment
return $this->segments[$this->pointer] ?? null;
}

public function isRequired(BaseData|BaseDataCollectable $data): bool
public function isRequired(IncludeableData $data): bool
{
if ($this->condition === null) {
return true;
Expand Down
5 changes: 2 additions & 3 deletions src/Support/Transformation/DataContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Spatie\LaravelData\Support\Transformation;

use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Contracts\BaseDataCollectable;
use Spatie\LaravelData\Contracts\IncludeableData;
use Spatie\LaravelData\Support\DataContainer;
use Spatie\LaravelData\Support\Partials\PartialsCollection;
use Spatie\LaravelData\Support\Wrapping\Wrap;
Expand Down Expand Up @@ -105,7 +104,7 @@ public function mergeTransformationContext(
}

public function getRequiredPartialsAndRemoveTemporaryOnes(
BaseData|BaseDataCollectable $data,
IncludeableData $data,
PartialsCollection $partials,
): PartialsCollection {
$requiredPartials = new PartialsCollection();
Expand Down
8 changes: 2 additions & 6 deletions src/Support/Transformation/TransformationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Spatie\LaravelData\Support\Transformation;

use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Contracts\BaseDataCollectable;
use Spatie\LaravelData\Contracts\ContextableData;
use Spatie\LaravelData\Contracts\IncludeableData;
use Spatie\LaravelData\Support\Partials\Partial;
use Spatie\LaravelData\Support\Partials\PartialsCollection;
Expand Down Expand Up @@ -144,11 +143,8 @@ public function rollBackPartialsWhenRequired(): void
}
}

/**
* @param IncludeableData&(BaseData|BaseDataCollectable) $data
*/
public function mergePartialsFromDataContext(
IncludeableData $data
IncludeableData&ContextableData $data
): self {
$dataContext = $data->getDataContext();

Expand Down
5 changes: 2 additions & 3 deletions src/Support/Transformation/TransformationContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Spatie\LaravelData\Support\Transformation;

use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Contracts\BaseDataCollectable;
use Spatie\LaravelData\Contracts\IncludeableData;
use Spatie\LaravelData\Support\Partials\ForwardsToPartialsDefinition;
use Spatie\LaravelData\Support\Partials\PartialsCollection;
use Spatie\LaravelData\Support\Wrapping\WrapExecutionType;
Expand Down Expand Up @@ -37,7 +36,7 @@ protected function __construct(
}

public function get(
BaseData|BaseDataCollectable $data,
IncludeableData $data,
): TransformationContext {
$includePartials = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/Support/DataReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public function none()
Spatie\LaravelData\Contracts\ResponsableData::class,
Spatie\LaravelData\Contracts\IncludeableData::class,
Spatie\LaravelData\Contracts\WrappableData::class,
Spatie\LaravelData\Contracts\ContextableData::class,
IteratorAggregate::class,
Countable::class,
ArrayAccess::class,
Spatie\LaravelData\Contracts\ContextableData::class,
Illuminate\Contracts\Database\Eloquent\Castable::class,
Illuminate\Contracts\Support\Arrayable::class,
Illuminate\Contracts\Support\Jsonable::class,
Expand Down

0 comments on commit f87b25a

Please sign in to comment.