rmtram/jsonp-parser

processing of JSONP decoding and encoding.

v1.0.0 2016-04-23 05:17 UTC

This package is auto-updated.

Last update: 2024-10-21 20:32:32 UTC


README

Build Status Total Downloads Latest Stable Version

JsonpPaser

The processing of JSONP decoding and encoding.

Install

composer require rmtram/jsonp-parser

Usage

Encoder

  • encode
$parser = new Rmtram\JsonpParser\Jsonp();
$parser->encoder(['name' => 'example'])
    ->encode();
// default callbackName = callback
string(28) "callback({"name":"example"})"
  • change callback name.
$parser = new Rmtram\JsonpParser\Jsonp();
$parser->encoder(['name' => 'example'])
    ->callback('example')
    ->encode();
string(28) "example({"name":"example"})"
  • depth(default 512)
  • PHP 5.5 and over
  • PHP Version 5.4 can not be used
$parser = new Rmtram\JsonpParser\Jsonp();
$parser
    ->encoder([['name' => 'example']])
    ->depth(1)
    ->encode();
false
  • option(default 0)

@link json.constants http://php.net/manual/en/json.constants.php

$parser = new Rmtram\JsonpParser\Jsonp();
$parser
    ->encoder([['name' => 'example']])
    ->option(JSON_UNESCAPED_UNICODE)
    ->encode();

Decoder

  • toArray
$parser = new Rmtram\JsonpParser\Jsonp();
var_dump($parser->decoder('callback({"name": "example"})')->toArray());
array(1) {
  ["name"]=> string(7) "example"
}
  • toObject
$parser = new Rmtram\JsonpParser\Jsonp();
var_dump($parser->decoder('callback({"name": "example"})')->toObject());
object(stdClass)#45 (1) {
  ["name"]=> string(7) "example"
}
  • trim the EOL.
$parser = new Rmtram\JsonpParser\Jsonp();
$parser->decoder('callback(
{"name":"example"})'
)->trimEOL()->toArray();
array(1) {
  ["name"]=> string(7) "example"
}
  • not trim the EOL.
$parser = new Rmtram\JsonpParser\Jsonp();
$parser->decoder('callback(
{"name":"example"})'
)->toArray();
null
  • depth(default 512)
$parser = new Rmtram\JsonpParser\Jsonp();
$parser
    ->decoder('callback([{"name": "example"}])')
    ->depth(1)
    ->toArray();
null
  • option(default 0)

@link json.constants http://php.net/manual/en/json.constants.php

$parser = new Rmtram\JsonpParser\Jsonp();
$parser
    ->decoder('callback([{"name": "example"}])')
    ->option(JSON_BIGINT_AS_STRING)
    ->toArray();

Util

  • check jsonp format.
//@param string $jsonp jsonp string.
//@param boolean $trimEOL (trim CR+LF/CR/LF) 
//@param boolean $strict true => 'regex and json_decode', false => 'regexp'
is($jsonp, $trimEOL = false, $strict = true)
$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback([1,2,3])');
true
$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback[1,2,3])');
false

trim eol

$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback(
[1,2,3])', true);
true
$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback(
[1,2,3])', false);
false

strict mode

normal

$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback([1,2,3])(1,2,3)', false, false);
true

strict

$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback([1,2,3])(1,2,3)', false, true);
false

normal

$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback([1,2,3])(1,2,3', false, false);
false

strict

$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback([1,2,3])(1,2,3', false, true);
false

normal

$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback(example)', false, false);
true

strict

$parser = new Rmtram\JsonpParser\Jsonp();
$parser->is('callback(example)', false, true);
false

Support Version

  • PHP 5.4
  • PHP 5.5
  • PHP 5.6
  • PHP 7
  • HHVM

LICENSE

MIT LICENSE