3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 namespace Civi\Api4\Generic
;
31 * Container for api results.
33 class Result
extends \ArrayObject
{
51 * Return first result.
54 public function first() {
55 foreach ($this as $values) {
65 public function last() {
66 $items = $this->getArrayCopy();
67 return array_pop($items);
74 public function itemAt($index) {
75 $length = $index < 0 ?
0 - $index : $index +
1;
76 if ($length > count($this)) {
79 return array_slice(array_values($this->getArrayCopy()), $index, 1)[0];
83 * Re-index the results array (which by default is non-associative)
85 * Drops any item from the results that does not contain the specified key
89 * @throws \API_Exception
91 public function indexBy($key) {
92 $this->indexedBy
= $key;
95 foreach ($this as $values) {
96 if (isset($values[$key])) {
97 $newResults[$values[$key]] = $values;
101 throw new \
API_Exception("Key $key not found in api results");
103 $this->exchangeArray($newResults);
109 * Returns the number of results
113 public function count() {
114 $count = parent
::count();
115 if ($count == 1 && is_array($this->first()) && array_keys($this->first()) == ['row_count']) {
116 return $this->first()['row_count'];
122 * Reduce each result to one field
127 public function column($name) {
128 return array_column($this->getArrayCopy(), $name, $this->indexedBy
);