jfadich / json-property by jfadich

Transform an object property into an enhanced array
439
3
2
Package Data
Maintainer Username: jfadich
Package Create Date: 2015-12-05
Package Last Update: 2020-04-02
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-22 03:10:14
Package Statistics
Total Downloads: 439
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 3
Total Watchers: 2
Total Forks: 1
Total Open Issues: 0

Build Status Latest Version HHVM Status

JsonProperty

JsonProperty provides a simple interface for storing key/value pairs to a single column on a model. This is useful for storing meta data or anything without a standard structure. The data is automatically serialized to a JSON when saving.

Installation

Use composer to install the package

composer require jfadich/json-property

Requirements

  • PHP >= 5.5.9

Configuration

  1. Add the JsonPropertyTrait trait to the model
  2. Set the jsonProperty property. This is the name of the method that will be called to access the JsonProperty object. You can set this to an array to enable multiple properties on a single model.
    namespace App;
    
    use Jfadich\JsonProperty\JsonPropertyTrait;
    use Jfadich\JsonProperty\JsonPropertyInterface;
    
    class SampleModel implements JsonPropertyInterface
    {
        use JsonPropertyTrait;
        protected $jsonProperty = 'meta';
    }

Usage

Call a method on the model named after the values you set to $jsonProperty to access the data stored in the JSON string

$model = new SampleModel();

$model->meta()->set('key', 'value');
$value = $model->meta()->get('key'); // 'value'
$value = $model->meta('key'); // 'value'

Available Methods

has($key)

Checks if there is a value for the given key

if($model->meta()->has('keyName')) {
    // Do something
}

get( $key, $default = null )

Get a value, or default if it is not present in the array. You can use the dot notation to access nested arrays.

set($key, $value)

Set/Update the given key/value pair.

merge( array $values, array $allowedKeys = [] )

Merge the given array into the saved object. This will not add keys that don't exist in original object unless the key is included in the whitelist.

push( $key, $value )

If the value for the given $key is an array the value will be pushed to the array.

forget($key)

Remove element from array. This will automatically be persisted

all()

Get all the elements from the array

Examples

The property on the object will always be an up to date JSON string so you can use what ever persistance method you choose.

$model = new SampleModel();

// Use dot notation to access nested values
$model->meta()->set('book.title', 'Cracking the Coding Interview');
$model->meta()->set('book.author', 'Gayle Laakmann McDowell');

// $model->meta
// { "book":{"title": "Cracking the Coding Interview", "author": "Gayle Laakmann McDowell"} }
//
// $model->meta()->all()
// array:3 [
//   "book" => array:2 [
//     "title" => "Cracking the Coding Interview"
//     "author" => "Gayle Laakmann McDowell"
//   ]
// ]

Customization

The JsonProperty object keeps the property on the model up to date with the current JSON string. If you want to automatically persist the data on update feel free to override the saveJsonString() method on the model.

public function saveJsonString($property, $jsonString)
{
    parent::saveJsonString($property, $jsonString);

    $this->saveToSQL(); // Persist to the database or any other method
}