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 use CRM_Utils_Array
as UtilsArray
;
26 class ArrayInsertionUtil
{
29 * If the values to be inserted contain a key _parent_id they will only be
30 * inserted if the parent node ID matches their ID
33 * The array to insert the value in
35 * Path to insertion point with structure:
36 * [[ name => is_multiple ], ..]
37 * @param mixed $values
38 * The value to be inserted
40 public static function insert(&$array, $parts, $values) {
42 $isMulti = array_shift($parts);
43 if (!isset($array[$key])) {
44 $array[$key] = $isMulti ?
[] : NULL;
47 $values = self
::filterValues($array, $isMulti, $values);
48 $array[$key] = $values;
52 foreach ($array[$key] as &$subArray) {
53 self
::insert($subArray, $parts, $values);
57 self
::insert($array[$key], $parts, $values);
69 private static function filterValues($parentArray, $isMulti, $values) {
70 $parentID = UtilsArray
::value('id', $parentArray);
73 $values = array_filter($values, function ($value) use ($parentID) {
74 return UtilsArray
::value('_parent_id', $value) == $parentID;
78 $unsets = ['_parent_id', '_base_id'];
79 array_walk($values, function (&$value) use ($unsets) {
80 foreach ($unsets as $unset) {
81 if (isset($value[$unset])) {
82 unset($value[$unset]);
88 $values = array_shift($values);