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