WP_View_Config_Data::merge_list_by_identityprivateWP 7.1.0

Merges an incoming list into the current one by member identity.

A member of the incoming list whose identity matches one already present merges into it in place, keeping its position; an unmatched member is appended to the end, except a literal null, which carries no identity and holds nothing to merge and so is dropped. An appended member has no existing leaf for a nested null to delete (the same rationale as set()), so its nulls are stripped rather than stored. A matched member's contents merge recursively with the same rules (merge_properties), so the identity-aware merge applies at any nesting level: each key named by the patch is substituted while the others are left intact, and a list nested inside a member merges by identity just like the list it lives in.

Method of the class: WP_View_Config_Data{}

No Hooks.

Returns

array. The merged list.

Usage

// private - for code of main (parent) class only
$result = $this->merge_list_by_identity( $current, $incoming );
$current(array) (required)
The current list.
$incoming(array) (required)
The incoming list.

Changelog

Since 7.1.0 Introduced.

WP_View_Config_Data::merge_list_by_identity() code WP 7.1

private function merge_list_by_identity( array $current, array $incoming ) {
	$result = $current;
	foreach ( $incoming as $item ) {
		// A null member carries no identity and holds nothing to merge,
		// so it is dropped rather than appended as a literal null.
		if ( null === $item ) {
			continue;
		}

		$identity = $this->list_item_identity( $item );

		// Find the index of the existing member with the same identity, if any.
		// If there's none, append the incoming member to the end of the list.
		$index = null;
		if ( null !== $identity ) {
			foreach ( $result as $i => $existing ) {
				if ( $this->list_item_identity( $existing ) === $identity ) {
					$index = $i;
					break;
				}
			}
		}
		if ( null === $index ) {
			// An appended member has no existing leaf for a nested null to
			// delete, so nulls are dropped rather than stored.
			$result[] = $this->strip_nulls( $item );
			continue;
		}

		// Otherwise, merge the incoming member into the existing one in place.
		$result[ $index ] = $this->merge_properties( $result[ $index ], $item, false );
	}

	return $result;
}