Skip to content

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

Closed
halaei wants to merge 1 commit into
laravel:masterfrom
halaei:serialize-casts-for-eloquent
Closed

[5.3] php-serialized value object attributes for Eloquent Model#13511
halaei wants to merge 1 commit into
laravel:masterfrom
halaei:serialize-casts-for-eloquent

Conversation

@halaei

@halaei halaei commented May 11, 2016

Copy link
Copy Markdown
Contributor

...(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

halaei commented May 11, 2016

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Member

I think that's the same as #13315

@halaei

halaei commented May 11, 2016

Copy link
Copy Markdown
Contributor Author

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

halaei commented May 11, 2016

Copy link
Copy Markdown
Contributor Author

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

@taylorotwell

Copy link
Copy Markdown
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