Initial support for importing supplier costs
This commit is contained in:
43
app/Traits/Import.php
Normal file
43
app/Traits/Import.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user