8000 [11.x] Typeables by bert-w · Pull Request #51302 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[11.x] Typeables #51302

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
55 changes: 8 additions & 47 deletions src/Illuminate/Config/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use Illuminate\Support\Traits\StrongTypeable;

class Repository implements ArrayAccess, ConfigContract
{
use Macroable;
use Macroable,
StrongTypeable;

/**
* All of the configuration items.
Expand Down Expand Up @@ -87,15 +88,7 @@ public function getMany($keys)
*/
public function string(string $key, $default = null): string
{
$value = $this->get($key, $default);

if (! is_string($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a string, %s given.', $key, gettype($value))
);
}

return $value;
return $this->typed()->get->string($key, $default);
}

/**
Expand All @@ -107,15 +100,7 @@ public function string(string $key, $default = null): string
*/
public function integer(string $key, $default = null): int
{
$value = $this->get($key, $default);

if (! is_int($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be an integer, %s given.', $key, gettype($value))
);
}

return $value;
return $this->typed()->get->integer($key, $default);
}

/**
Expand All @@ -127,15 +112,7 @@ public function integer(string $key, $default = null): int
*/
public function float(string $key, $default = null): float
{
$value = $this->get($key, $default);

if (! is_float($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a float, %s given.', $key, gettype($value))
);
}

return $value;
return $this->typed()->get->float($key, $default);
}

/**
Expand All @@ -147,15 +124,7 @@ public function float(string $key, $default = null): float
*/
public function boolean(string $key, $default = null): bool
{
$value = $this->get($key, $default);

if (! is_bool($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a boolean, %s given.', $key, gettype($value))
);
}

return $value;
return $this->typed()->get->boolean($key, $default);
}

/**
Expand All @@ -167,15 +136,7 @@ public function boolean(string $key, $default = null): bool
*/
public function array(string $key, $default = null): array
{
$value = $this->get($key, $default);

if (! is_array($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be an array, %s given.', $key, gettype($value))
);
}

return $value;
return $this->typed()->get->array($key, $default);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\View\Components\Factory;
use Illuminate\Contracts\Console\Isolatable;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\Typeable;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -18,7 +19,8 @@ class Command extends SymfonyCommand
Concerns\InteractsWithIO,
Concerns\InteractsWithSignals,
Concerns\PromptsForMissingInput,
Macroable;
Macroable,
Typeable;

/**
* The Laravel application instance.
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Http/Concerns/InteractsWithInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function str($key, $default = null)
*/
public function string($key, $default = null)
{
return str($this->input($key, $default));
return $this->typed()->input->string($key, $default);
}

/**
Expand All @@ -342,7 +342,7 @@ public function string($key, $default = null)
*/
public function boolean($key = null, $default = false)
{
return filter_var($this->input($key, $default), FILTER_VALIDATE_BOOLEAN);
return $this->typed()->input->boolean($key, $default);
}

/**
Expand All @@ -354,7 +354,7 @@ public function boolean($key = null, $default = false)
*/
public function integer($key, $default = 0)
{
return intval($this->input($key, $default));
return $this->typed()->input->integer($key, $default);
}

/**
Expand All @@ -366,7 +366,7 @@ public function integer($key, $default = 0)
*/
public function float($key, $default = 0.0)
{
return floatval($this->input($key, $default));
return $this->typed()->input->float($key, $default);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Session\SymfonySessionDecorator;
use Illuminate\Support;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Typeable;
use RuntimeException;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\InputBag;
Expand All @@ -26,6 +28,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
Concerns\InteractsWithContentTypes,
Concerns\InteractsWithFlashData,
Concerns\InteractsWithInput,
Support\Traits\Typeable,
Macroable;

/**
Expand Down Expand Up @@ -771,6 +774,16 @@ public function offsetUnset($offset): void
$this->getInputSource()->remove($offset);
}

/**
* Get the typeable proxy instance.
*
* @return Typeable<$this>
*/
protected function typeable(string $method = 'input'): Typeable
{
return new Typeable($this, $method);
}

/**
* Check if an input element is set on the request.
*
Expand Down
7 changes: 6 additions & 1 deletion src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\Typeable;
use Laravel\SerializableClosure\SerializableClosure;
use LogicException;
use Symfony\Component\Routing\Route as SymfonyRoute;

class Route
{
use CreatesRegularExpressionRouteConstraints, FiltersControllerMiddleware, Macroable, ResolvesRouteDependencies;
use CreatesRegularExpressionRouteConstraints,
FiltersControllerMiddleware,
Macroable,
ResolvesRouteDependencies,
Typeable;

/**
* The URI pattern the route responds to.
Expand Down
28 changes: 28 additions & 0 deletions src/Illuminate/Support/HigherOrderStrongTypeableProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Illuminate\Support;

class HigherOrderStrongTypeableProxy
{
/**
* Create a new strong-typeable proxy instance.
*
* @param mixed $target
* @return void
*/
public function __construct(protected mixed $target)
{
//
}

/**
* Dynamically access properties from the target.
*
* @param string $key
* @return \Illuminate\Support\StrongTypeable
*/
public function __get(string $key): StrongTypeable
{
return new StrongTypeable($this->target, $key);
}
}
28 changes: 28 additions & 0 deletions src/Illuminate/Support/HigherOrderTypeableProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Illuminate\Support;

class HigherOrderTypeableProxy
{
/**
* Create a new typeable proxy instance.
*
* @param mixed $target
* @return void
*/
public function __construct(protected mixed $target)
{
//
}

/**
* Dynamically access properties from the target.
*
* @param string $key
* @return \Illuminate\Support\Typeable
*/
public function __get(string $key): Typeable
{
return new Typeable($this->target, $key);
}
}
Loading
0