laravel-survey icon indicating copy to clipboard operation
laravel-survey copied to clipboard

Extract the logic for accessing/mutating values into casts

Open matt-daneshvar opened this issue 3 years ago • 0 comments

Prior to this PR, we always coverted array values (e.g. ['apple', 'orange']) into comma and space separated values (e.g. apple, orange) before we stored them.

This PR extracts the logic for accessing/mutating the value attribute of the Answer model, so that this behaviour can be easily customized.

Note: For backward compatiblity, we are using the same SeparatedByCommaAndSpace strategy as the default behaviour.

How to customize casting after this PR

Let's assume we intend all answers to be stored as JSON. To do this, we should:

  1. Create a new Custom Cast. Following the example on Larave's docs:
use \MattDaneshvar\Survey\Contracts\Value;

class Json implements Value
{
    /**
     * Cast the given value.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  string  $key
     * @param  mixed  $value
     * @param  array  $attributes
     * @return array
     */
    public function get($model, $key, $value, $attributes)
    {
        return json_decode($value, true);
    }
 
    /**
     * Prepare the given value for storage.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  string  $key
     * @param  array  $value
     * @param  array  $attributes
     * @return string
     */
    public function set($model, $key, $value, $attributes)
    {
        return json_encode($value);
    }
}
  1. Bind the new implementation to MattDaneshvar\Survey\Contracts\Value::class:
use \MattDaneshvar\Survey\Contracts\Value;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(Value::class, Json::class);
    }

    // ...

matt-daneshvar avatar Apr 17 '22 20:04 matt-daneshvar