File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/Kms/Transfer/Models/ColumnMap.php
<?php
namespace App\KommaApp\Kms\Transfer\Models;
use Illuminate\Database\Eloquent\Model;
class ColumnMap
{
/** @var string $modelFQCN */
private $modelFQCN;
/** @var mixed $modelFactoryMethod */
private $modelFactoryMethod;
/** @var string $modelAttributeName */
private $modelAttributeName;
/** @var mixed $mutator */
private $mutator;
/** @var string $columnKey */
private $columnKey;
/**
* ColumnMap constructor.
* @param string $columnName
*/
public function __construct(string $columnName)
{
$this->modelFQCN = '';
$this->modelAttributeName = '';
$this->columnKey = $columnName;
}
/**
* @return string
*/
public function getModelFQCN(): string
{
return $this->modelFQCN;
}
/**
* Sets the model fully qualified class name so that the importer knows which model to make for the import
*
* @param string $modelFQCN
* @return ColumnMap
*/
public function setModelFQCN(string $modelFQCN)
{
if(!is_subclass_of($modelFQCN, Model::class)) throw new \InvalidArgumentException('The given model name of "'.$modelFQCN.'" is no child of "'.Model::class.'" while it should be.');
$this->modelFQCN = $modelFQCN;
return $this;
}
/**
* @return string
*/
public function getModelAttributeName(): string
{
return $this->modelAttributeName;
}
/**
* Sets the attribute in which the columns value must be set.
* Only relevant when the setModelFQCN method was used and not for the setModelFactory
*
* @param string $modelAttributeName
* @return ColumnMap
*/
public function setModelAttributeName(string $modelAttributeName): ColumnMap
{
$this->modelAttributeName = $modelAttributeName;
return $this;
}
/**
* @return mixed
*/
public function getMutator()
{
return $this->mutator;
}
/**
* The mutator (a callable) transforms the column value to something else before passing it into the model or factory
*
* @param mixed $mutator
* @return ColumnMap
*/
public function setMutator($mutator): ColumnMap
{
if(!is_callable($mutator)) throw new \InvalidArgumentException('The mutator must be a callable');
$this->mutator = $mutator;
return $this;
}
/**
* @return mixed
*/
public function getModelFactoryMethod()
{
return $this->modelFactoryMethod;
}
/**
* Sets a factory function that is responsible for creating the model.
* The factory must have the following signature parameters: ColumnMap $columnMap, string $value, int $rowNumber
*
* @param mixed $modelFactoryMethod
* @return ColumnMap
*/
public function setModelFactory($modelFactoryMethod): ColumnMap
{
if(!is_callable($modelFactoryMethod)) throw new \InvalidArgumentException('The model factory method must be a callable');
try {
$reflectionFunction = new \ReflectionFunction($modelFactoryMethod);
} catch (\ReflectionException $e) {
throw new \InvalidArgumentException('Could not reflect the the factory method');
}
if($reflectionFunction->getNumberOfRequiredParameters() !== 3) throw new \InvalidArgumentException('The model factory method must accept 3 arguments: ColumnMap $columnMap, string $value, int $rowNumber');
$this->modelFactoryMethod = $modelFactoryMethod;
return $this;
}
/**
* Gets the columns name / key
*
* @return string
*/
public function getColumnKey(): string
{
return $this->columnKey;
}
}