/usr/share/php/propel/parser/PropelCSVParser.php is in php-propel-runtime 1.6.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | <?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
/**
* CSV parser. Converts data between associative array and CSV formats.
* CSV parsing code borrowed from php-csv-utils by Luke Visinoni
* http://code.google.com/p/php-csv-utils/
*
* @author Francois Zaninotto
* @package propel.runtime.parser
*/
class PropelCSVParser extends PropelParser
{
const QUOTE_NONE = 0;
const QUOTE_ALL = 1;
const QUOTE_NONNUMERIC = 2;
const QUOTE_MINIMAL = 3;
// these settings are predefined for Excel CSV format
public $delimiter = ',';
public $lineTerminator = "\r\n";
public $quotechar = '"';
public $escapechar = "\\";
public $quoting = self::QUOTE_MINIMAL;
/**
* Converts data from an associative array to CSV.
*
* @param array $array Source data to convert
* @param boolean $isList Whether the input data contains more than one row
* @param boolean $includeHeading Whether the output should contain a heading line
*
* @return string Converted data, as a CSV string
*/
public function fromArray($array, $isList = false, $includeHeading = true)
{
$rows = array();
if ($isList) {
if ($includeHeading) {
$rows[] = implode($this->formatRow(array_keys(reset($array))), $this->delimiter);
}
foreach ($array as $row) {
$rows[] = implode($this->formatRow($row), $this->delimiter);
}
} else {
if ($includeHeading) {
$rows[] = implode($this->formatRow(array_keys($array)), $this->delimiter);
}
$rows[] = implode($this->formatRow($array), $this->delimiter);
}
return implode($rows, $this->lineTerminator) . $this->lineTerminator;
}
public function listFromArray($array)
{
return $this->fromArray($array, true);
}
/**
* Accepts a row of data and returns it formatted
*
* @param array $row An array of data to be formatted for output to the file
* @return array The formatted array
*/
protected function formatRow($row)
{
foreach ($row as &$column) {
if (!is_scalar($column)) {
$column = $this->serialize($column);
}
switch ($this->quoting) {
case self::QUOTE_NONE:
// do nothing... no quoting is happening here
break;
case self::QUOTE_ALL:
$column = $this->quote($this->escape($column));
break;
case self::QUOTE_NONNUMERIC:
if (preg_match("/[^0-9]/", $column)) {
$column = $this->quote($this->escape($column));
}
break;
case self::QUOTE_MINIMAL:
default:
if ($this->containsSpecialChars($column)) {
$column = $this->quote($this->escape($column));
}
break;
}
}
return $row;
}
/**
* Escapes a column (escapes quotechar with escapechar)
*
* @param string $input A single value to be escaped for output
* @return string Escaped input value
*/
protected function escape($input)
{
return str_replace(
$this->quotechar,
$this->escapechar . $this->quotechar,
$input
);
}
/**
* Quotes a column with quotechar
*
* @param string $input A single value to be quoted for output
* @return string Quoted input value
*/
protected function quote($input)
{
return $this->quotechar . $input . $this->quotechar;
}
/**
* Returns true if input contains quotechar, delimiter or any of the characters in lineTerminator
*
* @param string $input A single value to be checked for special characters
* @return boolean True if contains any special characters
*/
protected function containsSpecialChars($input)
{
$special_chars = str_split($this->lineTerminator, 1);
$special_chars[] = $this->quotechar;
$special_chars[] = $this->delimiter;
foreach ($special_chars as $char) {
if (strpos($input, $char) !== false) {
return true;
}
}
return false;
}
/**
* Serializes a value to place it into a CSV output
*
* @param mixed $input
* @return string
*/
protected function serialize($input)
{
return serialize($input);
}
/**
* Alias for PropelCSVParser::fromArray()
*
* @param array $array Source data to convert
* @param boolean $isList Whether the input data contains more than one row
* @param boolean $includeHeading Whether the output should contain a heading line
*
* @return string Converted data, as a CSV string
*/
public function toCSV($array, $isList = false, $includeHeading = true)
{
return $this->fromArray($array, $isList, $includeHeading);
}
/**
* Converts data from CSV to an associative array.
*
* @param string $data Source data to convert, as a CSV string
* @param boolean $isList Whether the input data contains more than one row
* @param boolean $includeHeading Whether the input contains a heading line
*
* @return array Converted data
*/
public function toArray($data, $isList = false, $includeHeading = true)
{
$rows = explode($this->lineTerminator, $data);
if ($includeHeading) {
$heading = array_shift($rows);
$keys = explode($this->delimiter, $heading);
} else {
$keys = range(0, count($this->getColumns($rows[0])) - 1);
}
if ($isList) {
$array = array();
foreach ($rows as $row) {
$values = $this->cleanupRow($this->getColumns($row));
if ($values !== array()) {
$array []= array_combine($keys, $values);
}
}
} else {
$values = $this->cleanupRow($this->getColumns(array_shift($rows)));
if ($keys === array('') && $values === array()) {
$array = array();
} else {
if (count($keys) > count($values)) {
// empty values at the end of the row are not match bu the getColumns() regexp
$values = array_pad($values, count($keys), null);
}
$array = array_combine($keys, $values);
}
}
return $array;
}
public function listToArray($array)
{
return $this->toArray($array, true);
}
protected function getColumns($row)
{
$delim = preg_quote($this->delimiter, '/');
preg_match_all('/(".+?"|[^' . $delim . ']+)(' . $delim . '|$)/', $row, $matches);
return $matches[1];
}
/**
* Accepts a formatted row of data and returns it raw
*
* @param array An array of data from a CSV output
* @return array The cleaned up array
*/
protected function cleanupRow($row)
{
foreach ($row as $key => $column) {
if ($this->isQuoted($column)) {
$column = $this->unescape($this->unquote($column));
}
if ($this->isSerialized($column)) {
$column = $this->unserialize($column);
}
if ($column === 'N;') {
$column = null;
}
$row[$key] = $column;
}
return $row;
}
protected function isQuoted($input)
{
$quote = preg_quote($this->quotechar, '/');
return preg_match('/^' . $quote . '.*' . $quote . '$/', $input);
}
protected function unescape($input)
{
return str_replace(
$this->escapechar . $this->quotechar,
$this->quotechar,
$input
);
}
protected function unquote($input)
{
return trim($input, $this->quotechar);
}
/**
* Checks whether a value from CSV output is serialized
*/
protected function isSerialized($input)
{
return preg_match('/^\w\:\d+\:\{/', $input);
}
/**
* Unserializes a value from CSV output
*
* @param string $input
* @return mixed
*/
protected function unserialize($input)
{
return unserialize($input);
}
/**
* Alias for PropelCSVParser::toArray()
*
* @param string $data Source data to convert, as a CSV string
* @param boolean $isList Whether the input data contains more than one row
* @param boolean $includeHeading Whether the input contains a heading line
*
* @return array Converted data
*/
public function fromCSV($data, $isList = false, $includeHeading = true)
{
return $this->toArray($data, $isList, $includeHeading);
}
}
|