Skip to content

[5.3] php-serialized value object attributes for Eloquent Model #13511

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 1 commit into from

Conversation

halaei
Copy link
Contributor

@halaei halaei commented May 11, 2016

...(implementing with casts) supporting inheritance

Sample usecase: chat states for Telegram Bots

class Chat extends Model {
    protected $casts = ['state' => State::class];
    ...
}
abstract class State {
    abstract public function handleTelegramUpdate($update, Chat $chat);
}
class DefaultState extends State {
    public function handleTelegramUpdate($update, Chat $chat)
    {
        if(/*the $update means a menu item is selected*/) {
            $chat->state = new SubMenuState(/*the selected menu item*/);
            //send the sub-menu to the chat.
        }
    }
}
class SubMenuState extends State {
    ...
}

The pseudo code of main Telegram update handler function does the following:

    $update = getTelegramUpdate();
    if ($update is a chat) {
        $chat = $chatRepository->findOrCreate($update->chat_id);
       ...
       if($chat->state === null) {
           (new DefaultState)->handleTelegramUpdate($update, $chat);
       } else {
          $chat->state->handleTelegramUpdate($update, $chat);
       }
       $chat->save();
    }

What do you think?

@halaei
Copy link
Contributor Author

halaei commented May 11, 2016

It is possible to do the same thing with accessors and mutators by the way! So maybe no need for this PR, except it may be more convenient.

@themsaid
Copy link
Member

I think that's the same as #13315

@halaei
Copy link
Contributor Author

halaei commented May 11, 2016

By the way, have you seen this test? Someone might think it is not OK!

    public function testModelDateAttributesAreImmutable()
    {
        $model = new EloquentModelCastingStub();
        $model->setDateFormat('Y-m-d H:i:s');

        $model->dateAttribute = '2016-01-03 10:11:12';
        $this->assertNotSame($model->dateAttribute, $model->dateAttribute);
        $model->dateAttribute->second = 0;
        $this->assertEquals(12, $model->dateAttribute->second);

        $model['dateAttribute'] = '2016-01-03 10:11:12';
        $this->assertNotSame($model['dateAttribute'], $model['dateAttribute']);
        $model['dateAttribute']->second = 0;
        $this->assertEquals(12, $model['dateAttribute']->second);
    }

@halaei
Copy link
Contributor Author

halaei commented May 11, 2016

This PR is based on php serialization and support inheritance as well! I guess it is not 100% similar to #13315

@taylorotwell
Copy link
Member

Going to hold off on this in the core.

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

Successfully merging this pull request may close these issues.

3 participants