定制 zerifas/json 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

zerifas/json

Composer 安装命令:

composer require zerifas/json

包简介

Define a schema for your JSON documents, and validate them

README 文档

README

This is a small library allowing you to define a schema for your JSON documents, validate them, and get a "safe" version of the document, with all optional values set to their defaults.

Installation

Use composer:

$ composer required zerifas/json

Usage

<?php

require 'vendor/autoload.php';

use Zerifas\JSON;

$schema = new JSON\Obj([
    'id' => new JSON\Number(),
    'enabled' => new JSON\OptionalBoolean(false),
    'array' => new JSON\Arr(),
    'stringArray' => new JSON\Arr(new JSON\Str()),
    'optionalArray' => new JSON\OptionalArr(),
    'optionalStringArray' => new JSON\OptionalArr(new JSON\Str()),
    'optionalObj' => new JSON\OptionalObj(
        [
            'name' => new JSON\Str(),
        ],
        [
            'name' => 'Alice',
        ]
    ),
]);
$v = new JSON\Validator($schema);

$json = '{"id":1,"array":[],"stringArray":["Hello","World"]}';
if ($v->isValid($json)) {
    $doc = $v->getDocument();
    echo implode(', ', $doc->stringArray), PHP_EOL; // Hello, World
    echo $doc->optionalObj->name, PHP_EOL; // Alice
}

// This is not valid for 2 reasons: `id` is missing, and `array` is a number.
$json = '{"array":15,"stringArray":[]}';
if (!$v->isValid($json)) {
    // Errors will be an array:
    // [
    //     'Key path \'id\' is required, but missing.',
    //     'Key path \'array\' should be array, but is number.',
    // ]
    foreach ($v->getErrors() as $err) {
        echo $err, PHP_EOL;
    }
}

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-10-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固