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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace Civi\Api4\Generic
;
22 use Civi\API\Exception\NotImplementedException
;
25 * Create a new $ENTITY from supplied values.
27 * This action will create 1 new $ENTITY.
28 * It cannot be used to update existing $ENTITIES; use the `Update` or `Replace` actions for that.
30 class BasicCreateAction
extends AbstractCreateAction
{
34 * Function(array $item, BasicCreateAction $thisAction): array
39 * Basic Create constructor.
41 * @param string $entityName
42 * @param string $actionName
43 * @param callable $setter
45 public function __construct($entityName, $actionName, $setter = NULL) {
46 parent
::__construct($entityName, $actionName);
47 $this->setter
= $setter;
51 * We pass the writeRecord function an array representing one item to write.
52 * We expect to get the same format back.
54 * @param \Civi\Api4\Generic\Result $result
56 public function _run(Result
$result) {
57 $this->formatWriteValues($this->values
);
58 $this->validateValues();
59 $result->exchangeArray([$this->writeRecord($this->values
)]);
63 * This Basic Create class can be used in one of two ways:
65 * 1. Use this class directly by passing a callable ($setter) to the constructor.
66 * 2. Extend this class and override this function.
68 * Either way, this function should return an array representing the one new object.
72 * @throws \Civi\API\Exception\NotImplementedException
74 protected function writeRecord($item) {
75 if (is_callable($this->setter
)) {
76 $this->addCallbackToDebugOutput($this->setter
);
77 return call_user_func($this->setter
, $item, $this);
79 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());