protected function getRecords() {
$entities = [];
$toGet = $this->_itemsToGet('name');
+ $getDocs = $this->_isFieldSelected('description', 'comment', 'see');
$locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')],
array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
);
&& is_a('\Civi\Api4\\' . $matches[1], '\Civi\Api4\Generic\AbstractEntity', TRUE)
) {
$entity = ['name' => $matches[1]];
- if ($this->_isFieldSelected('description') || $this->_isFieldSelected('comment') || $this->_isFieldSelected('see')) {
+ if ($getDocs) {
$this->addDocs($entity);
}
$entities[$matches[1]] = $entity;
$action = ActionUtil::getAction($this->getEntityName(), $actionName);
if (is_object($action)) {
$this->_actions[$actionName] = ['name' => $actionName];
- if ($this->_isFieldSelected('description') || $this->_isFieldSelected('comment')) {
+ if ($this->_isFieldSelected('description', 'comment')) {
$actionReflection = new \ReflectionClass($action);
$actionInfo = ReflectionUtils::getCodeDocs($actionReflection);
unset($actionInfo['method']);
}
/**
- * Helper to see if a field should be selected by the getRecords function.
+ * Helper to see if field(s) should be selected by the getRecords function.
*
* Checks the SELECT, WHERE and ORDER BY params to see what fields are needed.
*
* Note that if no SELECT clause has been set then all fields should be selected
* and this function will always return TRUE.
*
- * @param string $field
+ * @param string ...$fieldNames
+ * One or more field names to check (uses OR if multiple)
* @return bool
+ * Returns true if any given fields are in use.
*/
- protected function _isFieldSelected($field) {
- if (!$this->select || in_array($field, $this->select) || isset($this->orderBy[$field])) {
+ protected function _isFieldSelected(string ...$fieldNames) {
+ if (!$this->select || array_intersect($fieldNames, array_merge($this->select, array_keys($this->orderBy)))) {
return TRUE;
}
- return $this->_whereContains($field);
+ return $this->_whereContains($fieldNames);
}
/**
- * Walk through the where clause and check if a field is in use.
+ * Walk through the where clause and check if field(s) are in use.
*
- * @param string $field
+ * @param string|array $fieldName
+ * A single fieldName or an array of names (uses OR if multiple)
* @param array $clauses
* @return bool
+ * Returns true if any given fields are found in the where clause.
*/
- protected function _whereContains($field, $clauses = NULL) {
+ protected function _whereContains($fieldName, $clauses = NULL) {
if ($clauses === NULL) {
$clauses = $this->where;
}
+ $fieldName = (array) $fieldName;
foreach ($clauses as $clause) {
if (is_array($clause) && is_string($clause[0])) {
- if ($clause[0] == $field) {
+ if (in_array($clause[0], $fieldName)) {
return TRUE;
}
elseif (is_array($clause[1])) {
- return $this->_whereContains($field, $clause[1]);
+ return $this->_whereContains($fieldName, $clause[1]);
}
}
}
// If no "select" is set, should always return true
$this->assertTrue($isFieldSelected->invoke($get, 'color'));
$this->assertTrue($isFieldSelected->invoke($get, 'shape'));
- $this->assertTrue($isFieldSelected->invoke($get, 'size'));
+ $this->assertTrue($isFieldSelected->invoke($get, 'size', 'color', 'shape'));
// With a non-empty "select" fieldsToSelect() will return fields needed to evaluate each clause.
$get->addSelect('id');
- $this->assertTrue($isFieldSelected->invoke($get, 'color'));
+ $this->assertTrue($isFieldSelected->invoke($get, 'color', 'shape', 'size'));
$this->assertTrue($isFieldSelected->invoke($get, 'id'));
- $this->assertFalse($isFieldSelected->invoke($get, 'shape'));
- $this->assertFalse($isFieldSelected->invoke($get, 'size'));
- $this->assertFalse($isFieldSelected->invoke($get, 'weight'));
+ $this->assertFalse($isFieldSelected->invoke($get, 'shape', 'size', 'weight'));
$this->assertFalse($isFieldSelected->invoke($get, 'group'));
$get->addClause('OR', ['shape', '=', 'round'], ['AND', [['size', '=', 'big'], ['weight', '!=', 'small']]]);
$this->assertTrue($isFieldSelected->invoke($get, 'id'));
$this->assertTrue($isFieldSelected->invoke($get, 'shape'));
$this->assertTrue($isFieldSelected->invoke($get, 'size'));
- $this->assertTrue($isFieldSelected->invoke($get, 'weight'));
+ $this->assertTrue($isFieldSelected->invoke($get, 'group', 'weight'));
$this->assertFalse($isFieldSelected->invoke($get, 'group'));
$get->addOrderBy('group');