4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2019
38 namespace Civi\Api4\Utils
;
40 use CRM_Utils_Array
as UtilsArray
;
42 class ArrayInsertionUtil
{
45 * If the values to be inserted contain a key _parent_id they will only be
46 * inserted if the parent node ID matches their ID
49 * The array to insert the value in
51 * Path to insertion point with structure:
52 * [[ name => is_multiple ], ..]
53 * @param mixed $values
54 * The value to be inserted
56 public static function insert(&$array, $parts, $values) {
58 $isMulti = array_shift($parts);
59 if (!isset($array[$key])) {
60 $array[$key] = $isMulti ?
[] : NULL;
63 $values = self
::filterValues($array, $isMulti, $values);
64 $array[$key] = $values;
68 foreach ($array[$key] as &$subArray) {
69 self
::insert($subArray, $parts, $values);
73 self
::insert($array[$key], $parts, $values);
85 private static function filterValues($parentArray, $isMulti, $values) {
86 $parentID = UtilsArray
::value('id', $parentArray);
89 $values = array_filter($values, function ($value) use ($parentID) {
90 return UtilsArray
::value('_parent_id', $value) == $parentID;
94 $unsets = ['_parent_id', '_base_id'];
95 array_walk($values, function (&$value) use ($unsets) {
96 foreach ($unsets as $unset) {
97 if (isset($value[$unset])) {
98 unset($value[$unset]);
104 $values = array_shift($values);