This repository was archived by the owner on Jul 16, 2021. It is now read-only.

Description
Wouldn't be great to encapsulate the attributes manipulation? That could be very useful eg. in the Request class. Regarding casting I thought of something as simple as:
- CastableContract
- CastableTrait
abstract class Model implements ArrayAccess, Arrayable, Castable, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
{
use CastableTrait;
…
}
class Request extends SymfonyRequest implements Arrayable, ArrayAccess, Castable
{
use CastableTrait, Macroable;
…
}
In combination with custom casting discussed in laravel/framework#16305 and laravel/framework#13706 it could be a very powerfull yet clean solution. Another approach could be a Processor/Transformer implementation (this is just a basic idea inspired by the need to encapsulate request data processing):
interface Processable
{
/**
* Get the processed contents of the object.
*
* @return mixed
*/
public function processed();
}
- an object/array could be also easily processed with a processor composite:
class ProcessableStack extends \SplObjectStorage implements Processable
{
/**
* Calculates an unique identifier for the contained processor.
*
* @param Processable $object
*
* @return string
*/
public function getHash($object): string
{
return get_class($object);
}
/**
* {@inheritdoc}
*/
public function processed()
{
if (!$input = func_get_arg(0)) {
return;
}
foreach ($this as $hash => $object) {
$input = $object->processed($input);
}
return $input;
}
}
$request->processedWith(UserUpdateProcessor::class);
// or
$request->setProcessor(new UserUpdateProcessor)->processed();
// or
$request->processed();
or maybe better internally by all(), input(), except() etc.?