osb/app/Traits/Import.php
2022-06-14 16:55:17 +10:00

43 lines
962 B
PHP

<?php
/**
* Import from files
*/
namespace App\Traits;
use Illuminate\Support\Collection;
trait Import
{
protected Collection $_columns;
/**
* Return the columns from the file that we'll work with.
* This creates an index of the position of each header in the file, which we use to find an value by getColumnKey()
*
* @param string $line
* @return Collection
* @throws \Exception
*/
private function setColumns(string $line): Collection
{
// If columns is not set, then this is an error
if (! $this->columns)
throw new \Exception('ERROR: Columns must be set before calling setColumns()');
$this->_columns = collect(explode(',',strtolower($line)))->filter();
return $this->_columns->intersect($this->columns);
}
/**
* Get the index for the column in the file
*
* @param string $key
* @return mixed
*/
private function getColumnKey(string $key)
{
return $this->_columns->search($this->columns->get($key));
}
}