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 | |
34 | * | |
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 callable $setter | |
45 | * Function(array $item, BasicCreateAction $thisAction) => array | |
46 | */ | |
47 | public function __construct($entityName, $actionName, $setter = NULL) { | |
48 | parent::__construct($entityName, $actionName); | |
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) { | |
961e974c | 59 | $this->formatWriteValues($this->values); |
19b53e5b C |
60 | $this->validateValues(); |
61 | $result->exchangeArray([$this->writeRecord($this->values)]); | |
62 | } | |
63 | ||
64 | /** | |
65 | * This Basic Create class can be used in one of two ways: | |
66 | * | |
67 | * 1. Use this class directly by passing a callable ($setter) to the constructor. | |
68 | * 2. Extend this class and override this function. | |
69 | * | |
70 | * Either way, this function should return an array representing the one new object. | |
71 | * | |
72 | * @param array $item | |
73 | * @return array | |
74 | * @throws \Civi\API\Exception\NotImplementedException | |
75 | */ | |
76 | protected function writeRecord($item) { | |
77 | if (is_callable($this->setter)) { | |
32f72d83 | 78 | $this->addCallbackToDebugOutput($this->setter); |
19b53e5b C |
79 | return call_user_func($this->setter, $item, $this); |
80 | } | |
81 | throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); | |
82 | } | |
83 | ||
84 | } |