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 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Generic
;
15 use Civi\API\Exception\NotImplementedException
;
18 * Create a new $ENTITY from supplied values.
20 * This action will create 1 new $ENTITY.
21 * It cannot be used to update existing $ENTITIES; use the `Update` or `Replace` actions for that.
23 class BasicCreateAction
extends AbstractCreateAction
{
27 * Function(array $item, BasicCreateAction $thisAction): array
32 * Basic Create constructor.
34 * @param string $entityName
35 * @param string $actionName
36 * @param callable $setter
38 public function __construct($entityName, $actionName, $setter = NULL) {
39 parent
::__construct($entityName, $actionName);
40 $this->setter
= $setter;
44 * We pass the writeRecord function an array representing one item to write.
45 * We expect to get the same format back.
47 * @param \Civi\Api4\Generic\Result $result
49 public function _run(Result
$result) {
50 $this->formatWriteValues($this->values
);
51 $this->validateValues();
52 $result->exchangeArray([$this->writeRecord($this->values
)]);
56 * This Basic Create class can be used in one of two ways:
58 * 1. Use this class directly by passing a callable ($setter) to the constructor.
59 * 2. Extend this class and override this function.
61 * Either way, this function should return an array representing the one new object.
65 * @throws \Civi\API\Exception\NotImplementedException
67 protected function writeRecord($item) {
68 if (is_callable($this->setter
)) {
69 $this->addCallbackToDebugOutput($this->setter
);
70 return call_user_func($this->setter
, $item, $this);
72 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());