Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche authored and actions-user committed Jul 26, 2021
1 parent 81ff68b commit d40b8ad
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 36 deletions.
1 change: 0 additions & 1 deletion src/Concerns/RequestableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Spatie\LaravelData\Concerns;

use Illuminate\Http\Request;
use Spatie\LaravelData\Actions\ResolveDataObjectFromArrayAction;

trait RequestableData
{
Expand Down
1 change: 0 additions & 1 deletion src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Spatie\LaravelData\Concerns\RequestableData;
use Spatie\LaravelData\Concerns\ResponsableData;
use Spatie\LaravelData\Support\EloquentCasts\DataEloquentCast;
use Spatie\LaravelData\Support\EmptyDataResolver;
use Spatie\LaravelData\Transformers\DataTransformer;

/**
Expand Down
5 changes: 2 additions & 3 deletions src/DataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Spatie\LaravelData;

use Illuminate\Contracts\Database\Eloquent\Castable as EloquentCastable;
use ArrayAccess;
use ArrayIterator;
use Closure;
use Countable;
use Exception;
use Illuminate\Contracts\Database\Eloquent\Castable as EloquentCastable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Responsable;
Expand All @@ -19,7 +19,6 @@
use Spatie\LaravelData\Concerns\IncludeableData;
use Spatie\LaravelData\Concerns\ResponsableData;
use Spatie\LaravelData\Support\EloquentCasts\DataCollectionEloquentCast;
use Spatie\LaravelData\Support\EloquentCasts\DataEloquentCast;
use Spatie\LaravelData\Transformers\DataCollectionTransformer;

class DataCollection implements Responsable, Arrayable, Jsonable, IteratorAggregate, Countable, ArrayAccess, EloquentCastable
Expand Down Expand Up @@ -127,7 +126,7 @@ public function offsetUnset($offset): void

public static function castUsing(array $arguments)
{
if(count($arguments) !== 1){
if (count($arguments) !== 1) {
throw new Exception('Data collection eloquent cast should have its data class as an argument');
}

Expand Down
15 changes: 7 additions & 8 deletions src/Support/EloquentCasts/DataCollectionEloquentCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ class DataCollectionEloquentCast implements CastsAttributes
public function __construct(
/** @var class-string<\Spatie\LaravelData\Data> */
protected string $dataClass
)
{
) {
}

public function get($model, string $key, $value, array $attributes): ?DataCollection
{
if($value === null){
if ($value === null) {
return null;
}

$data = json_decode($value, true, flags: JSON_THROW_ON_ERROR);

$data = array_map(
fn(array $item) => ($this->dataClass)::createFromArray($item),
fn (array $item) => ($this->dataClass)::createFromArray($item),
$data
);

Expand All @@ -34,20 +33,20 @@ public function get($model, string $key, $value, array $attributes): ?DataCollec

public function set($model, string $key, $value, array $attributes): ?string
{
if($value === null){
if ($value === null) {
return null;
}

if($value instanceof DataCollection){
if ($value instanceof DataCollection) {
$value = $value->all();
}

if(!is_array($value)){
if (! is_array($value)) {
throw new Exception("Value given to a data collection eloquent cast should be a DataCollection or array");
}

$data = array_map(
fn(array|Data $item) => is_array($item)
fn (array | Data $item) => is_array($item)
? ($this->dataClass)::createFromArray($item)
: $item,
$value
Expand Down
11 changes: 5 additions & 6 deletions src/Support/EloquentCasts/DataEloquentCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ class DataEloquentCast implements CastsAttributes
public function __construct(
/** @var class-string<\Spatie\LaravelData\Data> */
protected string $dataClass
)
{
) {
}

public function get($model, string $key, $value, array $attributes): ?Data
{
if($value === null){
if ($value === null) {
return null;
}

Expand All @@ -31,15 +30,15 @@ public function get($model, string $key, $value, array $attributes): ?Data

public function set($model, string $key, $value, array $attributes): ?string
{
if($value === null){
if ($value === null) {
return null;
}

if(is_array($value)){
if (is_array($value)) {
$value = ($this->dataClass)::createFromArray($value);
}

if(!$value instanceof Data){
if (! $value instanceof Data) {
$className = $model::class;

throw new Exception("Attribute `{$key}` of model `{$className}` should be a Data object");
Expand Down
4 changes: 2 additions & 2 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function __construct(
/** @test */
public function it_can_include_a_nested_lazy_property()
{
$data = new class(Lazy::create(fn () => LazyData::create('Hello')), Lazy::create(fn () => LazyData::collection([ 'is', 'it', 'me', 'your', 'looking', 'for', ])),) extends Data {
$data = new class(Lazy::create(fn () => LazyData::create('Hello')), Lazy::create(fn () => LazyData::collection([ 'is', 'it', 'me', 'your', 'looking', 'for', ])), ) extends Data {
public function __construct(
public Lazy | LazyData $data,
/** @var \Spatie\LaravelData\Tests\Fakes\LazyData[] */
Expand Down Expand Up @@ -330,7 +330,7 @@ public function it_can_dynamically_include_data_based_upon_the_request()
/** @test */
public function it_can_get_the_data_object_without_transforming()
{
$data = new class($dataObject = new SimpleData('Test'), $dataCollection = SimpleData::collection([ new SimpleData('A'), new SimpleData('B'), ]), Lazy::create(fn () => new SimpleData('Lazy')), 'Test', $transformable = new DateTime('16 may 1994'),) extends Data {
$data = new class($dataObject = new SimpleData('Test'), $dataCollection = SimpleData::collection([ new SimpleData('A'), new SimpleData('B'), ]), Lazy::create(fn () => new SimpleData('Lazy')), 'Test', $transformable = new DateTime('16 may 1994'), ) extends Data {
public function __construct(
public SimpleData $data,
public DataCollection $dataCollection,
Expand Down
4 changes: 2 additions & 2 deletions tests/Fakes/DummyModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class DummyModel extends Model
{
protected $casts = [
'data' => SimpleData::class,
'data_collection' => DataCollection::class.':'.SimpleData::class
'data_collection' => DataCollection::class.':'.SimpleData::class,
];

public $timestamps = false;

public static function migrate()
{
Schema::create('dummy_models', function(Blueprint $blueprint){
Schema::create('dummy_models', function (Blueprint $blueprint) {
$blueprint->increments('id');

$blueprint->text('data')->nullable();
Expand Down
20 changes: 8 additions & 12 deletions tests/Support/EloquentCasts/DataEloquentCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

namespace Spatie\LaravelData\Tests\Support\EloquentCasts;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Spatie\LaravelData\Support\EloquentCasts\DataEloquentCast;
use Spatie\LaravelData\Tests\Fakes\DummyModel;
use Spatie\LaravelData\Tests\Fakes\SimpleData;
use Spatie\LaravelData\Tests\TestCase;
Expand All @@ -24,31 +20,31 @@ public function setUp() : void
public function it_can_save_a_data_object()
{
DummyModel::create([
'data' => new SimpleData('Test')
'data' => new SimpleData('Test'),
]);

$this->assertDatabaseHas(DummyModel::class, [
'data' => json_encode(['string' => 'Test'])
'data' => json_encode(['string' => 'Test']),
]);
}

/** @test */
public function it_can_save_a_data_object_as_an_array()
{
DummyModel::create([
'data' => ['string' => 'Test']
'data' => ['string' => 'Test'],
]);

$this->assertDatabaseHas(DummyModel::class, [
'data' => json_encode(['string' => 'Test'])
'data' => json_encode(['string' => 'Test']),
]);
}

/** @test */
public function it_can_load_a_data_object()
{
DB::table('dummy_models')->insert([
'data' => json_encode(['string' => 'Test'])
'data' => json_encode(['string' => 'Test']),
]);

/** @var \Spatie\LaravelData\Tests\Fakes\DummyModel $model */
Expand All @@ -64,19 +60,19 @@ public function it_can_load_a_data_object()
public function it_can_save_a_null_as_a_value()
{
DummyModel::create([
'data' => null
'data' => null,
]);

$this->assertDatabaseHas(DummyModel::class, [
'data' => null
'data' => null,
]);
}

/** @test */
public function it_can_load_null_as_a_value()
{
DB::table('dummy_models')->insert([
'data' => null
'data' => null,
]);

/** @var \Spatie\LaravelData\Tests\Fakes\DummyModel $model */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function it_can_covert_a_data_object_to_typescript()
{
$config = TypeScriptTransformerConfig::create();

$data = new class(null, 42, true, 'Hello world', 3.14, ['the', 'meaning', 'of', 'life'], Lazy::create(fn () => 'Lazy'), SimpleData::create('Simple data'), SimpleData::collection([]),) extends Data {
$data = new class(null, 42, true, 'Hello world', 3.14, ['the', 'meaning', 'of', 'life'], Lazy::create(fn () => 'Lazy'), SimpleData::create('Simple data'), SimpleData::collection([]), ) extends Data {
public function __construct(
public null | int $nullable,
public int $int,
Expand Down

0 comments on commit d40b8ad

Please sign in to comment.