class PositionalArraySorter (View source)

Flexible array sorter that sorts an array according to a "position" meta data.

The expected format for the $subject is:

array( array( 'foo' => 'bar' 'position' => '', ), array( 'foo' => 'baz' 'position' => '', ), )

The supports one of the following syntax: start () end () before () after ()

where "weight" is the priority that defines which of two conflicting positions overrules the other, "key" is a string that references another key in the $subject and "numerical-order" is an integer that defines the order independently from the other keys. With the $positionPropertyPath parameter the property path to the position string can be changed.

Properties

protected array $subject
protected string $positionPropertyPath
protected array $startKeys
protected array $middleKeys
protected array $endKeys
protected array $beforeKeys
protected array $afterKeys

Methods

__construct(array $subject, string $positionPropertyPath = 'position')

No description

array
toArray()

Returns a sorted copy of the subject array

array
getSortedKeys()

Returns the keys of $this->subject sorted according to the position meta data

void
extractMiddleKeys(array $arrayKeysWithPosition)

Extracts all "middle" keys from $arrayKeysWithPosition. Those are all keys with a numeric position.

void
extractStartKeys(array $arrayKeysWithPosition)

Extracts all "start" keys from $arrayKeysWithPosition. Those are all keys with a position starting with "start" The result is a multi-dimensional arrays where the KEY of each array is a PRIORITY and the VALUE is an array of matching KEYS This also removes matching keys from the given $arrayKeysWithPosition

void
extractEndKeys(array $arrayKeysWithPosition)

Extracts all "end" keys from $arrayKeysWithPosition. Those are all keys with a position starting with "end" The result is a multi-dimensional arrays where the KEY of each array is a PRIORITY and the VALUE is an array of matching KEYS This also removes matching keys from the given $arrayKeysWithPosition

void
extractBeforeKeys(array $arrayKeysWithPosition, array $existingKeys)

Extracts all "before" keys from $arrayKeysWithPosition. Those are all keys with a position starting with "before" The result is a multi-dimensional arrays where the KEY of each array is a PRIORITY and the VALUE is an array of matching KEYS This also removes matching keys from the given $arrayKeysWithPosition

void
extractAfterKeys(array $arrayKeysWithPosition, array $existingKeys)

Extracts all "after" keys from $arrayKeysWithPosition. Those are all keys with a position starting with "after" The result is a multi-dimensional arrays where the KEY of each array is a PRIORITY and the VALUE is an array of matching KEYS This also removes matching keys from the given $arrayKeysWithPosition

array
collectArrayKeysAndPositions()

Collect the array keys inside $this->subject with each position meta-argument.

array
generateSortedKeysMap()

Flattens start-, middle-, end-, before- and afterKeys to a single dimension and merges them together to a single array

Details

__construct(array $subject, string $positionPropertyPath = 'position')

No description

Parameters

array $subject

The source array to sort

string $positionPropertyPath

optional property path to the string that contains the position

array toArray()

Returns a sorted copy of the subject array

Return Value

array

array getSortedKeys()

Returns the keys of $this->subject sorted according to the position meta data

TODO Detect circles in after / before dependencies (#52185)

Return Value

array

an ordered list of keys

Exceptions

InvalidPositionException

protected void extractMiddleKeys(array $arrayKeysWithPosition)

Extracts all "middle" keys from $arrayKeysWithPosition. Those are all keys with a numeric position.

The result is a multi-dimensional arrays where the KEY of each array is a PRIORITY and the VALUE is an array of matching KEYS This also removes matching keys from the given $arrayKeysWithPosition

Parameters

array $arrayKeysWithPosition

Return Value

void

protected void extractStartKeys(array $arrayKeysWithPosition)

Extracts all "start" keys from $arrayKeysWithPosition. Those are all keys with a position starting with "start" The result is a multi-dimensional arrays where the KEY of each array is a PRIORITY and the VALUE is an array of matching KEYS This also removes matching keys from the given $arrayKeysWithPosition

Parameters

array $arrayKeysWithPosition

Return Value

void

protected void extractEndKeys(array $arrayKeysWithPosition)

Extracts all "end" keys from $arrayKeysWithPosition. Those are all keys with a position starting with "end" The result is a multi-dimensional arrays where the KEY of each array is a PRIORITY and the VALUE is an array of matching KEYS This also removes matching keys from the given $arrayKeysWithPosition

Parameters

array $arrayKeysWithPosition

Return Value

void

protected void extractBeforeKeys(array $arrayKeysWithPosition, array $existingKeys)

Extracts all "before" keys from $arrayKeysWithPosition. Those are all keys with a position starting with "before" The result is a multi-dimensional arrays where the KEY of each array is a PRIORITY and the VALUE is an array of matching KEYS This also removes matching keys from the given $arrayKeysWithPosition

Parameters

array $arrayKeysWithPosition
array $existingKeys

Return Value

void

protected void extractAfterKeys(array $arrayKeysWithPosition, array $existingKeys)

Extracts all "after" keys from $arrayKeysWithPosition. Those are all keys with a position starting with "after" The result is a multi-dimensional arrays where the KEY of each array is a PRIORITY and the VALUE is an array of matching KEYS This also removes matching keys from the given $arrayKeysWithPosition

Parameters

array $arrayKeysWithPosition
array $existingKeys

Return Value

void

protected array collectArrayKeysAndPositions()

Collect the array keys inside $this->subject with each position meta-argument.

If there is no position but the array is numerically ordered, we use the array index as position.

Return Value

array

an associative array where each key of $subject has a position string assigned

protected array generateSortedKeysMap()

Flattens start-, middle-, end-, before- and afterKeys to a single dimension and merges them together to a single array

Return Value

array