Commit e3485f86 authored by Tomas Lang's avatar Tomas Lang

Mapper: base namespace as parameter

parent f7c5bdca
...@@ -15,13 +15,27 @@ class Mapper extends \LeanMapper\DefaultMapper ...@@ -15,13 +15,27 @@ class Mapper extends \LeanMapper\DefaultMapper
/** @var array */ /** @var array */
protected $mapping; protected $mapping;
/** @var string */
protected $baseNamespace;
/** /**
* @param array $tableMapping * @param array $tableMapping
*/ */
public function __construct($tableMapping = []) { public function __construct($tableMapping = [], $baseNamespace = 'App\\Model') {
$this->tableMapping = (array) $tableMapping; $this->tableMapping = (array) $tableMapping;
$this->baseNamespace = $baseNamespace;
}
/**
* @return string
*/
public function getBaseNamespace()
{
return $this->baseNamespace;
} }
...@@ -44,7 +58,7 @@ class Mapper extends \LeanMapper\DefaultMapper ...@@ -44,7 +58,7 @@ class Mapper extends \LeanMapper\DefaultMapper
if (isset($this->tableMapping[$tableName])) if (isset($this->tableMapping[$tableName]))
return $this->tableMapping[$tableName]; return $this->tableMapping[$tableName];
else { else {
return self::STANDARD_NAMESPACE . '\\' . implode('\\', array_map('ucfirst', explode('_', $tableName))); return $this->baseNamespace . '\\' . implode('\\', array_map('ucfirst', explode('_', $tableName)));
} }
} }
...@@ -72,10 +86,10 @@ class Mapper extends \LeanMapper\DefaultMapper ...@@ -72,10 +86,10 @@ class Mapper extends \LeanMapper\DefaultMapper
if ($table !== FALSE) if ($table !== FALSE)
return $table; return $table;
else { else {
if (strpos($namespace, self::STANDARD_NAMESPACE) === 0) { if (strpos($namespace, $this->baseNamespace) === 0) {
$namespace = str_replace(self::STANDARD_NAMESPACE, '', $namespace); $namespace = str_replace($this->baseNamespace, '', $namespace);
} else { } else {
throw new \InvalidArgumentException('Unable to get table name for class "' . $class . '". Class need to be in "' . self::STANDARD_NAMESPACE . '" namespace'); throw new \InvalidArgumentException('Unable to get table name for class "' . $class . '". Class need to be in "' . $this->baseNamespace . '" namespace');
} }
return strtolower($this->decamelize(str_replace('\\', '_', ltrim($namespace, '\\')))); return strtolower($this->decamelize(str_replace('\\', '_', ltrim($namespace, '\\'))));
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace LeanMapperWorkflow; namespace LeanMapperWorkflow;
use InvalidArgumentException; use InvalidArgumentException;
use LogicException;
/** /**
* Class Repository * Class Repository
...@@ -125,8 +126,10 @@ class Repository extends AbstractRepository ...@@ -125,8 +126,10 @@ class Repository extends AbstractRepository
public function createRestriction() public function createRestriction()
{ {
$restriction = Mapper::getNamespaceFromClass(get_class($this)) . '\\Restriction'; $restriction = Mapper::getNamespaceFromClass(get_class($this)) . '\\Restriction';
if (!class_exists($restriction))
$restriction = 'FakturacniSystem\\LeanMapperWorkflow\\Restriction'; if (!class_exists($restriction)) {
$restriction = 'LeanMapperWorkflow\\Restriction';
}
return new $restriction($this->mapper); return new $restriction($this->mapper);
} }
......
...@@ -6,13 +6,13 @@ $container = require __DIR__ . '/../bootstrap.php'; ...@@ -6,13 +6,13 @@ $container = require __DIR__ . '/../bootstrap.php';
$mapper = new \LeanMapperWorkflow\Mapper(array( $mapper = new \LeanMapperWorkflow\Mapper(array(
'table' => 'My\\Super\\Namespace' 'table' => 'My\\Super\\Namespace'
)); ), 'MyApp\\Model');
// get namespace from table // get namespace from table
Assert::same('My\\Super\\Namespace', $mapper->getNamespaceFromTable('table')); Assert::same('My\\Super\\Namespace', $mapper->getNamespaceFromTable('table'));
Assert::same('App\\Model\\Other\\Table', $mapper->getNamespaceFromTable('other_table')); Assert::same('MyApp\\Model\\Other\\Table', $mapper->getNamespaceFromTable('other_table'));
Assert::exception(function() use ($mapper) { Assert::exception(function() use ($mapper) {
$mapper->getNamespaceFromTable(''); $mapper->getNamespaceFromTable('');
...@@ -25,11 +25,11 @@ Assert::exception(function() use ($mapper) { ...@@ -25,11 +25,11 @@ Assert::exception(function() use ($mapper) {
Assert::same('table', $mapper->getTableFromClass('My\\Super\\Namespace')); Assert::same('table', $mapper->getTableFromClass('My\\Super\\Namespace'));
Assert::same('table', $mapper->getTableFromClass('My\\Super\\Namespace\\Entity')); Assert::same('table', $mapper->getTableFromClass('My\\Super\\Namespace\\Entity'));
Assert::same('my_another_class', $mapper->getTableFromClass('App\\Model\\My\\Another\\Class')); Assert::same('my_another_class', $mapper->getTableFromClass('MyApp\\Model\\My\\Another\\Class'));
Assert::exception(function() use ($mapper) { Assert::exception(function() use ($mapper) {
$mapper->getTableFromClass('My\\Another\\Class'); $mapper->getTableFromClass('My\\Another\\Class');
}, 'InvalidArgumentException', 'Unable to get table name for class "My\\Another\\Class". Class need to be in "' . \LeanMapperWorkflow\Mapper::STANDARD_NAMESPACE . '" namespace'); }, 'InvalidArgumentException', 'Unable to get table name for class "My\\Another\\Class". Class need to be in "MyApp\\Model" namespace');
Assert::exception(function() use ($mapper) { Assert::exception(function() use ($mapper) {
$mapper->getTableFromClass(''); $mapper->getTableFromClass('');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment