Commit e9c054c2 authored by Tomas Lang's avatar Tomas Lang

Fix coding style

parent 36857561
......@@ -2,17 +2,12 @@
namespace LeanMapperWorkflow;
use InvalidArgumentException;
abstract class AbstractRepository extends \LeanMapper\Repository
{
/**
* @return Restriction
*/
abstract public function createRestriction();
/**
* @param \DibiFluent $statement
* @param Restriction|null $restriction
......@@ -26,12 +21,18 @@ abstract class AbstractRepository extends \LeanMapper\Repository
$restriction = $this->createRestriction();
}
if ($restriction instanceof Restriction)
{
if ($restriction instanceof Restriction) {
$restriction->apply($statement, $this, $flag);
} else {
throw new \InvalidArgumentException('Parameter "restriction" (' . get_class($restriction) . ') must be instance of ' . __NAMESPACE__ . '\\Restriction.');
throw new InvalidArgumentException('Parameter "restriction" (' . get_class($restriction) . ') must be instance of ' . __NAMESPACE__ . '\\Restriction.');
}
}
/**
* @return Restriction
*/
abstract public function createRestriction();
}
......@@ -30,19 +30,22 @@ abstract class AbstractRestriction
$element = new \Nette\Reflection\ClassType(get_class($this));
do
{
do {
$annotations = $element->getAnnotations('property');
if (isset($annotations['property']))
foreach ($annotations['property'] as $property)
if (($parsedProperty = $this->parseProperty($property)) !== NULL && !isset($this->properties[$parsedProperty['variable']]))
if (isset($annotations['property'])) {
foreach ($annotations['property'] as $property) {
if (($parsedProperty = $this->parseProperty($property)) !== NULL && !isset($this->properties[$parsedProperty['variable']])) {
$this->properties[$parsedProperty['variable']] = $parsedProperty['type'];
}
}
}
if ($element instanceof \Nette\Reflection\ClassType)
if ($element instanceof \Nette\Reflection\ClassType) {
$element = $element->getParentClass();
else
} else {
$element = NULL;
}
} while ($element !== NULL);
$this->initDefaults();
......@@ -105,35 +108,32 @@ abstract class AbstractRestriction
*/
public function __set($key, $value)
{
if (!isset($this->properties[$key]))
if (!isset($this->properties[$key])) {
throw new \InvalidArgumentException("'{$key}' is not defined.");
}
$types = $this->properties[$key];
if (is_object($value))
{
if (is_object($value)) {
$found = FALSE;
foreach ($types as $class)
if (class_exists($class) && $value instanceof $class)
foreach ($types as $class) {
if (class_exists($class) && $value instanceof $class) {
$found = TRUE;
}
}
if (!$found)
if (!$found) {
throw new \InvalidArgumentException('Unexpected class.');
} elseif (is_array($value) && in_array('array', $types))
{
} elseif ($value === NULL && in_array('null', $types))
{
} elseif (in_array('int', $types) && is_numeric($value))
{
}
} elseif (is_array($value) && in_array('array', $types)) {
} elseif ($value === NULL && in_array('null', $types)) {
} elseif (in_array('int', $types) && is_numeric($value)) {
$value = (int) $value;
} elseif (in_array('bool', $types) || in_array('boolean', $types))
{
} elseif (in_array('bool', $types) || in_array('boolean', $types)) {
$value = (bool) $value;
} elseif (in_array('string', $types))
{
} elseif (in_array('string', $types)) {
$value = (string) $value;
} else
{
} else {
throw new \InvalidArgumentException('Unexpected value.');
}
......@@ -151,8 +151,9 @@ abstract class AbstractRestriction
*/
public function &__get($key)
{
if (!array_key_exists($key, $this->values))
if (!array_key_exists($key, $this->values)) {
throw new \InvalidArgumentException("'{$key}' is not filled or defined.");
}
return $this->values[$key];
}
......
......@@ -6,7 +6,6 @@ use LeanMapper\Relationship\HasMany;
use LeanMapper\Filtering;
use LeanMapper\Fluent;
use InvalidArgumentException;
use Nette\Callback;
use Nette\Utils\Strings;
......@@ -21,19 +20,16 @@ abstract class Entity extends \LeanMapper\Entity
*/
public function __call($name, array $arguments)
{
if (in_array($name, ['addTo', 'removeFrom', 'removeAll', 'replaceAll', 'getCountOf']))
{
if (in_array($name, ['addTo', 'removeFrom', 'removeAll', 'replaceAll', 'getCountOf'])) {
if (count($arguments) === 0) {
throw new InvalidArgumentException("Method '$name' in entity " . get_called_class() . " expects at least one argument with target column name.");
}
return $this->__call($name . Strings::firstUpper(array_shift($arguments)), $arguments);
} elseif (substr($name, 0, 10) === 'getCountOf')
{
} elseif (substr($name, 0, 10) === 'getCountOf') {
$property = $this->getCurrentReflection()->getEntityProperty(lcfirst(substr($name, 10)));
if ($property->containsCollection())
{
if ($property->containsCollection()) {
$relationship = $property->getRelationship();
$table = $relationship instanceof HasMany ? $relationship->getRelationshipTable() : $relationship->getTargetTable();
$column = $relationship->getColumnReferencingSourceTable();
......
......@@ -3,16 +3,9 @@
namespace LeanMapperWorkflow;
/**
* Class Mapper
*/
class Mapper extends \LeanMapper\DefaultMapper
{
const STANDARD_NAMESPACE = 'App\\Model';
/** @var array */
protected $mapping;
......@@ -73,12 +66,13 @@ class Mapper extends \LeanMapper\DefaultMapper
*/
public function getNamespaceFromTable($tableName)
{
if (empty($tableName))
if (empty($tableName)) {
throw new \InvalidArgumentException('Method getNamespaceFromTable received invalid (empty) parameter.');
}
if (isset($this->tableMapping[$tableName]))
if (isset($this->tableMapping[$tableName])) {
return $this->tableMapping[$tableName];
else {
} else {
return $this->baseNamespace . '\\' . implode('\\', array_map('ucfirst', explode('_', $tableName)));
}
}
......@@ -99,14 +93,15 @@ class Mapper extends \LeanMapper\DefaultMapper
{
$namespace = self::getNamespaceFromClass($class);
if (empty($namespace))
if (empty($namespace)) {
throw new \InvalidArgumentException('Method getTableFromClass received invalid (empty) parameter.');
}
$table = array_search($namespace, $this->tableMapping);
if ($table !== FALSE)
if ($table !== FALSE) {
return $table;
else {
} else {
if (strpos($namespace, $this->baseNamespace) === 0) {
$namespace = str_replace($this->baseNamespace, '', $namespace);
} else {
......
......@@ -144,6 +144,7 @@ class Repository extends AbstractRepository
if (!class_exists($restriction)) {
$restriction = 'LeanMapperWorkflow\\Restriction';
}
return new $restriction($this->mapper);
}
......
......@@ -23,7 +23,7 @@ class Restriction extends AbstractRestriction
*/
public function apply(\DibiFluent $statement, Repository $repository, $flag = NULL)
{
if(isset($this->id)) {
if (isset($this->id)) {
if (is_array($this->id)) {
if (count($this->id) === 0) {
throw new \UnexpectedValueException('There must be provided at least one id');
......@@ -35,11 +35,13 @@ class Restriction extends AbstractRestriction
}
}
if (isset($this->limit) && $flag !== self::FLAG_COUNT)
if (isset($this->limit) && $flag !== self::FLAG_COUNT) {
$statement->limit($this->limit);
}
if (isset($this->offset) && $flag !== self::FLAG_COUNT)
if (isset($this->offset) && $flag !== self::FLAG_COUNT) {
$statement->offset($this->offset);
}
}
......@@ -53,6 +55,8 @@ class Restriction extends AbstractRestriction
return $this->mapper->getTableByRepositoryClass(get_class($repository));
}
public function refreshJoins()
{
foreach ($this->joins as &$join) {
......
......@@ -3,6 +3,7 @@
namespace LeanMapperWorkflow\Traits;
use LeanMapperWorkflow\Repository;
use Nette\Application\IPresenter;
trait FindEntity
......@@ -18,8 +19,8 @@ trait FindEntity
*/
protected final function findEntity(Repository $repository, $id, $showMessage = FALSE, $redirectLink = NULL)
{
if ($this instanceof \Nette\Application\IPresenter && ($showMessage !== FALSE || $redirectLink !== NULL)) {
$callback = function() use ($showMessage, $redirectLink) {
if ($this instanceof IPresenter && ($showMessage !== FALSE || $redirectLink !== NULL)) {
$notFoundCallback = function() use ($showMessage, $redirectLink) {
if ($showMessage) {
$this->flashMessage('@todo', 'error');
}
......@@ -29,10 +30,10 @@ trait FindEntity
}
};
} else {
$callback = NULL;
$notFoundCallback = NULL;
}
return $repository->find($id, $callback);
return $repository->find($id, $notFoundCallback);
}
}
\ No newline at end of file
}
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