Mais conteúdo relacionado
Semelhante a JSON SchemaとPHP (14)
Mais de Hiraku Nakano (7)
JSON SchemaとPHP
- 2. 自己紹介
• 中野 拓
• https://twitter.com/Hiraku
• http://blog.tojiru.net/
• PHP歴6年ぐらい
• 最近使ってるフレームワークはYafとPhalconです
• WebAPI関係のお仕事
• 最近コード書いてない。。
- 9. JSON Schema
{
integer
“id”: 12345,
“name”: “Hiraku NAKANO”,
“tags”: [“PHP”,”JavaScript”]
}
id, name, tags という
プロパティを持った
オブジェクト
{
string
string[]
“type”:”object”,
“properties”: {
“id”: {“type”:”integer”},
“name”: {“type”:”string”},
“tags”: {
“type”:”array”,
“items”:{“type”:”string”}
}}}
- 10. もし型が違ったら、判定できる
{
integer
“id”: 12345,
“name”: “Hiraku NAKANO”,
“tags”: “PHP,JavaScript”
}
id, name, tags という
プロパティを持った
オブジェクト
{
string
string[]
“type”:”object”,
“properties”: {
“id”: {“type”:”integer”},
“name”: {“type”:”string”},
“tags”: {
“type”:”array”,
“items”:{“type”:”string”}
}}}
- 12. PHPでの実装
• JSON Schema for PHP がオススメ
https://github.com/justinrainbow/json-schema
• composerの内部で使われている
- 13. <?php
require ‘vendor/autoload.php’;
$json = ‘{“a”:1, “b”:2}’;
$schema = ‘{
“type”:”object”,
“properties”:{
“a”: {“type”:”integer”},
“b”: {“type”:”integer”}
}
}’;
$v = new JsonSchema¥Validator;
$v->check(json_decode($json), json_decode($schema));
var_dump($v->getErrors());
エラーがあれば
指摘してくれる
- 18. よく使うパターンはformatで
JSON
スキーマ
“hiraku@tojiru.net”
{
“type”:”string”,
“format”:”email”
}
date, time, date-time,
uri, host-name, email,
ipv6, ip-address,
color, style,
- 41. <?php
//...
$v = new JsonSchema¥Validator;
$get = json_decode(json_encode($_GET));
$v->check($get, $schema);
var_dump($v->getErrors());
- 44. <?php
/**
* @param int $a
* @param string $b
* @param array $config
*/
function hoge($a, $b, array $config) {
//...
}