Skip to content

Instantly share code, notes, and snippets.

@AnrDaemon
Last active August 14, 2018 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnrDaemon/6e60c43d8b65ef896e3da019bf5863a1 to your computer and use it in GitHub Desktop.
Save AnrDaemon/6e60c43d8b65ef896e3da019bf5863a1 to your computer and use it in GitHub Desktop.
CSV reader
<?php
return [
'ru.title',
'series',
'title',
'ru.name',
'ru.genitive',
'usage',
'setCount',
'packCount',
'behavior',
'formula',
'sampleFrom',
'sampleTo',
'sampleUnit',
'sampleStrokes',
'sampleAdjustFactor',
'sampleTime',
'sampleMinimum',
'colorFrom',
'colorTo',
'deviationFrom',
'deviationTo',
'deviationLimit',
'shelfLife',
'reactionPrinciple',
'dimensions',
'pdk',
'explosive',
'dangerClass',
'M',
'mgm2ppm',
'ppm2mgm',
'mgm2pvol',
'res4',
'res5',
'res6',
'toxicity',
'measuringCaveats',
'comment',
];
<?php
class MyItem
implements \ArrayAccess
{
const intDefs = [
];
protected $params = [
'name' => null,
'series' => null,
'title' => null,
'usage' => null,
'setCount' => null,
'packCount' => null,
'behavior' => null,
'formula' => null,
'sampling' => [],
/*'sampleFrom',
'sampleTo',
'sampleUnit',
'sampleStrokes',
'sampleAdjustFactor',
'sampleTime',*/
'samplingLimits' => [],
//'sampleMinimum',
'colorFrom' => null,
'colorTo' => null,
'deviation' => [],
/*'deviationFrom',
'deviationTo',
'deviationLimit',*/
'shelfLife' => null,
'reactionPrinciple' => null,
'pdk' => null,
'explosive' => null,
'multisampling' => false,
];
protected function offsetWritable($offset)
{
static $map = [
'id' => true,
];
return empty($map[$offset]);
}
public function __construct(array $row)
{
$this->params = [
'name' => $row['ru.name'],
'genitive' => $row['ru.genitive'],
'series' => $row['series'],
'title' => $row['title'],
'usage' => $row['usage'] ?: 'sampling',
'setCount' => $row['setCount'] ?: '1',
'packCount' => $row['packCount'],
'dimensions' => $row['dimensions'],
'behavior' => $row['behavior'],
'unit' => $row['sampleUnit'],
'formula' => $row['formula'],
'scaled' => false,
'sampling' => [],
'unit' => $row['sampleUnit'] ?: 'ppm',
'limits' => [],
'colorFrom' => $row['colorFrom'],
'colorTo' => $row['colorTo'],
'deviation' => [],
'shelfLife' => $row['shelfLife'],
'dangerClass' => $row['dangerClass'],
'reactionPrinciple' => $row['reactionPrinciple'],
'pdk' => $row['pdk'],
'explosive' => $row['explosive'],
'toxicity' => $row['toxicity'],
'M' => $row['M'],
'mgm2ppm' => $row['mgm2ppm'],
'ppm2mgm' => $row['ppm2mgm'],
'mgm2pvol' => $row['mgm2pvol'],
'comment' => $row['comment'],
'res4' => $row['res4'],
'res5' => $row['res5'],
'res6' => $row['res6'],
'multisampling' => $row['usage'] == 'multisampling',
];
$this->merge($row);
}
public function merge(array $row)
{
if(!empty($row['sampleFrom']) || !empty($row['formula']))
{
$this->params['sampling'][] = [
'formula' => $row['formula'],
'from' => $row['sampleFrom'],
'to' => $row['sampleTo'],
'unit' => $row['sampleUnit'] ?: $this->params['unit'],
'strokes' => $row['sampleStrokes'],
'adj' => $row['sampleAdjustFactor'],
'time' => $row['sampleTime'],
'colorFrom' => $row['colorFrom'],
'colorTo' => $row['colorTo'],
];
}
if(!empty($row['sampleMinimum']))
{
$this->params['limits'][] = [
'from' => $row['sampleFrom'],
'to' => $row['sampleTo'],
'unit' => $row['sampleUnit'] ?: $this->params['unit'],
'min' => $row['sampleMinimum'],
'strokes' => $row['sampleStrokes'],
];
}
if(!empty($row['deviationTo']))
{
$this->params['deviation'][] = [
'from' => $row['deviationFrom'],
'to' => $row['deviationTo'],
'unit' => $row['sampleUnit'] ?: $this->params['unit'],
'limit' => $row['deviationLimit'],
];
}
}
public function transform()
{
$this->params['range'] = [
'min' => reset($this->params['sampling'])['from'],
'max' => end($this->params['sampling'])['to'],
];
if(
$this->params['usage'] === 'sampling' &&
count($this->params['sampling']) > 1 &&
$this->params['sampling'][0]['from'] == $this->params['sampling'][1]['from'] &&
$this->params['sampling'][0]['to'] == $this->params['sampling'][1]['to']
)
{
$this->params['scaled'] = true;
}
if($this->params['usage'] === 'multisampling')
{
$this->params['multisampling'] = true;
$this->params['usage'] = 'sampling';
}
//if(count(array_unique(array_column())))
}
// ArrayAccess
public function offsetSet($offset, $value)
{
if($this instanceof \Iterator)
{
if(isset($offset))
$this->params[$offset] = $value;
else
$this->params[] = $value;
}
elseif(array_key_exists($offset, $this->params) && $this->offsetWritable($offset))
{
if(empty($value))
{
unset($this[$offset]);
}
else
{
$this->params[$offset] = $value;
}
}
else
{
// Not a list(not iteratable) - deny unknown properties
throw new \OutOfRangeException("Unable to modify property '$offset'.");
}
}
public function offsetExists($offset)
{
return isset($this->params[$offset]);
}
public function offsetGet($offset)
{
return $this->params[$offset];
}
public function offsetUnset($offset)
{
if($this instanceof \Iterator)
{
unset($this->params[$offset]);
}
elseif(array_key_exists($offset, $this->params) && $this->offsetWritable($offset))
{
$this->params[$offset] = static::intDefs[$offset] ?? '';
}
}
}
<?php
$fieldNames = require __DIR__ . '/fieldNames.php';
$file = new \SplFileObject('impuls.csv', 'rb');
$file->setCsvControl(",", "\"", "\\");
$file->setFlags(\SplFileObject::READ_CSV);
$list = [];
$item = null;
foreach($file as $i => $line)
{
if(!isset($line[0]))
break;
if($i < 2)
continue;
if(strlen($line[0]) && $line[0][0] === '#')
continue;
$row = array_combine($fieldNames, $line);
if(empty($line[0]))
{
$item->merge($row);
}
else
{
$list[] = $item = new MyItem($row);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment