Skip to content

Commit

Permalink
Merge pull request #79 from anavel/analysis-XleKEk
Browse files Browse the repository at this point in the history
Applied fixes from StyleCI
  • Loading branch information
ablunier authored Nov 14, 2016
2 parents 5fe226c + b99c7f3 commit 5c1a793
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 131 deletions.
3 changes: 0 additions & 3 deletions src/Abstractor/Eloquent/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ public function getFormField()
return $this->formField;
}


public function hideValue($value = null)
{
if (!is_null($value)) {
Expand All @@ -190,7 +189,6 @@ public function hideValue($value = null)
return $this->hideValue;
}


public function saveIfEmpty($value = null)
{
if (!is_null($value)) {
Expand All @@ -200,7 +198,6 @@ public function saveIfEmpty($value = null)
return $this->saveIfEmpty;
}


public function noValidate($value = null)
{
if (!is_null($value)) {
Expand Down
3 changes: 0 additions & 3 deletions src/Abstractor/Eloquent/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,20 @@ public function __construct(FormManagerFactory $factory)
$this->factory = $factory;
}


public function setColumn(Column $column)
{
$this->column = $column;

return $this;
}


public function setConfig(array $config)
{
$this->config = $config;

return $this;
}


public function get()
{
$formElement = $this->getFormElement();
Expand Down
107 changes: 57 additions & 50 deletions src/Abstractor/Eloquent/Model.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
<?php

namespace Anavel\Crud\Abstractor\Eloquent;

use ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer;
use Anavel\Crud\Abstractor\ConfigurationReader;
use Anavel\Crud\Abstractor\Eloquent\Traits\HandleFiles;
use Anavel\Crud\Abstractor\Eloquent\Traits\ModelFields;
use Anavel\Crud\Abstractor\Exceptions\AbstractorException;
use Anavel\Crud\Contracts\Abstractor\Field as FieldContract;
use Anavel\Crud\Contracts\Abstractor\FieldFactory as FieldFactoryContract;
use Anavel\Crud\Contracts\Abstractor\Model as ModelAbstractorContract;
use Anavel\Crud\Abstractor\ConfigurationReader;
use Anavel\Crud\Contracts\Abstractor\Relation;
use Anavel\Crud\Contracts\Abstractor\RelationFactory as RelationFactoryContract;
use Anavel\Crud\Contracts\Abstractor\FieldFactory as FieldFactoryContract;
use ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer;
use Anavel\Crud\Contracts\Form\Generator as FormGenerator;
use App;
use FormManager\ElementInterface;
use Illuminate\Database\Eloquent\Model as LaravelModel;
use App;
use Anavel\Crud\Contracts\Form\Generator as FormGenerator;
use Anavel\Crud\Abstractor\Exceptions\AbstractorException;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;

class Model implements ModelAbstractorContract
{
Expand Down Expand Up @@ -143,10 +142,10 @@ public function getColumns($action, $withForeignKeys = false)
}

$customDisplayedColumns = $this->getConfigValue($action, 'display');
$customHiddenColumns = $this->getConfigValue($action, 'hide') ? : [];
$customHiddenColumns = $this->getConfigValue($action, 'hide') ?: [];

$columns = array();
if (! empty($customDisplayedColumns) && is_array($customDisplayedColumns)) {
$columns = [];
if (!empty($customDisplayedColumns) && is_array($customDisplayedColumns)) {
foreach ($customDisplayedColumns as $customColumn) {
if (strpos($customColumn, '.')) {
$customColumnRelation = explode('.', $customColumn);
Expand All @@ -161,14 +160,14 @@ public function getColumns($action, $withForeignKeys = false)

$relationColumns = $nestedRelation->getModelAbstractor()->getColumns($action);

if (! array_key_exists($customColumnRelationFieldName, $relationColumns)) {
throw new AbstractorException("Column " . $customColumnRelationFieldName . " does not exist on relation " . implode('.', $customColumnRelation) . " of model " . $this->getModel());
if (!array_key_exists($customColumnRelationFieldName, $relationColumns)) {
throw new AbstractorException('Column '.$customColumnRelationFieldName.' does not exist on relation '.implode('.', $customColumnRelation).' of model '.$this->getModel());
}

$columns[$customColumn] = $relationColumns[$customColumnRelationFieldName];
} else {
if (! array_key_exists($customColumn, $tableColumns)) {
throw new AbstractorException("Column " . $customColumn . " does not exist on " . $this->getModel());
if (!array_key_exists($customColumn, $tableColumns)) {
throw new AbstractorException('Column '.$customColumn.' does not exist on '.$this->getModel());
}

$columns[$customColumn] = $tableColumns[$customColumn];
Expand All @@ -195,8 +194,8 @@ protected function getNestedRelation(Model $modelAbstractor, $relationName)
{
$relations = $modelAbstractor->getRelations();

if (! $relations->has($relationName)) {
throw new AbstractorException("Relation " . $relationName . " not configured on " . $modelAbstractor->getModel());
if (!$relations->has($relationName)) {
throw new AbstractorException('Relation '.$relationName.' not configured on '.$modelAbstractor->getModel());
}

$relation = $relations->get($relationName);
Expand All @@ -217,15 +216,15 @@ public function getRelations()

$relations = collect();

if (! empty($configRelations)) {
if (!empty($configRelations)) {
foreach ($configRelations as $relationName => $configRelation) {
if (is_int($relationName)) {
$relationName = $configRelation;
}

$config = [];
if ($configRelation !== $relationName) {
if (! is_array($configRelation)) {
if (!is_array($configRelation)) {
$config['type'] = $configRelation;
} else {
$config = $configRelation;
Expand All @@ -240,7 +239,7 @@ public function getRelations()
$secondaryRelations = $relation->getSecondaryRelations();


if (! $secondaryRelations->isEmpty()) {
if (!$secondaryRelations->isEmpty()) {
$relations->put(
$relationName,
collect(['relation' => $relation, 'secondaryRelations' => $secondaryRelations])
Expand All @@ -256,16 +255,18 @@ public function getRelations()

/**
* @param string|null $arrayKey
* @return array
*
* @throws AbstractorException
*
* @return array
*/
public function getListFields($arrayKey = 'main')
{
$columns = $this->getColumns('list');

$fieldsPresentation = $this->getConfigValue('fields_presentation') ? : [];
$fieldsPresentation = $this->getConfigValue('fields_presentation') ?: [];

$fields = array();
$fields = [];
foreach ($columns as $name => $column) {
$presentation = null;
if (array_key_exists($name, $fieldsPresentation)) {
Expand All @@ -277,7 +278,7 @@ public function getListFields($arrayKey = 'main')
'presentation' => $presentation,
'form_type' => null,
'validation' => null,
'functions' => null
'functions' => null,
];

$fields[$arrayKey][] = $this->fieldFactory
Expand All @@ -291,16 +292,18 @@ public function getListFields($arrayKey = 'main')

/**
* @param string|null $arrayKey
* @return array
*
* @throws AbstractorException
*
* @return array
*/
public function getDetailFields($arrayKey = 'main')
{
$columns = $this->getColumns('detail');

$fieldsPresentation = $this->getConfigValue('fields_presentation') ? : [];
$fieldsPresentation = $this->getConfigValue('fields_presentation') ?: [];

$fields = array();
$fields = [];
foreach ($columns as $name => $column) {
$presentation = null;
if (array_key_exists($name, $fieldsPresentation)) {
Expand All @@ -312,7 +315,7 @@ public function getDetailFields($arrayKey = 'main')
'presentation' => $presentation,
'form_type' => null,
'validation' => null,
'functions' => null
'functions' => null,
];

$fields[$arrayKey][] = $this->fieldFactory
Expand All @@ -325,20 +328,22 @@ public function getDetailFields($arrayKey = 'main')
}

/**
* @param bool|null $withForeignKeys
* @param bool|null $withForeignKeys
* @param string|null $arrayKey
* @return array
*
* @throws AbstractorException
*
* @return array
*/
public function getEditFields($withForeignKeys = false, $arrayKey = 'main')
{
$columns = $this->getColumns('edit', $withForeignKeys);

$this->readConfig('edit');

$fields = array();
$fields = [];
foreach ($columns as $name => $column) {
if (! in_array($name, $this->getReadOnlyColumns())) {
if (!in_array($name, $this->getReadOnlyColumns())) {
$presentation = null;
if (array_key_exists($name, $this->fieldsPresentation)) {
$presentation = $this->fieldsPresentation[$name];
Expand All @@ -349,7 +354,7 @@ public function getEditFields($withForeignKeys = false, $arrayKey = 'main')
'presentation' => $presentation,
'form_type' => null,
'validation' => null,
'functions' => null
'functions' => null,
];

$config = $this->setConfig($config, $name);
Expand All @@ -359,24 +364,24 @@ public function getEditFields($withForeignKeys = false, $arrayKey = 'main')
->setConfig($config)
->get();

if (! empty($this->instance) && ! empty($this->instance->getAttribute($name))) {
if (!empty($this->instance) && !empty($this->instance->getAttribute($name))) {
$field->setValue($this->instance->getAttribute($name));
}

$fields[$arrayKey][$name] = $field;

if (! empty($config['form_type']) && $config['form_type'] === 'file') {
if (!empty($config['form_type']) && $config['form_type'] === 'file') {
$field = $this->fieldFactory
->setColumn($column)
->setConfig([
'name' => $name . '__delete',
'name' => $name.'__delete',
'presentation' => null,
'form_type' => 'checkbox',
'no_validate' => true,
'functions' => null
'functions' => null,
])
->get();
$fields[$arrayKey][$name . '__delete'] = $field;
$fields[$arrayKey][$name.'__delete'] = $field;
}
}
}
Expand All @@ -395,6 +400,7 @@ protected function getReadOnlyColumns()

/**
* @param string $action
*
* @return ElementInterface
*/
public function getForm($action)
Expand All @@ -407,13 +413,14 @@ public function getForm($action)

/**
* @param array $requestForm
*
* @return mixed
*/
public function persist(Request $request)
{
/** @var \ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager $modelManager */
$modelManager = App::make('ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager');
if (! empty($this->instance)) {
if (!empty($this->instance)) {
$item = $this->instance;
} else {
$item = $modelManager->getModelInstance($this->getModel());
Expand All @@ -425,10 +432,10 @@ public function persist(Request $request)
return;
}

if (! empty($fields['main'])) {
if (!empty($fields['main'])) {
$skip = null;
foreach ($fields['main'] as $key => $field) {
/** @var FieldContract $field */
/* @var FieldContract $field */
if ($skip === $key) {
$skip = null;
continue;
Expand All @@ -449,21 +456,21 @@ public function persist(Request $request)
}

if (get_class($field->getFormField()) === \FormManager\Fields\File::class) {
$handleResult = $this->handleField($request, $item, $fields['main'], 'main', $fieldName,$this->mustDeleteFilesInFilesystem);
if (! empty($handleResult['skip'])) {
$handleResult = $this->handleField($request, $item, $fields['main'], 'main', $fieldName, $this->mustDeleteFilesInFilesystem);
if (!empty($handleResult['skip'])) {
$skip = $handleResult['skip'];
}
if (! empty($handleResult['requestValue'])) {
if (!empty($handleResult['requestValue'])) {
$requestValue = $handleResult['requestValue'];
}
}


if (! $field->saveIfEmpty() && empty($requestValue)) {
if (!$field->saveIfEmpty() && empty($requestValue)) {
continue;
}

if (! empty($requestValue) || (empty($requestValue) && ! empty($item->getAttribute($fieldName)))) {
if (!empty($requestValue) || (empty($requestValue) && !empty($item->getAttribute($fieldName)))) {
$item->setAttribute(
$fieldName,
$field->applyFunctions($requestValue)
Expand All @@ -477,15 +484,15 @@ public function persist(Request $request)
$this->setInstance($item);


if (! empty($relations = $this->getRelations())) {
if (!empty($relations = $this->getRelations())) {
foreach ($relations as $relationKey => $relation) {
if ($relation instanceof Collection) {
$input = $request->input($relationKey);
/** @var $relationInstance Relation */
$relationInstance = $relation->get('relation');
$relationInstance->persist($input, $request);
} else {
/** @var $relation Relation */
/* @var $relation Relation */
$relation->persist($request->input($relationKey), $request);
}
}
Expand Down Expand Up @@ -534,7 +541,7 @@ public function getFieldValue($item, $fieldName)
}

if (empty($relation)) {
return null;
return;
}

if ($relation instanceof Collection) {
Expand All @@ -558,7 +565,7 @@ public function getFieldValue($item, $fieldName)
}

/**
* @return boolean
* @return bool
*/
public function mustDeleteFilesInFilesystem()
{
Expand Down
Loading

0 comments on commit 5c1a793

Please sign in to comment.