Merge pull request #16372 from colemanw/api4Magic
[civicrm-core.git] / Civi / Api4 / Action / Setting / Set.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21 namespace Civi\Api4\Action\Setting;
22
23 use Civi\Api4\Generic\Result;
24
25 /**
26 * Set the value of one or more CiviCRM settings.
27 *
28 * @method array getValues
29 * @method $this setValues(array $value)
30 */
31 class Set extends AbstractSettingAction {
32
33 /**
34 * Setting names/values to set.
35 *
36 * @var mixed
37 * @required
38 */
39 protected $values = [];
40
41 /**
42 * @param \Civi\Api4\Generic\Result $result
43 * @param \Civi\Core\SettingsBag $settingsBag
44 * @param array $meta
45 * @param int $domain
46 * @throws \Exception
47 */
48 protected function processSettings(Result $result, $settingsBag, $meta, $domain) {
49 foreach ($this->values as $name => $value) {
50 if (isset($value) && !empty($meta[$name]['serialize'])) {
51 $value = \CRM_Core_DAO::serializeField($value, $meta[$name]['serialize']);
52 }
53 $settingsBag->set($name, $value);
54 $result[] = [
55 'name' => $name,
56 'value' => $this->values[$name],
57 'domain_id' => $domain,
58 ];
59 }
60 }
61
62 /**
63 * Add an item to the values array
64 * @param string $settingName
65 * @param mixed $value
66 * @return $this
67 */
68 public function addValue($settingName, $value) {
69 $this->values[$settingName] = $value;
70 return $this;
71 }
72
73 }