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
22 namespace Civi\Api4\Generic
;
24 use Civi\API\Exception\NotImplementedException
;
27 * Basic action for deleting or performing some other task with a set of records. Ex:
29 * $myAction = new BasicBatchAction('Entity', 'action', function($item) {
30 * // Do something with $item
34 * @package Civi\Api4\Generic
36 class BasicBatchAction
extends AbstractBatchAction
{
41 * Function(array $item, BasicBatchAction $thisAction) => array
46 * BasicBatchAction constructor.
48 * @param string $entityName
49 * @param string $actionName
50 * @param string|array $select
51 * One or more fields to select from each matching item.
52 * @param callable $doer
53 * Function(array $item, BasicBatchAction $thisAction) => array
55 public function __construct($entityName, $actionName, $select = 'id', $doer = NULL) {
56 parent
::__construct($entityName, $actionName, $select);
61 * We pass the doTask function an array representing one item to update.
62 * We expect to get the same format back.
64 * @param \Civi\Api4\Generic\Result $result
66 public function _run(Result
$result) {
67 foreach ($this->getBatchRecords() as $item) {
68 $result[] = $this->doTask($item);
73 * This Basic Batch class can be used in one of two ways:
75 * 1. Use this class directly by passing a callable ($doer) to the constructor.
76 * 2. Extend this class and override this function.
78 * Either way, this function should return an array with an output record
83 * @throws \Civi\API\Exception\NotImplementedException
85 protected function doTask($item) {
86 if (is_callable($this->doer
)) {
87 return call_user_func($this->doer
, $item, $this);
89 throw new NotImplementedException('Doer function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());