Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
41498ac5 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
41498ac5 TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | */ |
18 | ||
19 | ||
19b53e5b C |
20 | namespace Civi\Api4\Generic; |
21 | ||
22 | use Civi\API\Exception\NotImplementedException; | |
23 | ||
24 | /** | |
fc95d9a5 | 25 | * Create a new $ENTITY from supplied values. |
19b53e5b | 26 | * |
fc95d9a5 | 27 | * This action will create 1 new $ENTITY. |
e3c6d5ff | 28 | * It cannot be used to update existing $ENTITIES; use the `Update` or `Replace` actions for that. |
19b53e5b C |
29 | */ |
30 | class BasicCreateAction extends AbstractCreateAction { | |
31 | ||
32 | /** | |
33 | * @var callable | |
9bafff7c | 34 | * Function(array $item, BasicCreateAction $thisAction): array |
19b53e5b C |
35 | */ |
36 | private $setter; | |
37 | ||
38 | /** | |
39 | * Basic Create constructor. | |
40 | * | |
41 | * @param string $entityName | |
42 | * @param string $actionName | |
43 | * @param callable $setter | |
19b53e5b C |
44 | */ |
45 | public function __construct($entityName, $actionName, $setter = NULL) { | |
46 | parent::__construct($entityName, $actionName); | |
47 | $this->setter = $setter; | |
48 | } | |
49 | ||
50 | /** | |
51 | * We pass the writeRecord function an array representing one item to write. | |
52 | * We expect to get the same format back. | |
53 | * | |
54 | * @param \Civi\Api4\Generic\Result $result | |
55 | */ | |
56 | public function _run(Result $result) { | |
961e974c | 57 | $this->formatWriteValues($this->values); |
19b53e5b C |
58 | $this->validateValues(); |
59 | $result->exchangeArray([$this->writeRecord($this->values)]); | |
60 | } | |
61 | ||
62 | /** | |
63 | * This Basic Create class can be used in one of two ways: | |
64 | * | |
65 | * 1. Use this class directly by passing a callable ($setter) to the constructor. | |
66 | * 2. Extend this class and override this function. | |
67 | * | |
68 | * Either way, this function should return an array representing the one new object. | |
69 | * | |
70 | * @param array $item | |
71 | * @return array | |
72 | * @throws \Civi\API\Exception\NotImplementedException | |
73 | */ | |
74 | protected function writeRecord($item) { | |
75 | if (is_callable($this->setter)) { | |
32f72d83 | 76 | $this->addCallbackToDebugOutput($this->setter); |
19b53e5b C |
77 | return call_user_func($this->setter, $item, $this); |
78 | } | |
79 | throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); | |
80 | } | |
81 | ||
82 | } |