class DataTree implements ArrayAccess, Serializable, IteratorAggregate, Countable, ArrayRepresentation, JsonSerializable (View source)

deprecated Use Container methods and noresources/ns-php-data package

Serializable data tree structure

Constants

REPLACE

Content fusion mode.

Replace existing data with new content.

MERGE

Content fusion mode.

  • Merge exising data with new content.
  • Append new keys

MERGE_OVERWRITE

Content fusion mode.

  • Merge existing content with new content.
  • Overwrite existing key values.

_FUSION_MODES

Combination of all fusion modes flags

Methods

__construct(array $data = [])

No description

__clone()

Deep copy of the tree

string
__toString()

String representation

The
__get(string $key)

No description

__set(string $key, mixed $value)

Equivalent of offsetSet

offsetExists($key)

No description

has(string $key)

Indicates if a element key exists

offsetGet($key)

No description

The
get(string $key)

Get a value associated to a key

offsetSet($key, $value)

No description

setElement(string|integer $key, mixed $value, integer $mode = self::REPLACE)

Set element of a DataTree

offsetUnset($key)

No description

getIterator()

No description

count()

No description

jsonSerialize()

No description

serialize()

Serialize table to JSON

unserialize(string $serialized)

Load element table from JSON

array
getArrayCopy()

Convert the DataTree to a regular PHP array

mixed
getElement(mixed $key, mixed $defaultValue = null)

Return the element value or the given default value if the setting is not present

append($value)

No description

New
prepend(mixed $value)

Insert an indexed value at the beginning of the setting table

setContent(array $data, integer $mode = self::REPLACE)

No description

loadFile(string $filename, integer $mode = self::REPLACE, string|null $mediaType = null)

Load a DataTree from a file

loadData($data, $structuredTextFormat, $mode = self::REPLACE)

No description

Details

__construct(array $data = [])

Parameters

array $data Initial data

__clone()

Deep copy of the tree

string __toString()

String representation

Return Value

string

The __get(string $key)

Parameters

string $key Arrat key

Return Value

The

Exceptions

InvalidArgumentException

__set(string $key, mixed $value)

Equivalent of offsetSet

Parameters

string $key Key
mixed $value Value

offsetExists($key)

Parameters

$key

has(string $key)

Indicates if a element key exists

Parameters

string $key

offsetGet($key)

Parameters

$key

The get(string $key)

Get a value associated to a key

This method follow the PSR-11 implementation requirements. It is proposed as an interoperability effort. However, the behavior of this method differs from the regular behavior of DataTree.

Parameters

string $key Key. According to PSR-11, $key MUST be a string

Return Value

The element value or NULL if the key does not exists

Exceptions

DataTreeElementNotFoundException

See also

https://www.php-fig.org/psr/psr-11/

offsetSet($key, $value)

Parameters

$key
$value

setElement(string|integer $key, mixed $value, integer $mode = self::REPLACE)

Set element of a DataTree

Parameters

string|integer $key Element key
mixed $value Element value
integer $mode Fusion mode

Exceptions

Exception

offsetUnset($key)

Parameters

$key

getIterator()

count()

jsonSerialize()

serialize()

Serialize table to JSON

unserialize(string $serialized)

Load element table from JSON

Parameters

string $serialized A JSON string

array getArrayCopy()

Convert the DataTree to a regular PHP array

Return Value

array Array representation of the class instance

mixed getElement(mixed $key, mixed $defaultValue = null)

Return the element value or the given default value if the setting is not present

Parameters

mixed $key String key or array of string key representing the setting subpath
mixed $defaultValue

Return Value

mixed

append($value)

Parameters

$value

New prepend(mixed $value)

Insert an indexed value at the beginning of the setting table

Parameters

mixed $value

Return Value

New number of settings

DataTree setContent(array $data, integer $mode = self::REPLACE)

Parameters

array $data Data tree content
integer $mode Fusion mode

Return Value

DataTree

Exceptions

ErrorException

DataTree loadFile(string $filename, integer $mode = self::REPLACE, string|null $mediaType = null)

Load a DataTree from a file

Parameters

string $filename File name. File format could be

  • JSON
  • YAML
  • Ini
  • php
    PHP files are loaded with the require() function and expect a Traversable return
    value.

    Several media type support depends on available PHP extensions.
integer $mode Control interaction with existing content
string|null $mediaType Specify file media type. If null, media type is automatically detected

Return Value

DataTree

Exceptions

InvalidArgumentException

loadData($data, $structuredTextFormat, $mode = self::REPLACE)

Parameters

$data
$structuredTextFormat
$mode