Merge pull request #17305 from mlutfy/core1755
[civicrm-core.git] / Civi / Api4 / Generic / BasicSaveAction.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 */
18
19
20 namespace Civi\Api4\Generic;
21
22 use Civi\API\Exception\NotImplementedException;
23
24 /**
25 * $ACTION one or more $ENTITIES.
26 *
27 * If saving more than one new $ENTITY with similar values, use the `defaults` parameter.
28 *
29 * Set `reload` if you need the api to return complete $ENTITY records.
30 */
31 class BasicSaveAction extends AbstractSaveAction {
32
33 /**
34 * @var callable
35 * Function(array $item, BasicCreateAction $thisAction): array
36 */
37 private $setter;
38
39 /**
40 * Basic Create constructor.
41 *
42 * @param string $entityName
43 * @param string $actionName
44 * @param string $idField
45 * @param callable $setter
46 */
47 public function __construct($entityName, $actionName, $idField = 'id', $setter = NULL) {
48 parent::__construct($entityName, $actionName, $idField);
49 $this->setter = $setter;
50 }
51
52 /**
53 * We pass the writeRecord function an array representing one item to write.
54 * We expect to get the same format back.
55 *
56 * @param \Civi\Api4\Generic\Result $result
57 */
58 public function _run(Result $result) {
59 foreach ($this->records as &$record) {
60 $record += $this->defaults;
61 $this->formatWriteValues($record);
62 }
63 $this->validateValues();
64 foreach ($this->records as $item) {
65 $result[] = $this->writeRecord($item);
66 }
67 if ($this->reload) {
68 /** @var BasicGetAction $get */
69 $get = \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4]);
70 $get
71 ->setCheckPermissions($this->getCheckPermissions())
72 ->addWhere($this->getIdField(), 'IN', (array) $result->column($this->getIdField()));
73 $result->exchangeArray((array) $get->execute());
74 }
75 }
76
77 /**
78 * This Basic Save class can be used in one of two ways:
79 *
80 * 1. Use this class directly by passing a callable ($setter) to the constructor.
81 * 2. Extend this class and override this function.
82 *
83 * Either way, this function should return an array representing the one new object.
84 *
85 * @param array $item
86 * @return array
87 * @throws \Civi\API\Exception\NotImplementedException
88 */
89 protected function writeRecord($item) {
90 if (is_callable($this->setter)) {
91 $this->addCallbackToDebugOutput($this->setter);
92 return call_user_func($this->setter, $item, $this);
93 }
94 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
95 }
96
97 }