File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/Kms/Transfer/Models/ColumnMap.php
<?php
namespace App\Komma\Kms\Transfer\Models;
use Illuminate\Database\Eloquent\Model;
class ColumnMap
{
/** @var string */
private $modelFQCN;
/** @var mixed */
private $modelFactoryMethod;
/** @var string */
private $modelAttributeName;
/** @var mixed */
private $mutator;
/** @var string */
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): self
{
$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): self
{
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): self
{
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;
}
}