Documentation

JsonFile
in package

Reads/writes json files.

Tags
author

Konstantin Kudryashiv ever.zet@gmail.com

author

Jordi Boggiano j.boggiano@seld.be

Table of Contents

COMPOSER_SCHEMA_PATH  = '/../../../res/composer-schema.json'
JSON_PRETTY_PRINT  = 128
JSON_UNESCAPED_SLASHES  = 64
JSON_UNESCAPED_UNICODE  = 256
LAX_SCHEMA  = 1
STRICT_SCHEMA  = 2
$io  : mixed
$path  : mixed
$rfs  : mixed
__construct()  : mixed
Initializes json file reader/parser.
encode()  : string
Encodes an array into (optionally pretty-printed) JSON
exists()  : bool
Checks whether json file exists.
getPath()  : string
parseJson()  : mixed
Parses json string and returns hash.
read()  : mixed
Reads json file.
validateSchema()  : bool
Validates the schema of the current json file according to composer-schema.json rules
write()  : mixed
Writes json file.
validateSyntax()  : bool
Validates the syntax of a JSON string
filePutContentsIfModified()  : mixed
modify file properties only if content modified
throwEncodeError()  : mixed
Throws an exception according to a given code with a customized message

Constants

COMPOSER_SCHEMA_PATH

public mixed COMPOSER_SCHEMA_PATH = '/../../../res/composer-schema.json'

JSON_PRETTY_PRINT

public mixed JSON_PRETTY_PRINT = 128

JSON_UNESCAPED_SLASHES

public mixed JSON_UNESCAPED_SLASHES = 64

JSON_UNESCAPED_UNICODE

public mixed JSON_UNESCAPED_UNICODE = 256

STRICT_SCHEMA

public mixed STRICT_SCHEMA = 2

Properties

Methods

__construct()

Initializes json file reader/parser.

public __construct(string $path[, RemoteFilesystem $rfs = null ][, IOInterface $io = null ]) : mixed
Parameters
$path : string

path to a lockfile

$rfs : RemoteFilesystem = null

required for loading http/https json files

$io : IOInterface = null
Tags
throws
InvalidArgumentException
Return values
mixed

encode()

Encodes an array into (optionally pretty-printed) JSON

public static encode(mixed $data[, int $options = 448 ]) : string
Parameters
$data : mixed

Data to encode into a formatted JSON string

$options : int = 448

json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)

Return values
string

Encoded json

exists()

Checks whether json file exists.

public exists() : bool
Return values
bool

getPath()

public getPath() : string
Return values
string

parseJson()

Parses json string and returns hash.

public static parseJson(string $json[, string $file = null ]) : mixed
Parameters
$json : string

json string

$file : string = null

the json file

Return values
mixed

read()

Reads json file.

public read() : mixed
Tags
throws
RuntimeException
Return values
mixed

validateSchema()

Validates the schema of the current json file according to composer-schema.json rules

public validateSchema([int $schema = self::STRICT_SCHEMA ][, string|null $schemaFile = null ]) : bool
Parameters
$schema : int = self::STRICT_SCHEMA

a JsonFile::*_SCHEMA constant

$schemaFile : string|null = null

a path to the schema file

Tags
throws
JsonValidationException
Return values
bool

true on success

write()

Writes json file.

public write(array<string|int, mixed> $hash[, int $options = 448 ]) : mixed
Parameters
$hash : array<string|int, mixed>

writes hash into json file

$options : int = 448

json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)

Tags
throws
UnexpectedValueException|Exception
Return values
mixed

validateSyntax()

Validates the syntax of a JSON string

protected static validateSyntax(string $json[, string $file = null ]) : bool
Parameters
$json : string
$file : string = null
Tags
throws
UnexpectedValueException
throws
ParsingException
Return values
bool

true on success

filePutContentsIfModified()

modify file properties only if content modified

private filePutContentsIfModified(mixed $path, mixed $content) : mixed
Parameters
$path : mixed
$content : mixed
Return values
mixed

throwEncodeError()

Throws an exception according to a given code with a customized message

private static throwEncodeError(int $code) : mixed
Parameters
$code : int

return code of json_last_error function

Tags
throws
RuntimeException
Return values
mixed

Search results