Skip to content

kjdev/php-ext-jq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Extension for jq

Build Status

This extension allows jq.

Build

% git clone --depth=1 https://github.com/kjdev/php-ext-jq.git
% cd php-ext-jq
% phpize
% ./configure
% make
% make test
% make install

Configuration

jq.ini:

extension=jq.so
; jq.display_errors=Off

Usage

$jq = Jq\Input::fromString('{"name": "jq", "version": "0.1.0"}');
print_r($jq->filter('.'));
echo 'NAME: ', $jq->filter('.name'), PHP_EOL;
echo 'VERSION: ', $jq->filter('.version'), PHP_EOL;

output:

Array
(
    [name] => jq
    [version] => 0.1.0
)
NAME: jq
VERSION: 0.1.0

Class synopsis

Jq\Input

Jq\Input {
  public static Jq\Executor fromString(string $text)
  public static Jq\Executor fromFile(string $file)
}

Jq\Input::fromString

public static Jq\Executor fromString(string $text)

Load a JSON string.

Parameters:

  • text

    JSON text string.

Return Values:

Returns Jq\Executor instance.


Jq\Input::fromFile

public static Jq\Executor fromFile(string $file)

Load a JSON file.

Parameters:

  • file

    JSON file name.

Return Values:

Returns Jq\Executor instance.


Jq\Executor

Jq\Executor {
  public mixed filter(string $filter, int $flags)
}

Jq\Executor::filter

public mixed filter(string $filter, int $flags = 0)

Get filtering result of the load string.

Parameters:

  • filter

    jq filter string.

  • flags

    • Jq\RAW is raw output
    • Jq\SORT is object with the keys in sorted order

Return Values:

Returns the result value, or FALSE on error.


Jq\Run

Jq\Run {
  public static mixed fromString(string $text, string $filter, int $flags = 0)
  public static mixed fromFile(string $file, string $filter, int $flags = 0)
}

Jq\Run::fromString

public static mixed fromString(string $text, string $filter, int $flags = 0)

Get filtering result of the JSON string.

Parameters:

  • text

    JSON text string.

  • filter

    jq filter string.

  • flags

    • Jq\RAW is raw output
    • Jq\SORT is object with the keys in sorted order

Return Values:

Returns the result value, or FALSE on error.


Jq\Run::fromFile

public static mixed fromFile(string $file, string $filter, int $flags = 0)

Get filtering result of the JSON file.

Parameters:

  • file

    JSON file name.

  • filter

    jq filter string.

  • flags

    • Jq\RAW is raw output
    • Jq\SORT is object with the keys in sorted order

Return Values:

Returns the result value, or FALSE on error.

Examples

  • Setting a Jq\RAW
$jq = Jq\Input::fromString('{"name": "jq", "version": "0.1.0"}');
print_r($jq->filter('.', Jq\RAW));
echo PHP_EOL;
echo 'NAME: ', $jq->filter('.name', Jq\RAW), PHP_EOL;
echo 'VERSION: ', $jq->filter('.version', Jq\RAW), PHP_EOL;

The above example will output:

{"name":"jq","version":"0.1.0"}
NAME: jq
VERSION: 0.1.0
  • Execute static function
$text = '{"name": "jq", "version": "0.1.0"}';
print_r(Jq\Run::fromString($text, '.'));
echo 'NAME: ', Jq\Run::fromString($text, '.name'), PHP_EOL;
echo 'VERSION: ', Jq\Run::fromString($text, '.version', Jq\RAW), PHP_EOL;

The above example will output:

Array
(
    [name] => jq
    [version] => 0.1.0
)
NAME: jq
VERSION: 0.1.0

About

This extension allows jq

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
COPYING.jq

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •