avoid crash with one column and blank lines
[civicrm-core.git] / Civi / Api4 / Query / SqlField.php
CommitLineData
3176b04c
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12namespace Civi\Api4\Query;
13
80aabc65
CW
14use Civi\API\Exception\UnauthorizedException;
15
3176b04c
CW
16/**
17 * Sql column expression
18 */
19class SqlField extends SqlExpression {
20
7ce7b1cd
CW
21 public $supportsExpansion = TRUE;
22
3176b04c 23 protected function initialize() {
19fde02c
CW
24 if ($this->alias && $this->alias !== $this->expr) {
25 throw new \API_Exception("Aliasing field names is not allowed, only expressions can have an alias.");
26 }
c9e3ae2e 27 $this->fields[] = $this->expr;
3176b04c
CW
28 }
29
30 public function render(array $fieldList): string {
80aabc65 31 if (!isset($fieldList[$this->expr])) {
c9e3ae2e 32 throw new \API_Exception("Invalid field '{$this->expr}'");
3176b04c 33 }
80aabc65
CW
34 if ($fieldList[$this->expr] === FALSE) {
35 throw new UnauthorizedException("Unauthorized field '{$this->expr}'");
36 }
9c961a3a
CW
37 if (!empty($fieldList[$this->expr]['sql_renderer'])) {
38 $renderer = $fieldList[$this->expr]['sql_renderer'];
39 return $renderer($fieldList[$this->expr]);
40 }
c9e3ae2e 41 return $fieldList[$this->expr]['sql_name'];
3176b04c
CW
42 }
43
44}