(REF) APIv4 BatchAction - Split 'getBatchRecords()' in two
[civicrm-core.git] / Civi / Api4 / Generic / AbstractBatchAction.php
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 */
18
19
20 namespace Civi\Api4\Generic;
21
22 /**
23 * Base class for all batch actions (Update, Delete, Replace).
24 *
25 * This differs from the AbstractQuery class in that the "Where" clause is required.
26 *
27 * @package Civi\Api4\Generic
28 */
29 abstract class AbstractBatchAction extends AbstractQueryAction {
30
31 /**
32 * Criteria for selecting $ENTITIES to process.
33 *
34 * @var array
35 * @required
36 */
37 protected $where = [];
38
39 /**
40 * @var array
41 */
42 private $select;
43
44 /**
45 * BatchAction constructor.
46 * @param string $entityName
47 * @param string $actionName
48 * @param string|array $select
49 * One or more fields to load for each item.
50 */
51 public function __construct($entityName, $actionName, $select = 'id') {
52 $this->select = (array) $select;
53 parent::__construct($entityName, $actionName);
54 }
55
56 /**
57 * Get a list of records for this batch.
58 *
59 * @return array
60 */
61 protected function getBatchRecords() {
62 return (array) $this->getBatchAction()->execute();
63 }
64
65 /**
66 * Get a query which resolves the list of records for this batch.
67 *
68 * This is similar to `getBatchRecords()`, but you may further refine the
69 * API call (e.g. selecting different fields or data-pages) before executing.
70 *
71 * @return \Civi\Api4\Generic\AbstractGetAction
72 */
73 protected function getBatchAction() {
74 $params = [
75 'checkPermissions' => $this->checkPermissions,
76 'where' => $this->where,
77 'orderBy' => $this->orderBy,
78 'limit' => $this->limit,
79 'offset' => $this->offset,
80 ];
81 if (empty($this->reload)) {
82 $params['select'] = $this->select;
83 }
84 return \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4] + $params);
85 }
86
87 /**
88 * @return array
89 */
90 protected function getSelect() {
91 return $this->select;
92 }
93
94 }