Skip to content

Commit 2bc9662

Browse files
Merge pull request #3 from 42coders/generic-small-improvements
Minor code improvements
2 parents 9563a12 + c222751 commit 2bc9662

File tree

8 files changed

+67
-40
lines changed

8 files changed

+67
-40
lines changed

config/config.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,24 @@
6060

6161
'autofields' => [
6262
'type' => [
63-
'tinyint(1)' => 'the42coders\TLAP\Fields\CheckboxField',
64-
'text' => 'the42coders\TLAP\Fields\TrixField',
65-
'timestamp' => 'the42coders\TLAP\Fields\TimeStampField',
66-
'datetime' => 'the42coders\TLAP\Fields\TimeStampField',
67-
'bigint' => 'the42coders\TLAP\Fields\NumberField',
68-
'bigint unsigned' => 'the42coders\TLAP\Fields\NumberField',
63+
'tinyint(1)' => \the42coders\TLAP\Fields\CheckboxField::class,
64+
'text' => \the42coders\TLAP\Fields\TrixField::class,
65+
'timestamp' => \the42coders\TLAP\Fields\TimeStampField::class,
66+
'datetime' => \the42coders\TLAP\Fields\TimeStampField::class,
67+
'bigint' => \the42coders\TLAP\Fields\NumberField::class,
68+
'bigint unsigned' => \the42coders\TLAP\Fields\NumberField::class,
6969
],
7070
'name' => [
71-
'pw' => 'the42coders\TLAP\Fields\PasswordField',
72-
'password' => 'the42coders\TLAP\Fields\PasswordField',
71+
'pw' => \the42coders\TLAP\Fields\PasswordField::class,
72+
'password' => \the42coders\TLAP\Fields\PasswordField::class,
7373
],
7474
],
7575

7676
'datatableFilter' => [
7777
'type' => [
78-
'text' => 'the42coders\TLAP\Filters\ShortenTextFilter',
79-
'timestamp' => 'the42coders\TLAP\Filters\FormatTimestamps',
80-
'datetime' => 'the42coders\TLAP\Filters\FormatTimestamps',
78+
'text' => \the42coders\TLAP\Filters\ShortenTextFilter::class,
79+
'timestamp' => \the42coders\TLAP\Filters\FormatTimestamps::class,
80+
'datetime' => \the42coders\TLAP\Filters\FormatTimestamps::class,
8181
],
8282
],
8383

src/Contracts/Fields/Field.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace the42coders\TLAP\Contracts\Fields;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
interface Field
8+
{
9+
public function getValue(Model $model = null);
10+
11+
public function getInput($value);
12+
13+
public function render($model = null);
14+
15+
public function __toString();
16+
}

src/Contracts/Filters/Filter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace the42coders\TLAP\Contracts\Filters;
4+
5+
interface Filter
6+
{
7+
public function filter($value);
8+
}

src/Fields/Field.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66

77
use Illuminate\Database\Eloquent\Model;
8+
use \the42coders\TLAP\Contracts\Fields\Field as FieldContract;
89

9-
class Field
10+
class Field implements FieldContract
1011
{
1112
public $name;
1213
public $template;

src/Filters/Filter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace the42coders\TLAP\Filters;
44

5-
class Filter
5+
use the42coders\TLAP\Contracts\Filters\Filter as FilterContract;
6+
7+
abstract class Filter implements FilterContract
68
{
79

810
}

src/Http/Controllers/TLAPController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public function update($models, $id, Request $request)
6161

6262
$input = $request->all();
6363

64-
if(empty($input['pw'])){
64+
if (empty($input['pw'])){
6565
unset($input['pw']);
66-
}else {
66+
} else {
6767
$input['pw'] = Hash::make($input['pw']);
6868
}
6969

70-
if(empty($input['password'])){
70+
if (empty($input['password'])){
7171
unset($input['password']);
72-
}else {
72+
} else {
7373
$input['password'] = Hash::make($input['password']);
7474
}
7575

src/TLAP.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ public static function routes()
3434

3535
public static function getForm($columnName, $columnInfo, $model = null)
3636
{
37-
$autofieldNames = config('tlap.autofields.name');
38-
$autofieldTypes = config('tlap.autofields.type');
37+
$autoFieldName = config('tlap.autofields.name.' . $columnName);
38+
$autoFieldType = config('tlap.autofields.type.' . $columnInfo['type']);
3939

40-
if(array_key_exists($columnName, $autofieldNames)){
41-
$field = new $autofieldNames[$columnName]($columnName);
40+
if (class_exists($autoFieldName)){
41+
$field = new $autoFieldName($columnName);
4242
return $field->render($model);
4343
}
4444

45-
if(array_key_exists($columnInfo['type'], $autofieldTypes)){
46-
$field = new $autofieldTypes[$columnInfo['type']]($columnName);
45+
if (class_exists($autoFieldType)){
46+
$field = new $autoFieldType($columnName);
4747
return $field->render($model);
4848
}
4949

src/Traits/TLAPAdminTrait.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace the42coders\TLAP\Traits;
44

5-
use Illuminate\Database\Eloquent\Collection;
65
use Illuminate\Http\Request;
76
use Illuminate\Support\Arr;
87
use Illuminate\Support\Facades\DB;
98
use Illuminate\Support\Str;
109
use ReflectionClass;
11-
use the42coders\TLAP\Fields\TextField;
10+
use the42coders\TLAP\Contracts\Fields\Field;
1211
use the42coders\TLAP\TLAP;
1312

1413
trait TLAPAdminTrait
@@ -21,13 +20,13 @@ public static function fields()
2120
return self::$fields;
2221
}
2322

24-
public static function getTLAPStaticColumnStructure($tableName)
23+
public static function getTLAPStaticColumnStructure($tableName): array
2524
{
2625
$structure = [];
2726

28-
$table_info_columns = DB::select(DB::raw('SHOW COLUMNS FROM ' . $tableName));
27+
$tableInfoColumns = DB::select(DB::raw('SHOW COLUMNS FROM ' . $tableName));
2928

30-
foreach ($table_info_columns as $column) {
29+
foreach ($tableInfoColumns as $column) {
3130
$structure[$column->Field] = [
3231
'type' => $column->Type,
3332
'nullable' => $column->Null,
@@ -40,7 +39,7 @@ public static function getTLAPStaticColumnStructure($tableName)
4039
return $structure;
4140
}
4241

43-
public static function withRelations():array
42+
public static function withRelations(): array
4443
{
4544
return Arr::pluck(self::getEloquentRelations(), 'name');
4645
}
@@ -64,7 +63,7 @@ public static function getEloquentRelations(): array
6463

6564
public static function getModelName(): string
6665
{
67-
return substr(strrchr(__CLASS__, "\\"), 1);
66+
return class_basename(__CLASS__);
6867
}
6968

7069
public static function getModelPluralName(): string
@@ -77,7 +76,7 @@ public static function getTLAPStaticTableName()
7776
return (new static)->getTable();
7877
}
7978

80-
public static function getTLAPTableStructure()
79+
public static function getTLAPTableStructure(): array
8180
{
8281
$tableName = self::getTLAPStaticTableName();
8382

@@ -97,6 +96,8 @@ public static function getForm($model = null): string
9796
}
9897

9998
$form = '';
99+
100+
/** @var Field $field */
100101
foreach(self::fields() as $field) {
101102
$form .= $field->render($model);
102103
}
@@ -111,7 +112,7 @@ public static function getColumnNames(): array
111112

112113
public static function getDatatableFields(): array
113114
{
114-
if(empty(self::fields())){
115+
if (empty(self::fields())){
115116
return array_keys(self::cleanData(self::getTLAPTableStructure()));
116117
}
117118

@@ -140,9 +141,9 @@ public static function validation():array
140141

141142
$columns = self::getTLAPTableStructure();
142143

143-
foreach($columns as $columName => $column){
144+
foreach($columns as $columnName => $column){
144145
if($column['nullable'] === 'NO'){
145-
$validation[$columName] = 'required';
146+
$validation[$columnName] = 'required';
146147
}
147148
}
148149

@@ -173,11 +174,11 @@ public static function getDatatable(Request $request, $ids = null): \Illuminate\
173174

174175
$columns = self::getColumnNames();
175176

176-
if($columns[0] !== 'id'){
177+
if ($columns[0] !== 'id'){
177178
array_unshift($columns, 'id');
178179
}
179180

180-
if(empty($request->input('search.value')))
181+
if (empty($request->input('search.value')))
181182
{
182183
$data = self::select($columns)
183184
->offset($start)
@@ -219,15 +220,14 @@ public static function applyDataFilter(array $data, array $columnsStructure): ar
219220

220221
foreach($data as $dataKey => $dataValue){
221222
foreach($columnStructure as $columnKey => $columnValue){
222-
if(array_key_exists($columnValue['type'], config('tlap.datatableFilter.type'))){
223-
$filterName = config('tlap.datatableFilter.type')[$columnValue['type']];
224-
$filter = new $filterName();
223+
$filterName = config('tlap.datatableFilter.type.' . $columnValue['type']);
224+
if (class_exists($filterName)) {
225+
$filter = app($filterName);
225226
$data[$dataKey][$columnKey] = $filter->filter($dataValue[$columnKey]);
226227
}
227228
}
228229
}
229230

230-
231231
return $data;
232232
}
233233

0 commit comments

Comments
 (0)