8000 [Proposal] Decouple mutators / attribute casting from the Eloquent ORM · Issue #16377 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[Proposal] Decouple mutators / attribute casting from the Eloquent ORM #16377

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
8000
andrzejkupczyk opened this issue Nov 12, 2016 · 1 comment
Closed

Comments

@andrzejkupczyk
Copy link
Contributor

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 #16305 and #13706 it could be a very powerfull and yet a 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):

  • Processable contract
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 Processor $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 => $processor) {
            $input = $processor->run($input);
        }

        return $input;
    }
}
  • examples of use:
$request->processedWith(UserUpdateRequest::class);
// or
$request->setProcessor(new UserUpdateRequest)->processed();
// or
$request->processed();

or maybe better interally by all(), input(), except() etc.?

@GrahamCampbell
Copy link
Member

Thanks. Please open an issue on the internals repo to discuss this. We strictly only deal with isolated bug reports here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0