class CSV in Apigee Edge 8
CSV formatter for Apigee Edge field storage.
@ApigeeFieldStorageFormat( id = "csv", label = "CSV", fields = { "boolean", "float", "integer", "decimal", "list_float", "list_integer", "list_string", "string", "string_long", "email", "timestamp" }, weight = -1, )
This class uses an internal method to generate the CSV file that builds on the CSV functions in PHP. This is because the CsvEncoder in Symfony always writes the header.
Hierarchy
- class \Drupal\apigee_edge\Plugin\ApigeeFieldStorageFormat\CSV implements FieldStorageFormatInterface
Expanded class hierarchy of CSV
See also
https://github.com/symfony/symfony/issues/27447
1 file declares its use of CSV
- DeveloperSyncTest.php in tests/
src/ Functional/ DeveloperSyncTest.php
File
- src/
Plugin/ ApigeeFieldStorageFormat/ CSV.php, line 52
Namespace
Drupal\apigee_edge\Plugin\ApigeeFieldStorageFormatView source
class CSV implements FieldStorageFormatInterface {
/**
* {@inheritdoc}
*/
public function encode(array $data) : string {
$values = array_map(function (array $item) {
return $item['value'];
}, $data);
return trim($this
->writeCommaSeparatedValues([
$values,
]));
}
/**
* {@inheritdoc}
*/
public function decode(string $data) : array {
$data = trim($data) . PHP_EOL;
$values = $this
->readCommaSeparatedValues($data);
$result = [];
$it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($values));
foreach ($it as $v) {
$result[] = [
'value' => $v,
];
}
return $result;
}
/**
* Writes the CSV data into a string.
*
* @param array $data
* CSV data.
*
* @return string
* Encoded CSV.
*/
protected function writeCommaSeparatedValues(array $data) : string {
$handle = fopen("php://temp", "w+");
foreach ($data as $row) {
fputcsv($handle, $row);
}
rewind($handle);
$value = stream_get_contents($handle);
fclose($handle);
return $value;
}
/**
* Reads the CSV data from a string.
*
* @param string $data
* CSV data.
*
* @return array
* Decoded CSV.
*/
protected function readCommaSeparatedValues(string $data) : array {
$handle = fopen("php://temp", "r+");
fwrite($handle, $data);
rewind($handle);
$result = [];
while ($cols = fgetcsv($handle, 0)) {
$result[] = $cols;
}
fclose($handle);
return $result;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CSV:: |
public | function |
Decodes field data from the target format. Overrides FieldStorageFormatInterface:: |
|
CSV:: |
public | function |
Encodes field data to the target format. Overrides FieldStorageFormatInterface:: |
|
CSV:: |
protected | function | Reads the CSV data from a string. | |
CSV:: |
protected | function | Writes the CSV data into a string. |