Skip to content
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

Implement Snake Case Mapping for ViewModel Properties and Methods #48

Merged
merged 3 commits into from
Jan 30, 2023

Conversation

gjinali
Copy link
Contributor

@gjinali gjinali commented Jan 29, 2023

Hello Spatie team,

I use this package to serve data to InertiaJs pages, and it helps me structure and organize my page queries. However, I'd like to keep the component properties in snake case on the front end. Currently, this package does not support it.

I've implemented a solution that works and has test coverage. The solution is inspired by how laravel-data package maps class properties to snake case. Since this package still supports PHP 7, I couldn't use PHP Attributes, instead, a simpler solution is implemented.

Example:

class PageViewModel extends ViewModel
{
    protected $snakeCase = true;
    
    public $dummyProperty = 'abc';
    
    public function publishedPost(): stdClass
    {
        return (object) [
            'title' => 'title',
            'body' => 'body',
        ];
    }
}

the result for the array or response will be like this:

[
  "dummy_property" => "abc",
  "published_post" => [
       "title": "title",
       "body": "body"
  ]
]  

I hope you'll consider merging this feature. Thanks!

@gjinali gjinali changed the title Add Support for Snake Case Property and Method Naming Add Support for Snake Case mapping for class Property and Method Naming Jan 29, 2023
@gjinali gjinali changed the title Add Support for Snake Case mapping for class Property and Method Naming Implement Snake Case Mapping for ViewModel Properties and Methods Jan 30, 2023
@gjinali gjinali requested a review from freekmurze January 30, 2023 11:39
@freekmurze freekmurze merged commit a6faf8e into spatie:main Jan 30, 2023
@freekmurze
Copy link
Member

Thanks!

@gjinali gjinali deleted the mappers branch January 30, 2023 14:23
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.

2 participants