trait FetchModeTrait
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Database/FetchModeTrait.php \Drupal\Core\Database\FetchModeTrait
- 10 core/lib/Drupal/Core/Database/FetchModeTrait.php \Drupal\Core\Database\FetchModeTrait
Provide helper methods for statement fetching.
Hierarchy
- trait \Drupal\Core\Database\FetchModeTrait
4 files declare their use of FetchModeTrait
- PrefetchedResult.php in core/
lib/ Drupal/ Core/ Database/ Statement/ PrefetchedResult.php - Result.php in core/
modules/ mysqli/ src/ Driver/ Database/ mysqli/ Result.php - ResultBase.php in core/
lib/ Drupal/ Core/ Database/ Statement/ ResultBase.php - StatementBase.php in core/
lib/ Drupal/ Core/ Database/ Statement/ StatementBase.php
File
-
core/
lib/ Drupal/ Core/ Database/ FetchModeTrait.php, line 10
Namespace
Drupal\Core\DatabaseView source
trait FetchModeTrait {
/**
* Converts a row of data in associative format to list.
*
* @param array $rowAssoc
* A row of data in associative format.
*
* @return array
* The row in list format.
*/
protected function assocToNum(array $rowAssoc) : array {
return array_values($rowAssoc);
}
/**
* Converts a row of data in associative format to object.
*
* @param array $rowAssoc
* A row of data in associative format.
*
* @return object
* The row in object format.
*/
protected function assocToObj(array $rowAssoc) : \stdClass {
return (object) $rowAssoc;
}
/**
* Converts a row of data in associative format to classed object.
*
* @param array $rowAssoc
* A row of data in associative format.
* @param string $className
* Name of the created class.
* @param array $constructorArguments
* Elements of this array are passed to the constructor.
*
* @return object
* The row in classed object format.
*/
protected function assocToClass(array $rowAssoc, string $className, array $constructorArguments) : object {
$classObj = new $className(...$constructorArguments);
foreach ($rowAssoc as $column => $value) {
$classObj->{$column} = $value;
}
return $classObj;
}
/**
* Converts a row of data in associative format to column.
*
* @param array $rowAssoc
* A row of data in associative format.
* @param string[] $columnNames
* The list of the row columns.
* @param int $columnIndex
* The index of the column to fetch the value of.
*
* @return string
* The value of the column.
*
* @throws \ValueError
* If the column index is not defined.
*/
protected function assocToColumn(array $rowAssoc, array $columnNames, int $columnIndex) : mixed {
if (!isset($columnNames[$columnIndex])) {
throw new \ValueError('Invalid column index');
}
return $rowAssoc[$columnNames[$columnIndex]];
}
/**
* Converts a row of data in associative format to a specified format.
*
* @param array $rowAssoc
* A row of data in FetchAs::Associative format.
* @param \Drupal\Core\Database\Statement\FetchAs $mode
* The target target mode.
* @param array $fetchOptions
* The fetch mode options.
*
* @return array<scalar|null>|object|scalar|null|false
* The data in the target mode.
*
* @throws \ValueError
* If the column index is not defined.
*/
protected function assocToFetchMode(array $rowAssoc, FetchAs $mode, array $fetchOptions) : array|object|int|float|string|bool|null {
return match ($mode) { FetchAs::Associative => $rowAssoc,
FetchAs::ClassObject => $this->assocToClass($rowAssoc, $fetchOptions['class'], $fetchOptions['constructor_args']),
FetchAs::Column => $this->assocToColumn($rowAssoc, array_keys($rowAssoc), $fetchOptions['column']),
FetchAs::List => $this->assocToNum($rowAssoc),
FetchAs::Object => $this->assocToObj($rowAssoc),
};
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary |
|---|---|---|---|
| FetchModeTrait::assocToClass | protected | function | Converts a row of data in associative format to classed object. |
| FetchModeTrait::assocToColumn | protected | function | Converts a row of data in associative format to column. |
| FetchModeTrait::assocToFetchMode | protected | function | Converts a row of data in associative format to a specified format. |
| FetchModeTrait::assocToNum | protected | function | Converts a row of data in associative format to list. |
| FetchModeTrait::assocToObj | protected | function | Converts a row of data in associative format to object. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.