(security/core#67) Port APIv3's escaping scheme to APIv4
[civicrm-core.git] / Civi / Api4 / Utils / ArrayInsertionUtil.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29/**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36
37
19b53e5b
C
38namespace Civi\Api4\Utils;
39
40use CRM_Utils_Array as UtilsArray;
41
42class ArrayInsertionUtil {
43
44 /**
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
47 *
48 * @param $array
49 * The array to insert the value in
50 * @param array $parts
51 * Path to insertion point with structure:
52 * [[ name => is_multiple ], ..]
53 * @param mixed $values
54 * The value to be inserted
55 */
56 public static function insert(&$array, $parts, $values) {
57 $key = key($parts);
58 $isMulti = array_shift($parts);
59 if (!isset($array[$key])) {
60 $array[$key] = $isMulti ? [] : NULL;
61 }
62 if (empty($parts)) {
63 $values = self::filterValues($array, $isMulti, $values);
64 $array[$key] = $values;
65 }
66 else {
67 if ($isMulti) {
68 foreach ($array[$key] as &$subArray) {
69 self::insert($subArray, $parts, $values);
70 }
71 }
72 else {
73 self::insert($array[$key], $parts, $values);
74 }
75 }
76 }
77
78 /**
79 * @param $parentArray
80 * @param $isMulti
81 * @param $values
82 *
83 * @return array|mixed
84 */
85 private static function filterValues($parentArray, $isMulti, $values) {
86 $parentID = UtilsArray::value('id', $parentArray);
87
88 if ($parentID) {
89 $values = array_filter($values, function ($value) use ($parentID) {
90 return UtilsArray::value('_parent_id', $value) == $parentID;
91 });
92 }
93
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]);
99 }
100 }
101 });
102
103 if (!$isMulti) {
104 $values = array_shift($values);
105 }
106 return $values;
107 }
108
109}