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