定制 wunderwerkio/http-api-utils 二次开发

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

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

wunderwerkio/http-api-utils

Composer 安装命令:

composer require wunderwerkio/http-api-utils

包简介

Set of useful HTTP API utilities. Provides json schema validation and error handling.

README 文档

README

This package provides useful utilities for use with HTTP-APIs of any kind.

For response generation the symfony/http-foundation package is being used.

Install

Install this package via composer:

composer require wunderwerkio/http-api-utils

Features

HttpApiValidationTrait

The HttpApiValidationTrait provides methods to validate a data structure against a [JSON Schema)(https://json-schema.org/). The validation itself is done with the justinrainbow/json-schema package.

Define the schema:

$schema = [
  "type" => "object",
  "properties" => [
    "name" => [
      "type" => "string",
    ],
    "age" => [
      "type" => "integer",
    ],
  ],
  "required" => ["name"],
];

Depending on your input data, you can use different validation methods to suit your needs:

  • validateDataStructure Accepts a reference to an object. This is meant to be used with results from json_decode.
  • validateArray Accepts an array as the data to validate.
  • validateJsonString Accepts a JSON encoded string.

Example (with the schema from above):

$validData = (object) [
  'name' => 'Max',
  'age' => 42,
];

$result = $this->validateDataStructure($validData, $schema);

$result->isValid(); // -> TRUE

// With invalid data.

$invalidData = (object) [
  'age' => '42',
];

$result = $this->validateDataStructure($invalidData, $schema);

$result->isValid(); // -> FALSE
$result->getErrors(); // -> Array of validation errors.
$result->getResponse(); // -> JsonApiErrorResponse with the validation errors.

For more information, check out the JSON Schema package: https://github.com/justinrainbow/json-schema.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-03