Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce FromHeaderParameter Attribute #944

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Introduce FromHeaderParameter Attribute: Map Header to DTO Prop
abdallah-zaghloul committed Feb 9, 2025
commit b21248a85607ac270bc8117aa14f169e996e6307
46 changes: 46 additions & 0 deletions src/Attributes/FromHeaderParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Spatie\LaravelData\Attributes;

use Attribute;
use Spatie\LaravelData\Attributes\InjectsPropertyValue;
use Illuminate\Http\Request;
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Support\DataProperty;
use Spatie\LaravelData\Support\Skipped;

#[Attribute(Attribute::TARGET_PROPERTY)]
class FromHeaderParameter implements InjectsPropertyValue
{
public function __construct(
public string $headerParameter,
public bool $replaceWhenPresentInPayload = true,
/** @deprecated */
public bool $replaceWhenPresentInBody = true
) {
}

public function resolve(
DataProperty $dataProperty,
mixed $payload,
array $properties,
CreationContext $creationContext
): mixed {
if (! $payload instanceof Request) {
return Skipped::create();
}

$parameter = $payload->header($this->headerParameter);

if ($parameter === null) {
return Skipped::create();
}

return $parameter;
}

public function shouldBeReplacedWhenPresentInPayload(): bool
{
return $this->replaceWhenPresentInPayload && $this->replaceWhenPresentInBody;
}
}
43 changes: 43 additions & 0 deletions src/Attributes/FromHeaderParameterProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Spatie\LaravelData\Attributes;

use Attribute;
use Spatie\LaravelData\Attributes\Concerns\ResolvesPropertyForInjectedValue;
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Support\DataProperty;

#[Attribute(Attribute::TARGET_PROPERTY)]
class FromHeaderParameterProperty extends FromHeaderParameter
{
use ResolvesPropertyForInjectedValue;

public function __construct(
string $headerParameter,
public ?string $property = null,
bool $replaceWhenPresentInPayload = true,
/** @deprecated */
bool $replaceWhenPresentInBody = true
) {
parent::__construct($headerParameter, $replaceWhenPresentInPayload, $replaceWhenPresentInBody);
}

public function resolve(
DataProperty $dataProperty,
mixed $payload,
array $properties,
CreationContext $creationContext
): mixed {
return $this->resolvePropertyForInjectedValue(
$dataProperty,
$payload,
$properties,
$creationContext
);
}

protected function getPropertyKey(): string|null
{
return $this->property;
}
}