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 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Utils
;
16 * Class ArrayInsertionUtil
18 * @package Civi\Api4\Utils
20 class ArrayInsertionUtil
{
23 * If the values to be inserted contain a key _parent_id they will only be
24 * inserted if the parent node ID matches their ID
27 * The array to insert the value in
29 * Path to insertion point with structure:
30 * [[ name => is_multiple ], ..]
31 * @param mixed $values
32 * The value to be inserted
34 public static function insert(&$array, $parts, $values) {
36 $isMulti = array_shift($parts);
37 if (!isset($array[$key])) {
38 $array[$key] = $isMulti ?
[] : NULL;
41 $values = self
::filterValues($array, $isMulti, $values);
42 $array[$key] = $values;
46 foreach ($array[$key] as &$subArray) {
47 self
::insert($subArray, $parts, $values);
51 self
::insert($array[$key], $parts, $values);
63 private static function filterValues($parentArray, $isMulti, $values) {
64 $parentID = $parentArray['id'] ??
NULL;
67 $values = array_filter($values, function ($value) use ($parentID) {
68 return ($value['_parent_id'] ??
NULL) == $parentID;
72 $unsets = ['_parent_id', '_base_id'];
73 array_walk($values, function (&$value) use ($unsets) {
74 foreach ($unsets as $unset) {
75 if (isset($value[$unset])) {
76 unset($value[$unset]);
82 $values = array_shift($values);