Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jan 19, 2024
1 parent 253d8ca commit b26206a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ All notable changes to `laravel-data` will be documented in this file.
- Added contexts to the creation and transformation process
- Allow creating a data object or collection using a factory
- Speed up the process of creating and transforming data objects
- Rewritten docs

**Some more "internal" changes**

- Restructured tests for the future we have ahead
- Benchmarks added to make data even faster

## 3.11.0 - 2023-12-21

Expand Down
4 changes: 1 addition & 3 deletions src/Casts/Castable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
interface Castable
{
/**
* Get the name of the caster class to use when casting to this cast target.
*
* @param array $arguments
* @param array $arguments
*/
public static function dataCastUsing(array $arguments): Cast;
}
32 changes: 22 additions & 10 deletions src/Support/DataMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Spatie\LaravelData\Enums\CustomCreationMethodType;

/**
* @property Collection<DataParameter> $parameters
* @property Collection<DataParameter|DataProperty> $parameters
*/
class DataMethod
{
Expand All @@ -22,20 +22,31 @@ public function __construct(

public function accepts(mixed ...$input): bool
{
/** @var Collection<array-key, \Spatie\LaravelData\Support\DataParameter|\Spatie\LaravelData\Support\DataProperty> $parameters */
$parameters = array_is_list($input)
? $this->parameters
: $this->parameters->mapWithKeys(fn (DataParameter|DataProperty $parameter) => [$parameter->name => $parameter]);
$requiredParameterCount = 0;

$parameters = $parameters->reject(
fn (DataParameter|DataProperty $parameter) => $parameter instanceof DataParameter && $parameter->type->type->isCreationContext()
);
foreach ($this->parameters as $parameter) {
if ($parameter->type->type->isCreationContext()) {
continue;
}

$requiredParameterCount++;
}

if (count($input) > $parameters->count()) {
if (count($input) > $requiredParameterCount) {
return false;
}

foreach ($parameters as $index => $parameter) {
$useNameAsIndex = ! array_is_list($input);

foreach ($this->parameters as $index => $parameter) {
if ($parameter->type->type->isCreationContext()) {
continue;
}

if ($useNameAsIndex) {
$index = $parameter->name;
}

$parameterProvided = array_key_exists($index, $input);

if (! $parameterProvided && $parameter->hasDefaultValue === false) {
Expand All @@ -59,6 +70,7 @@ public function accepts(mixed ...$input): bool
) {
return false;
}

}

return true;
Expand Down

0 comments on commit b26206a

Please sign in to comment.