Wikibase
MediaWiki Wikibase extension
Wikibase\Lib\Serialization\SerializationModifier Class Reference

Class which can be used to easily modify serializations and arrays. More...

Public Member Functions

 modifyUsingCallbacks (array $array, array $modifications)
 

Private Member Functions

 modifyUsingUnflattenedCallbacks (&$value, array $modifications)
 Iterates the value and modifications and runs all the needed sub-modifications, then runs the modifications of the current array level ('' key), if any. More...
 
 unflattenPaths (array $array)
 Turn a flat array with paths as keys into a nested structure. More...
 
 getPathParts (?string $path)
 

Detailed Description

Class which can be used to easily modify serializations and arrays.

This could easily be factored out into a library.

Member Function Documentation

◆ getPathParts()

Wikibase\Lib\Serialization\SerializationModifier::getPathParts ( ?string  $path)
private
Parameters
null | string$path
Returns
string[]

◆ modifyUsingCallbacks()

Wikibase\Lib\Serialization\SerializationModifier::modifyUsingCallbacks ( array  $array,
array  $modifications 
)
Parameters
array$arrayThe array to modify.
(callable|callable[])[]$modifications Mapping from paths to modifications. Array keys are paths: Element keys should be separated with / characters.
  • characters can be used to match all keys at a given level. Empty string can be used to modify $array directly. Examples: '' 'foo/*' 'root/entities/*\/statement/references/*\/snaks/*' More specific paths always run first (e.g. foo/bar before foo), so that array elements added by callbacks are never matched against other paths. Elements are callbacks or lists of callbacks: Callback accepts 1 parameter which is the element to touch Callback should return the altered element
Returns
array The altered array.

◆ modifyUsingUnflattenedCallbacks()

Wikibase\Lib\Serialization\SerializationModifier::modifyUsingUnflattenedCallbacks ( $value,
array  $modifications 
)
private

Iterates the value and modifications and runs all the needed sub-modifications, then runs the modifications of the current array level ('' key), if any.

Parameters
mixed$valueThe value to modify (usually an array except on leaf nodes).
array$modificationsModifications as returned by unflattenPaths.

◆ unflattenPaths()

Wikibase\Lib\Serialization\SerializationModifier::unflattenPaths ( array  $array)
private

Turn a flat array with paths as keys into a nested structure.

Example input:

[
    '' => 'cb0',
    'claims' => 'cb1',
    'claims/foo' => 'cb2',
    'claims/*\/bar' => 'cb3',
    'label' => 'cb4',
]

Example output:

[
    '' => 'cb0',
    'claims' => [
        '' => 'cb1',
        'foo' => [ '' => 'cb2' ],
        '*' => [
            'bar' => [ '' => 'cb3' ],
        ],
    ],
    'label' => [ '' => 'cb4' ],
]
Parameters
array$array
Returns
array

The documentation for this class was generated from the following file: