4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace Civi\Api4\Utils
;
24 class ArrayInsertionUtil
{
27 * If the values to be inserted contain a key _parent_id they will only be
28 * inserted if the parent node ID matches their ID
31 * The array to insert the value in
33 * Path to insertion point with structure:
34 * [[ name => is_multiple ], ..]
35 * @param mixed $values
36 * The value to be inserted
38 public static function insert(&$array, $parts, $values) {
40 $isMulti = array_shift($parts);
41 if (!isset($array[$key])) {
42 $array[$key] = $isMulti ?
[] : NULL;
45 $values = self
::filterValues($array, $isMulti, $values);
46 $array[$key] = $values;
50 foreach ($array[$key] as &$subArray) {
51 self
::insert($subArray, $parts, $values);
55 self
::insert($array[$key], $parts, $values);
67 private static function filterValues($parentArray, $isMulti, $values) {
68 $parentID = $parentArray['id'] ??
NULL;
71 $values = array_filter($values, function ($value) use ($parentID) {
72 return ($value['_parent_id'] ??
NULL) == $parentID;
76 $unsets = ['_parent_id', '_base_id'];
77 array_walk($values, function (&$value) use ($unsets) {
78 foreach ($unsets as $unset) {
79 if (isset($value[$unset])) {
80 unset($value[$unset]);
86 $values = array_shift($values);