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
20 namespace Civi\Api4\Generic
;
22 use Civi\API\Exception\NotImplementedException
;
25 * $ACTION one or more $ENTITIES.
27 * $ENTITIES are selected based on criteria specified in `where` parameter (required).
29 * @package Civi\Api4\Generic
31 class BasicBatchAction
extends AbstractBatchAction
{
36 * Function(array $item, BasicBatchAction $thisAction) => array
41 * BasicBatchAction constructor.
44 * $myAction = new BasicBatchAction($entityName, $actionName, function($item) {
45 * // Do something with $item
50 * @param string $entityName
51 * @param string $actionName
52 * @param string|array $select
53 * One or more fields to select from each matching item.
54 * @param callable $doer
55 * Function(array $item, BasicBatchAction $thisAction) => array
57 public function __construct($entityName, $actionName, $select = 'id', $doer = NULL) {
58 parent
::__construct($entityName, $actionName, $select);
63 * We pass the doTask function an array representing one item to update.
64 * We expect to get the same format back.
66 * @param \Civi\Api4\Generic\Result $result
68 public function _run(Result
$result) {
69 foreach ($this->getBatchRecords() as $item) {
70 $result[] = $this->doTask($item);
75 * This Basic Batch class can be used in one of two ways:
77 * 1. Use this class directly by passing a callable ($doer) to the constructor.
78 * 2. Extend this class and override this function.
80 * Either way, this function should return an array with an output record
85 * @throws \Civi\API\Exception\NotImplementedException
87 protected function doTask($item) {
88 if (is_callable($this->doer
)) {
89 $this->addCallbackToDebugOutput($this->doer
);
90 return call_user_func($this->doer
, $item, $this);
92 throw new NotImplementedException('Doer function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());