4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2020 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2020
38 namespace Civi\Api4\Generic
;
40 use Civi\API\Exception\NotImplementedException
;
43 * Retrieve items based on criteria specified in the 'where' param.
45 * Use the 'select' param to determine which fields are returned, defaults to *.
47 class BasicGetAction
extends AbstractGetAction
{
48 use Traits\ArrayQueryActionTrait
;
53 * Function(BasicGetAction $thisAction) => array<array>
58 * Basic Get constructor.
60 * @param string $entityName
61 * @param string $actionName
62 * @param callable $getter
64 public function __construct($entityName, $actionName, $getter = NULL) {
65 parent
::__construct($entityName, $actionName);
66 $this->getter
= $getter;
70 * Fetch results from the getter then apply filter/sort/select/limit.
72 * @param \Civi\Api4\Generic\Result $result
74 public function _run(Result
$result) {
75 $this->setDefaultWhereClause();
76 $values = $this->getRecords();
77 $result->exchangeArray($this->queryArray($values));
81 * This Basic Get class is a general-purpose api for non-DAO-based entities.
83 * Useful for fetching records from files or other places.
84 * You can specify any php function to retrieve the records, and this class will
85 * automatically filter, sort, select & limit the raw data from your callback.
87 * You can implement this action in one of two ways:
88 * 1. Use this class directly by passing a callable ($getter) to the constructor.
89 * 2. Extend this class and override this function.
91 * Either way, this function should return an array of arrays, each representing one retrieved object.
93 * The simplest thing for your getter function to do is return every full record
94 * and allow this class to automatically do the sorting and filtering.
96 * Sometimes however that may not be practical for performance reasons.
97 * To optimize your getter, it can use the following helpers from $this:
99 * Use this->_itemsToGet() to match records to field values in the WHERE clause.
100 * Note the WHERE clause can potentially be very complex and it is not recommended
101 * to parse $this->where yourself.
103 * Use $this->_isFieldSelected() to check if a field value is called for - useful
104 * if loading the field involves expensive calculations.
106 * Be careful not to make assumptions, e.g. if LIMIT 100 is specified and your getter "helpfully" truncates the list
107 * at 100 without accounting for WHERE, ORDER BY and LIMIT clauses, the final filtered result may be very incorrect.
110 * @throws \Civi\API\Exception\NotImplementedException
112 protected function getRecords() {
113 if (is_callable($this->getter
)) {
114 return call_user_func($this->getter
, $this);
116 throw new NotImplementedException('Getter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());