Skip to content

Commit a78b079

Browse files
committed
Improve the field name detection on the foreign models
1 parent b6b7609 commit a78b079

File tree

7 files changed

+241
-21
lines changed

7 files changed

+241
-21
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServ
6161

6262
> A layout is required for the default views! The code generator allows you to create a layout using the command-line. Of cource you can use your own layout. You'll only need to include [CSS bootstrap framework](http://getbootstrap.com/ "CSS bootstrap framework") in your layout for the default templates to work properly. Additionally, you can chose to you design your own templetes using a different or no css framework.
6363
64+
## Lessons
65+
Checkout our YouTube channel
66+
> https://youtu.be/l21qNcsMAWg
67+
> https://youtu.be/infoecfXOCw
68+
69+
6470
## Available Commands
6571

6672
> The command in between the square brackets **[]** must be replaced with a variable of your choice.

src/Models/Field.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@ public function getLabels()
391391
return $this->labels;
392392
}
393393

394+
/**
395+
* Checks if the field is a header or not
396+
*
397+
* @return bool
398+
*/
399+
public function isHeader()
400+
{
401+
return $this->isHeader;
402+
}
403+
394404
/**
395405
* Checks if the field is nullable or not.
396406
*

src/Models/ForeignRelationship.php

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use CrestApps\CodeGenerator\Support\Config;
66
use CrestApps\CodeGenerator\Support\Contracts\JsonWriter;
77
use CrestApps\CodeGenerator\Support\Helpers;
8+
use CrestApps\CodeGenerator\Support\ResourceMapper;
89
use CrestApps\CodeGenerator\Support\Str;
910
use DB;
1011
use Exception;
12+
use File;
1113
use Illuminate\Database\Eloquent\Model;
1214

1315
class ForeignRelationship implements JsonWriter
@@ -179,13 +181,13 @@ protected function guessForeignField()
179181

180182
$primary = $this->getPrimaryKeyForForeignModel();
181183
$idPatterns = Config::getKeyPatterns();
182-
183184
$columns = array_filter($columns, function ($column) use ($primary, $idPatterns) {
185+
184186
return $column != $primary && !Helpers::strIs($idPatterns, $column);
185187
});
186188

187-
if (count($columns) == 1) {
188-
return $columns[0];
189+
if (count($columns) > 0) {
190+
return current($columns);
189191
}
190192

191193
return $primary;
@@ -278,7 +280,7 @@ public function getPrimaryKeyForForeignModel()
278280
return $model->getKeyName();
279281
}
280282

281-
return 'id';
283+
return $this->getKeyNameFromResource() ?: 'id';
282284
}
283285

284286
/**
@@ -289,15 +291,79 @@ public function getPrimaryKeyForForeignModel()
289291
public function getModelColumns()
290292
{
291293
$model = $this->getForeignModelInstance();
292-
294+
$columns = [];
293295
if ($this->isModel($model)) {
294296
$tableName = $model->getTable();
295-
return DB::getSchemaBuilder()->getColumnListing($tableName);
297+
$columns = DB::getSchemaBuilder()->getColumnListing($tableName);
298+
}
299+
300+
if (count($columns) == 0) {
301+
$columns = $this->getFieldNamesFromResource();
302+
}
303+
304+
return $columns;
305+
}
306+
307+
/**
308+
* Gets the foreign model columns from the resource file if one exists
309+
*
310+
* @return null | string
311+
*/
312+
protected function getKeyNameFromResource()
313+
{
314+
$resource = $this->getForeignResource();
315+
316+
if (!is_null($resource) && (($field = $resource->getPrimaryField()) != null)) {
317+
return $field->name;
318+
}
319+
320+
return null;
321+
}
322+
323+
/**
324+
* Gets the foreign model columns from the resource file if one exists
325+
*
326+
* @return array
327+
*/
328+
protected function getFieldNamesFromResource()
329+
{
330+
$resource = $this->getForeignResource();
331+
332+
if (!is_null($resource)) {
333+
return $resource->pluckFields();
296334
}
297335

298336
return [];
299337
}
300338

339+
/**
340+
* Gets the foreign model fields from resource file
341+
*
342+
* @return mix (null | CrestApps\CodeGenerator\Models\Resource)
343+
*/
344+
protected function getForeignResource()
345+
{
346+
$modelName = $this->getForeignModelName();
347+
$resourceFile = ResourceMapper::pluckFirst($modelName) ?: Helpers::makeJsonFileName($modelName);
348+
$resourceFileFullName = Config::getResourceFilePath($resourceFile);
349+
350+
if (File::exists($resourceFileFullName)) {
351+
return Resource::fromFile($resourceFile, 'crestapps');
352+
}
353+
354+
return null;
355+
}
356+
357+
/**
358+
* Gets the foreign model's class name
359+
*
360+
* @return string
361+
*/
362+
protected function getForeignModelName()
363+
{
364+
return class_basename($this->getFullForeignModel());
365+
}
366+
301367
/**
302368
* Gets a single instance of the foreign mode.
303369
*

src/Models/Resource.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,72 @@ public function hasFields()
6767
return $this->totalFields() > 0;
6868
}
6969

70+
/**
71+
* Get the first header field if available
72+
*
73+
* @return min (null | CrestApps\CodeGenerator\Models\Field)
74+
*/
75+
public function getHeaderField()
76+
{
77+
return collect($this->fields)->first(function ($field) {
78+
return $field->isHeader();
79+
});
80+
}
81+
82+
/**
83+
* Get the first primary field if available
84+
*
85+
* @return min (null | CrestApps\CodeGenerator\Models\Field)
86+
*/
87+
public function getPrimaryField()
88+
{
89+
return collect($this->fields)->first(function ($field) {
90+
return $field->isPrimary();
91+
});
92+
}
93+
94+
/**
95+
* Extracts the giving property name from the fields
96+
*
97+
* @return array
98+
*/
99+
public function pluckFields($property = 'name')
100+
{
101+
$names = [];
102+
103+
if ($this->hasFields()) {
104+
foreach ($this->getFields() as $field) {
105+
106+
if (property_exists($field, $property)) {
107+
$names[] = $field->{$property};
108+
}
109+
}
110+
}
111+
112+
return $names;
113+
}
114+
115+
/**
116+
* Extracts the giving property name from the fields
117+
*
118+
* @return array
119+
*/
120+
public function pluckKey($property = 'name')
121+
{
122+
$names = [];
123+
124+
if ($this->hasFields()) {
125+
foreach ($this->getFields() as $field) {
126+
127+
if (property_exists($field, $property)) {
128+
$names[] = $field->{$property};
129+
}
130+
}
131+
}
132+
133+
return $names;
134+
}
135+
70136
/**
71137
* Checks if the resources has relations
72138
*
@@ -132,6 +198,16 @@ public function toArray()
132198
];
133199
}
134200

201+
/**
202+
* Get the fields
203+
*
204+
* @return array
205+
*/
206+
public function getFields()
207+
{
208+
return $this->fields ?: [];
209+
}
210+
135211
/**
136212
* Converts the fields into a json-ready array
137213
*

src/Support/FieldTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ protected function getLabels($title, $name)
299299
$title = $this->getFirstElement($title);
300300
}
301301

302+
$name = Helpers::removePostFixWith($name, '_id');
303+
302304
$this->replaceModelName($title, $name, 'field_');
303305

304306
if ($this->hasLanguages()) {

src/Support/Helpers.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,8 @@ public static function prettifyJson(array $object)
6868
public static function convertNameToLabel($name)
6969
{
7070
$title = ucwords(str_replace('_', ' ', $name));
71-
$idString = ' Id';
7271

73-
if (ends_with($title, $idString)) {
74-
return rtrim($title, $idString);
75-
}
76-
77-
return $title;
72+
return self::removePostFixWith($title, ' Id');
7873
}
7974

8075
/**

0 commit comments

Comments
 (0)