Skip to content

Commit

Permalink
Merge commit '516eec79268b07a8459e19bd6517866d480cee14' into feature-…
Browse files Browse the repository at this point in the history
…property-morphable

# Conflicts:
#	tests/CreationTest.php
#	tests/ValidationTest.php
  • Loading branch information
bentleyo committed Jan 28, 2025
2 parents cc76f20 + 516eec7 commit 635d813
Show file tree
Hide file tree
Showing 106 changed files with 3,251 additions and 899 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v2.2.0
uses: dependabot/fetch-metadata@v2.3.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: '8.4'
coverage: none

- name: Install composer dependencies
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.1, 8.2, 8.3]
php: [8.1, 8.2, 8.3, 8.4]
laravel: [10.*, 11.*]
stability: [prefer-lowest, prefer-stable]
include:
Expand All @@ -20,7 +20,8 @@ jobs:
exclude:
- php: 8.1
laravel: 11.*

- php: 8.4
laravel: 10.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ node_modules
.php-cs-fixer.cache
.phpbench
.DS_Store
/.phpunit.cache/
97 changes: 97 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,103 @@

All notable changes to `laravel-data` will be documented in this file.

## 4.13.0 - 2025-01-24

### What's Changed

* Auto lazy by @rubenvanassche in https://github.com/spatie/laravel-data/pull/831

**Full Changelog**: https://github.com/spatie/laravel-data/compare/4.12.0...4.13.0

## 4.12.0 - 2025-01-24

What a release! Probably to biggest minor release we've ever done!

Some cool highlights:

#### Disabling optional values

Optional values are great, but sometimes a `null` value is desirable from now on you can do the following:

```php
class SongData extends Data {
public function __construct(
public string $title,
public string $artist,
public Optional|null|string $album,
) {
}
}

SongData::factory()
->withoutOptionalValues()
->from(['title' => 'Never gonna give you up', 'artist' => 'Rick Astley']); // album will `null` instead of `Optional`


```
#### Injecting property values

It was already possible to inject a Laravel route parameter when creating a data object, we've now extended this functionality quite a bit and also allow injecting dependencies from the container and the authenticated user.

```php
class SongData extends Data {
#[FromAuthenticatedUser]
public UserData $user;
}


```
#### Merging manual rules

In the past when the validation rules of a property were manually defined, the automatic validation rules for that property were omitted. From now on, you can define manual validation rules and merge them with the automatically generated validation rules:

```php
```php
#[MergeValidationRules]
class SongData extends Data
{
public function __construct(
public string $title,
public string $artist,
) {
}

public static function rules(): array
{
return [
'title' => ['max:20'],
'artist' => ['max:20'],
];
}
}


```
#### New property mappers:

We now ship by default a `Uppercase` and `Lowercase` mapper for mapping property names.

### All changes:

* Fix GitHub action fail by @rust17 in https://github.com/spatie/laravel-data/pull/918
* Point to the right problem on ArgumentCountError exception by @nsvetozarevic in https://github.com/spatie/laravel-data/pull/884
* Fix an issue where anonymous classes in castables were serialized (#903) by @rubenvanassche in https://github.com/spatie/laravel-data/pull/923
* Add the ability to optionally merge automatically inferred rules with manual rules by @CWAscend in https://github.com/spatie/laravel-data/pull/848
* Implement enum json serialization by @dont-know-php in https://github.com/spatie/laravel-data/pull/896
* Use comments instead of docblocks in configuration file. by @edwinvdpol in https://github.com/spatie/laravel-data/pull/904
* Casting DateTimeInterface: Truncate nanoseconds to microseconds (first 6 digits) / with Tests by @yob-yob in https://github.com/spatie/laravel-data/pull/908
* Use container to call `Data::authorize()` to allow for dependencies by @cosmastech in https://github.com/spatie/laravel-data/pull/910
* Improve type for CreationContextFactory::alwaysValidate by @sanfair in https://github.com/spatie/laravel-data/pull/925
* Removed comma character from Data Rule stub by @andrey-helldar in https://github.com/spatie/laravel-data/pull/926
* Use BaseData contract i/o Data concrete in CollectionAnnotation by @riesjart in https://github.com/spatie/laravel-data/pull/928
* New mappers added: `LowerCaseMapper` and `UpperCaseMapper` by @andrey-helldar in https://github.com/spatie/laravel-data/pull/927
* Allow disabling default Optional values in CreationContext by @ragulka in https://github.com/spatie/laravel-data/pull/931
* Fix introduction.md by @pikant in https://github.com/spatie/laravel-data/pull/937
* General code health improvements by @xHeaven in https://github.com/spatie/laravel-data/pull/920
* Filling properties from current user by @c-v-c-v in https://github.com/spatie/laravel-data/pull/879

**Full Changelog**: https://github.com/spatie/laravel-data/compare/4.11.1...4.12.0

## 4.11.1 - 2024-10-23

- Fix an issue where the cache structures command did not work if the directory did not exist (#892)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"fakerphp/faker": "^1.14",
"friendsofphp/php-cs-fixer": "^3.0",
"inertiajs/inertia-laravel": "^1.2",
"larastan/larastan": "^2.7",
"livewire/livewire": "^3.0",
"mockery/mockery": "^1.6",
"nesbot/carbon": "^2.63",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^8.0|^9.0",
"pestphp/pest": "^2.31",
"pestphp/pest-plugin-laravel": "^2.0",
Expand Down
50 changes: 26 additions & 24 deletions config/data.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
<?php

return [
/**
/*
* The package will use this format when working with dates. If this option
* is an array, it will try to convert from the first format that works,
* and will serialize dates using the first format from the array.
*/
'date_format' => DATE_ATOM,

/**
/*
* When transforming or casting dates, the following timezone will be used to
* convert the date to the correct timezone. If set to null no timezone will
* be passed.
*/
'date_timezone' => null,

/**
/*
* It is possible to enable certain features of the package, these would otherwise
* be breaking changes, and thus they are disabled by default. In the next major
* version of the package, these features will be enabled by default.
*/
'features' => [
'cast_and_transform_iterables' => false,

/**
/*
* When trying to set a computed property value, the package will throw an exception.
* You can disable this behaviour by setting this option to true, which will then just
* ignore the value being passed into the computed property and recalculate it.
*/
'ignore_exception_when_trying_to_set_computed_property_value' => false,
],

/**
/*
* Global transformers will take complex types and transform them into simple
* types.
*/
Expand All @@ -41,7 +41,7 @@
BackedEnum::class => Spatie\LaravelData\Transformers\EnumTransformer::class,
],

/**
/*
* Global casts will cast values into complex types when creating a data
* object from simple types.
*/
Expand All @@ -51,7 +51,7 @@
// Enumerable::class => Spatie\LaravelData\Casts\EnumerableCast::class,
],

/**
/*
* Rule inferrers can be configured here. They will automatically add
* validation rules to properties of a data object based upon
* the type of the property.
Expand All @@ -64,7 +64,7 @@
Spatie\LaravelData\RuleInferrers\AttributesRuleInferrer::class,
],

/**
/*
* Normalizers return an array representation of the payload, or null if
* it cannot normalize the payload. The normalizers below are used for
* every data object, unless overridden in a specific data object class.
Expand All @@ -78,22 +78,22 @@
Spatie\LaravelData\Normalizers\JsonNormalizer::class,
],

/**
/*
* Data objects can be wrapped into a key like 'data' when used as a resource,
* this key can be set globally here for all data objects. You can pass in
* `null` if you want to disable wrapping.
*/
'wrap' => null,

/**
/*
* Adds a specific caster to the Symphony VarDumper component which hides
* some properties from data objects and collections when being dumped
* by `dump` or `dd`. Can be 'enabled', 'disabled' or 'development'
* which will only enable the caster locally.
*/
'var_dumper_caster_mode' => 'development',

/**
/*
* It is possible to skip the PHP reflection analysis of data objects
* when running in production. This will speed up the package. You
* can configure where data objects are stored and which cache
Expand All @@ -119,14 +119,14 @@
],
],

/**
/*
* A data object can be validated when created using a factory or when calling the from
* method. By default, only when a request is passed the data is being validated. This
* behaviour can be changed to always validate or to completely disable validation.
*/
'validation_strategy' => \Spatie\LaravelData\Support\Creation\ValidationStrategy::OnlyRequests->value,

/**
/*
* A data object can map the names of its properties when transforming (output) or when
* creating (input). By default, the package will not map any names. You can set a
* global strategy here, or override it on a specific data object.
Expand All @@ -136,46 +136,48 @@
'output' => null,
],

/**
/*
* When using an invalid include, exclude, only or except partial, the package will
* throw an exception. You can disable this behaviour by setting this option to true.
*/
'ignore_invalid_partials' => false,

/**
/*
* When transforming a nested chain of data objects, the package can end up in an infinite
* loop when including a recursive relationship. The max transformation depth can be
* set as a safety measure to prevent this from happening. When set to null, the
* package will not enforce a maximum depth.
*/
'max_transformation_depth' => null,

/**
/*
* When the maximum transformation depth is reached, the package will throw an exception.
* You can disable this behaviour by setting this option to true which will return an
* empty array.
*/
'throw_when_max_transformation_depth_reached' => true,

/**
* When using the `make:data` command, the package will use these settings to generate
* the data classes. You can override these settings by passing options to the command.
*/
/*
* When using the `make:data` command, the package will use these settings to generate
* the data classes. You can override these settings by passing options to the command.
*/
'commands' => [
/**

/*
* Provides default configuration for the `make:data` command. These settings can be overridden with options
* passed directly to the `make:data` command for generating single Data classes, or if not set they will
* automatically fall back to these defaults. See `php artisan make:data --help` for more information
*/
'make' => [
/**

/*
* The default namespace for generated Data classes. This exists under the application's root namespace,
* so the default 'Data` will end up as '\App\Data', and generated Data classes will be placed in the
* app/Data/ folder. Data classes can live anywhere, but this is where `make:data` will put them.
*/
'namespace' => 'Data',

/**
/*
* This suffix will be appended to all data classes generated by make:data, so that they are less likely
* to conflict with other related classes, controllers or models with a similar name without resorting
* to adding an alias for the Data object. Set to a blank string (not null) to disable.
Expand All @@ -184,7 +186,7 @@
],
],

/**
/*
* When using Livewire, the package allows you to enable or disable the synths
* these synths will automatically handle the data objects and their
* properties when used in a Livewire component.
Expand Down
60 changes: 60 additions & 0 deletions docs/advanced-usage/available-property-mappers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: Available property mappers
weight: 19
---

In previous sections we've already seen how
to [create](/docs/laravel-data/v4/as-a-data-transfer-object/mapping-property-names) data objects where the keys of the
payload differ from the property names of the data object. It is also possible
to [transform](/docs/laravel-data/v4/as-a-resource/mapping-property-names) data objects to an
array/json/... where the keys of the payload differ from the property names of the data object.

These mappings can be set manually put the package also provide a set of mappers that can be used to automatically map
property names:

```php
class ContractData extends Data
{
public function __construct(
#[MapName(CamelCaseMapper::class)]
public string $name,
#[MapName(SnakeCaseMapper::class)]
public string $recordCompany,
#[MapName(new ProvidedNameMapper('country field'))]
public string $country,
#[MapName(StudlyCaseMapper::class)]
public string $cityName,
#[MapName(LowerCaseMapper::class)]
public string $addressLine1,
#[MapName(UpperCaseMapper::class)]
public string $addressLine2,
) {
}
}
```

Creating the data object can now be done as such:

```php
ContractData::from([
'name' => 'Rick Astley',
'record_company' => 'RCA Records',
'country field' => 'Belgium',
'CityName' => 'Antwerp',
'addressline1' => 'some address line 1',
'ADDRESSLINE2' => 'some address line 2',
]);
```

When transforming such a data object the payload will look like this:

```json
{
"name" : "Rick Astley",
"record_company" : "RCA Records",
"country field" : "Belgium",
"CityName" : "Antwerp",
"addressline1" : "some address line 1",
"ADDRESSLINE2" : "some address line 2"
}
```
Loading

0 comments on commit 635d813

Please sign in to comment.