Merge pull request #15353 from totten/master-api4-header
[civicrm-core.git] / Civi / Api4 / Generic / BasicGetAction.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29/**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36
37
19b53e5b
C
38namespace Civi\Api4\Generic;
39
40use Civi\API\Exception\NotImplementedException;
41
42/**
43 * Retrieve items based on criteria specified in the 'where' param.
44 *
45 * Use the 'select' param to determine which fields are returned, defaults to *.
46 */
47class BasicGetAction extends AbstractGetAction {
48 use Traits\ArrayQueryActionTrait;
49
50 /**
51 * @var callable
52 *
53 * Function(BasicGetAction $thisAction) => array<array>
54 */
55 private $getter;
56
57 /**
58 * Basic Get constructor.
59 *
60 * @param string $entityName
61 * @param string $actionName
62 * @param callable $getter
63 */
64 public function __construct($entityName, $actionName, $getter = NULL) {
65 parent::__construct($entityName, $actionName);
66 $this->getter = $getter;
67 }
68
69 /**
70 * Fetch results from the getter then apply filter/sort/select/limit.
71 *
72 * @param \Civi\Api4\Generic\Result $result
73 */
74 public function _run(Result $result) {
75 $this->setDefaultWhereClause();
76 $values = $this->getRecords();
77 $result->exchangeArray($this->queryArray($values));
78 }
79
80 /**
81 * This Basic Get class is a general-purpose api for non-DAO-based entities.
82 *
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.
86 *
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.
90 *
91 * Either way, this function should return an array of arrays, each representing one retrieved object.
92 *
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.
95 *
96 * Sometimes however that may not be practical for performance reasons.
97 * To optimize your getter, it can use the following helpers from $this:
98 *
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.
102 *
103 * Use $this->_isFieldSelected() to check if a field value is called for - useful
104 * if loading the field involves expensive calculations.
105 *
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.
108 *
109 * @return array
110 * @throws \Civi\API\Exception\NotImplementedException
111 */
112 protected function getRecords() {
113 if (is_callable($this->getter)) {
114 return call_user_func($this->getter, $this);
115 }
116 throw new NotImplementedException('Getter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
117 }
118
119}