2017 © Pedro Peláez
 

library json-property

Transform an object property into an enhanced array

image

jfadich/json-property

Transform an object property into an enhanced array

  • Friday, December 9, 2016
  • by jfadich
  • Repository
  • 1 Watchers
  • 3 Stars
  • 151 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 7 % Grown

The README.md

Build Status Latest Version HHVM Status, (*1)

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., (*2)

Installation

Use composer to install the package, (*3)

composer require jfadich/json-property, (*4)

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, (*5)

$model = new SampleModel();

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

Available Methods

has($key), (*6)

Checks if there is a value for the given key, (*7)

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

get( $key, $default = null ), (*8)

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

set($key, $value), (*10)

Set/Update the given key/value pair., (*11)

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

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., (*13)

push( $key, $value ), (*14)

If the value for the given $key is an array the value will be pushed to the array., (*15)

forget($key), (*16)

Remove element from array. This will automatically be persisted, (*17)

all(), (*18)

Get all the elements from the array, (*19)

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., (*20)

$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., (*21)

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

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

The Versions

09/12 2016

dev-master

9999999-dev

Transform an object property into an enhanced array

  Sources   Download

MIT

The Requires

 

The Development Requires

by John Fadich

json

09/12 2016

0.1.1

0.1.1.0

Transform an object property into an enhanced array

  Sources   Download

MIT

The Requires

 

The Development Requires

by John Fadich

json

09/12 2016

dev-dev

dev-dev

Transform an object property into an enhanced array

  Sources   Download

MIT

The Requires

 

The Development Requires

by John Fadich

json

13/12 2015

0.1.0

0.1.0.0

Transform an object property into an enhanced array

  Sources   Download

MIT

The Requires

 

The Development Requires

by John Fadich

json

09/12 2015

0.0.5

0.0.5.0

Transform an object property into an enhanced array

  Sources   Download

MIT

The Requires

 

The Development Requires

by John Fadich

json

06/12 2015

0.0.4

0.0.4.0

Transform an object property into an enhanced array

  Sources   Download

MIT

The Requires

 

The Development Requires

by John Fadich

json

05/12 2015

0.0.3

0.0.3.0

Makes interacting with a JSON string stored in a database easier

  Sources   Download

MIT

The Requires

 

The Development Requires

by John Fadich

laravel json

05/12 2015

0.0.2

0.0.2.0

Makes interacting with a JSON string stored in a database easier

  Sources   Download

MIT

The Requires

 

The Development Requires

by John Fadich

laravel json

05/12 2015

0.0.1

0.0.1.0

Makes interacting with a JSON string stored in a database easier

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by John Fadich

laravel json