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 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
21 | ||
19b53e5b C |
22 | namespace Civi\Api4\Generic; |
23 | ||
24 | use Civi\API\Exception\NotImplementedException; | |
25 | ||
26 | /** | |
27 | * Create a new object from supplied values. | |
28 | * | |
29 | * This function will create 1 new object. It cannot be used to update existing objects. Use the Update or Replace actions for that. | |
30 | */ | |
31 | class BasicCreateAction extends AbstractCreateAction { | |
32 | ||
33 | /** | |
34 | * @var callable | |
35 | * | |
36 | * Function(array $item, BasicCreateAction $thisAction) => array | |
37 | */ | |
38 | private $setter; | |
39 | ||
40 | /** | |
41 | * Basic Create constructor. | |
42 | * | |
43 | * @param string $entityName | |
44 | * @param string $actionName | |
45 | * @param callable $setter | |
46 | * Function(array $item, BasicCreateAction $thisAction) => array | |
47 | */ | |
48 | public function __construct($entityName, $actionName, $setter = NULL) { | |
49 | parent::__construct($entityName, $actionName); | |
50 | $this->setter = $setter; | |
51 | } | |
52 | ||
53 | /** | |
54 | * We pass the writeRecord function an array representing one item to write. | |
55 | * We expect to get the same format back. | |
56 | * | |
57 | * @param \Civi\Api4\Generic\Result $result | |
58 | */ | |
59 | public function _run(Result $result) { | |
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)) { | |
78 | return call_user_func($this->setter, $item, $this); | |
79 | } | |
80 | throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); | |
81 | } | |
82 | ||
83 | } |