Coder Social home page Coder Social logo

vkovic / laravel-custom-casts Goto Github PK

View Code? Open in Web Editor NEW
219.0 3.0 21.0 89 KB

Make your own custom cast type for Laravel model attributes

License: MIT License

PHP 97.53% Dockerfile 2.47%
laravel package casting cast attribute accessor mutator

laravel-custom-casts's Introduction

Hi there ๐Ÿ‘‹ my name is Vladimir Kovic

Kamona-WD's github stats

laravel-custom-casts's People

Contributors

flkc avatar jameshemery avatar jimhlad avatar johnkary avatar marko298 avatar mrmage avatar pvsaintpe avatar sbine avatar vkovic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-custom-casts's Issues

Typo in docs?

The optional getAttribute method receives the raw $value from the database, and should return a mutated value. If this method is omitted, the raw database value will be returned.

Is it really getAttribute or rather castAttribute? The example is kinda vague about this.

Laravel 6 support

Laravel 6 is released. It would be nice to have this package updated to support Laravel 6.

Parameters

In native laravel cast, you can add params for casts. For example date.
Adding this feature gives more control over casting process.

class User extends Model {
  protected $casts = [
    'avatar' => 'file:disk',
  ];
}

Manager

To give people to distribute their custom casts, we have to add the ability to dynamically register new casts. In laravel such functionality is done using Manager::class and extend method.

class PackageServiceProvider extends ServiceProvider
{
    public function register()
    {        
        $this->app->make(CustomCastManager::class)->extend('file', FileCast::class);
    }
}

Lumen compatibility

According to your composer you are strictly targeting Laravel framework, however inside you are using only it's components like eloquent and maybe it's dependencies.

All the functionality can be easily reused inside Lumen framework (micro-framework of Laravel) cause it also has support for Eloquent usage. But unfortunately your composer requires from me specially Laravel.

Pls change your dependencies to concrete illuminate packages versions so this package can be installed on both Laravel and Lumen

Tests failing

Tests failing because of deprecation of str_* functions.

We should use Illuminate\Support\Str instead.

Laravel 5.8 incompatibility

Hi
It seems your package is incompatible with Laravel v5.8.

ฮป composer require vkovic/laravel-custom-casts
Using version ^1.0 for vkovic/laravel-custom-casts
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: remove laravel/framework v5.8.14
    - Conclusion: don't install laravel/framework v5.8.14
    - vkovic/laravel-custom-casts v1.0.0 requires laravel/framework 5.5.*|5.6.*|5.7.* -> satisfiable by laravel/framework[5.5.x-dev, 5.6.x-dev, 5.7.x-dev].
    - vkovic/laravel-custom-casts v1.0.1 requires laravel/framework 5.5.*|5.6.*|5.7.* -> satisfiable by laravel/framework[5.5.x-dev, 5.6.x-dev, 5.7.x-dev].
    - Can only install one of: laravel/framework[5.5.x-dev, v5.8.14].
    - Can only install one of: laravel/framework[5.6.x-dev, v5.8.14].
    - Can only install one of: laravel/framework[5.7.x-dev, v5.8.14].
    - Installation request for laravel/framework (locked at v5.8.14, required as 5.8.*) -> satisfiable by laravel/framework[v5.8.14].
    - Installation request for vkovic/laravel-custom-casts ^1.0 -> satisfiable by vkovic/laravel-custom-casts[v1.0.0, v1.0.1].

Installation failed, reverting ./composer.json to its original content.

Chainable casts

Idea is to add the ability to use chained casts. Example

class User extends Model {
  protected $casts = [
    'meta' => ['json', CastToMetadataClass::class],
  ];
}

with this you can build a lot of reusable parts and combine them

FirstOrCreate issue

Describe the bug

Running

$email = Network\Email::firstOrCreate( $emailParams );

also tried

$email = new Network\Email;
$email->firstOrCreate( $emailParams );

Those don't run the data conversion.
Although running...

$email = Network\Email::create( $emailParams );

Does run the conversion just fine.

To Reproduce
My cast class is

class PostgresArray extends CustomCastBase
{
    public function setAttribute($value)  {
        if (is_string($value))  return $value;
        if (is_array($value))   return "{" . implode(',', $value) . "}";
    }
    public function castAttribute($value)   {
        if (is_string($value))  return $value;
        if (is_array($value))   return "{" . implode(',', $value) . "}";
    }
}

I'm on the latest current version of your library, using Laravel 6.

I had added dd commands in my cast to make sure it wasn't executing.

Am I missing something from my cast or is this a limitation with the library?

Thanks!

Support for ValueObject cast.

Is your feature request related to a problem? Please describe.
Laravel 9 supports AttributeCasting for ValueObject for example class money
I wanna this library to support ValueObject casting.

Describe the solution you'd like
We probably should add a way to allow to return of multiple value for setAttribute via hashmap or via some custom DTO object SetAttributes

    public function setAttribute($value)
    {
        return ucwords($value);
    }

We should return
Describe alternatives you've considered

Additional context
image

Human readable cast names

I think it would be awesome to be able to specify the cast name in human readable format:

protected $casts = [
    'amount' => MoneyCast::class
];

becomes

protected $casts = [
    'amount' => 'money'
];

Perhaps the approach used could be similar to how Laravel infers the policy class names using the guessPolicyNamesUsing method.

How difficult would this be to add? Not super familiar with this package yet so wasn't sure how much effort is involved (although happy to help out as well!)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.