CsvFile
API reference › CSV Export › CsvFile
CsvFile represents the CSV file.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$cellDelimiter | string | Delimiter between the CSV file cells. Default: ',. | CsvFile |
$enclosure | string | The cell content enclosure. Default: '"'. | CsvFile |
$encoding | string | Output encoding. Input text for UTF-16 export must be UTF-8. Default: 'UTF-8'. | CsvFile |
$entriesCount | int | The count of entries written into the file. Default: 0. | CsvFile |
$name | string | The path of the file. A standalone CSV survives destruction. Default: ''. | CsvFile |
$rowDelimiter | string | Delimiter between the CSV file rows. Default: "\. | CsvFile |
$spreadsheetSafe | bool | Prefixes formula-like text with an apostrophe for spreadsheet import. Default: false. | CsvFile |
$writeBom | bool| | Writes the encoding’s BOM when enabled, or a supplied literal prefix. Default: false. | CsvFile |
$fileHandler protected | resource| | Stream for the active write, or null when no stream is open. Default: null. | CsvFile |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__destruct() | Attempts to finalize an open stream without reporting I/O errors. | CsvFile |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
close() | Finalizes an open CSV and checks that buffered bytes were flushed. | CsvFile |
delete() | Removes the named file and ends the writer’s lifecycle. | CsvFile |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
isComplete() | Indicates that the finalized file can be included in a result. | CsvFile |
open() | Opens the CSV for writing and allows retry after a failed open. | CsvFile |
writeRow() | Writes a complete CSV record. | CsvFile |
composeRowContent() protected | Encodes the cells and prepends a record separator after the first row. | CsvFile |
encodeValue() protected | Converts supported cell types and applies the selected dialect. | CsvFile |
writeContent() protected | Writes the given content into the file. | CsvFile |
Method details
Section titled “Method details”__destruct()
Section titled “__destruct() ”public function __destruct(): mixedAttempts to finalize an open stream without reporting I/O errors.
Returns
mixed
close()
Section titled “close() ”public function close(): boolFinalizes an open CSV and checks that buffered bytes were flushed.
delete()
Section titled “delete() ”public function delete(): boolRemoves the named file and ends the writer’s lifecycle.
Returns
bool: Whether the file is absent after the call.
Throws
yii\base\Exception: if flushing or closing the file fails.
isComplete()
Section titled “isComplete() ”public function isComplete(): boolIndicates that the finalized file can be included in a result.
Returns
bool: Whether the file was closed without a failure and is readable.
open()
Section titled “open() ”public function open(): boolOpens the CSV for writing and allows retry after a failed open.
Returns
bool: Always true.
Throws
LogicException: if the writer was closed or a write failed.yii\base\InvalidConfigException: if the dialect is invalid.yii\base\Exception: if the directory or the file cannot be created.
writeRow()
Section titled “writeRow() ”public function writeRow(array<array-key, mixed>|object $rowData): intWrites a complete CSV record.
Parameters
| Parameter | Type | Description |
|---|---|---|
$rowData | array<array-key, | Cell values: an array, a Traversable or public properties. |
Returns
int: The byte count of the record, without the BOM.
Throws
yii\base\InvalidConfigException: if the dialect, a cell value or the text to transcode is invalid.LogicException: if the writer was closed or a write failed.yii\base\Exception: if the file cannot be opened or written.
composeRowContent()
Section titled “composeRowContent() ”protected function composeRowContent(array<array-key, mixed>|object $rowData): stringEncodes the cells and prepends a record separator after the first row.
Parameters
| Parameter | Type | Description |
|---|---|---|
$rowData | array<array-key, | Cell values: an array, a Traversable or public properties. |
Returns
string: The encoded record.
Throws
yii\base\InvalidConfigException: if a cell value cannot be encoded.
encodeValue()
Section titled “encodeValue() ”protected function encodeValue(mixed $value): stringConverts supported cell types and applies the selected dialect.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | Scalar, null or Stringable cell value. |
Returns
string: The cell text, enclosed and escaped when an enclosure is set. An unquoted cell contains a delimiter or line break.
Throws
yii\base\InvalidConfigException: if the type is unsupported, the spreadsheet-safe check gets non-UTF-8 text, or
writeContent()
Section titled “writeContent() ”protected function writeContent(string $content): intWrites the given content into the file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$content | string | Content to be written. |
Returns
int: The number of bytes written.
Throws
yii\base\Exception: on failure.