承接 jfadich/json-property 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jfadich/json-property

Composer 安装命令:

composer require jfadich/json-property

包简介

Transform an object property into an enhanced array

关键字:

README 文档

README

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
}

统计信息

  • 总下载量: 444
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固