laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 31 forks source link

[proposal] The function data_set and data_fill 's parameter ‘value’ support Closure. #2091

Open kaiscol opened 4 years ago

kaiscol commented 4 years ago

I don't know It is a necessary feature.(But I think there should be many scenarios that need to be used in this way.) Please see an example below:

[
    {
        "name":"Jones",
        "telephone":"+1-123-123-1234",
        "cars":[
            {
                "brand":"Maserati",
                "license_plate_number":"ABC 123"
            },
            {
                "brand":"Ferrari",
                "license_plate_number":"DEF 456"
            }
        ]
    },
    {
        "name":"Brown",
        "telephone":"+1-456-456-4567",
        "cars":[
            {
                "brand":"Lamborghini",
                "license_plate_number":"GHI 123"
            },
            {
                "brand":"Porsche",
                "license_plate_number":"JKL 456"
            }
        ]
    }
]

change to

[
    {
        "name":"Jones",
        "telephone":"+1-123-***-1234",
        "cars":[
            {
                "brand":"Maserati",
                "license_plate_number":"ABC ***"
            },
            {
                "brand":"Ferrari",
                "license_plate_number":"DEF ***"
            }
        ]
    },
    {
        "name":"Brown",
        "telephone":"+1-456-***-4567",
        "cars":[
            {
                "brand":"Lamborghini",
                "license_plate_number":"GHI ***"
            },
            {
                "brand":"Porsche",
                "license_plate_number":"JKL ***"
            }
        ]
    }
]

if the data_set and data_fill support value of Closure. I can do that:

data_set($data, '*.telephone', function($originValue){
    // do something
});
data_set($data, '*.cars.*.license_plate_number', function($originValue){
    // do something
});
kaiscol commented 4 years ago

I hope someone could discuss this issue and I'm pleased to code for this issue.