Skip to content

Commit 40ae5b6

Browse files
committed
Fix #109 and #110
1 parent d1734c2 commit 40ae5b6

File tree

6 files changed

+97
-52
lines changed

6 files changed

+97
-52
lines changed

src/Commands/ResourceFileCreateCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public function handle()
6161
}
6262

6363
$fields = FieldTransformer::fromString($this->option('fields'), 'generic', $input->translationFor);
64-
6564
$relations = $this->getRelations($input->relations);
6665
$indexes = $this->getIndexes($input->indexes);
6766

src/Models/Field.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,10 +1043,10 @@ public function getOnAction($action)
10431043
*/
10441044
public function setDataTypeParams(array $properties)
10451045
{
1046-
if (Helpers::isKeyExists($properties, 'data-type-params') && is_array($properties['data-type-params'])) {
1046+
if (Helpers::isKeyExists($properties, 'data-type-params')) {
10471047
$this->methodParams = $this->getDataTypeParams((array) $properties['data-type-params']);
10481048
}
1049-
1049+
10501050
return $this;
10511051
}
10521052

@@ -1060,7 +1060,6 @@ public function setDataTypeParams(array $properties)
10601060
public function getDataTypeParams(array $params)
10611061
{
10621062
$type = $this->getEloquentDataMethod();
1063-
10641063
if (in_array($type, ['char', 'string']) && isset($params[0]) && ($length = intval($params[0])) > 0) {
10651064
return [$length];
10661065
}

src/Models/ForeignRelationship.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ForeignRelationship implements JsonWriter
7979
public function __construct($type, $parameters, $name, $field = null)
8080
{
8181
$this->setType($type);
82-
$this->setParameters((array) $parameters);
82+
$this->setParameters($parameters);
8383
$this->name = $name;
8484
$this->setField($field);
8585
}
@@ -89,10 +89,14 @@ public function __construct($type, $parameters, $name, $field = null)
8989
*
9090
* @return void
9191
*/
92-
public function setParameters(array $parameters)
92+
public function setParameters($parameters)
9393
{
94-
$this->parameters = [];
95-
94+
$this->parameters = [];
95+
96+
if(!is_array($parameters)){
97+
$parameters = Helpers::convertStringToArray($parameters, '|');
98+
}
99+
96100
foreach ($parameters as $parameter) {
97101
$this->parameters[] = Helpers::eliminateDupilcates($parameter, "\\");
98102
}
@@ -433,11 +437,24 @@ public function toArray()
433437
*/
434438
public static function get(array $options)
435439
{
440+
436441
if (!array_key_exists('type', $options) || !array_key_exists('params', $options) || !array_key_exists('name', $options)) {
442+
443+
if(count($options) >= 3) {
444+
$values = array_values($options);
445+
$field = isset($values[3]) ? $values[3] : null;
446+
return new ForeignRelationship(
447+
$values[1],
448+
$values[2],
449+
$values[0],
450+
$field
451+
);
452+
}
453+
437454
return null;
438455
}
439-
440-
$field = array_key_exists('field', $options) ? $options['field'] : null;
456+
457+
$field = array_key_exists('field', $options) ? $options['field'] : null;
441458

442459
return new ForeignRelationship(
443460
$options['type'],

src/Support/FieldTransformer.php

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -83,53 +83,60 @@ public static function fromString($str, $localeGroup = 'generic', array $languag
8383
// OR
8484
// name:a;html-type:select;options:first|second|third|fourth
8585
$fields = [];
86-
$fieldNames = array_unique(Helpers::convertStringToArray($str));
86+
$fieldNames = array_unique(Helpers::convertStringToArray($str, ','));
8787

8888
foreach ($fieldNames as $fieldName) {
8989
$field = [];
9090

91-
if (str_contains($fieldName, ':')) {
92-
// Handle the following format
93-
// name:a;html-type:select;options:first|second|third|fourth
94-
if (!str_is('*name*:*', $fieldName)) {
95-
throw new Exception('The "name" property was not provided and is required!');
96-
}
97-
98-
$parts = Helpers::convertStringToArray($fieldName, ';');
91+
if (!str_contains($fieldName, ':')) {
92+
93+
$field['name'] = $fieldName;
94+
95+
continue;
96+
}
97+
98+
// Handle the following format
99+
// name:a;html-type:select;options:first|second|third|fourth
100+
if (!str_is('*name*:*', $fieldName)) {
101+
throw new Exception('The "name" property was not provided and is required!');
102+
}
103+
104+
$parts = Helpers::convertStringToArray($fieldName, ';');
105+
106+
foreach ($parts as $part) {
107+
108+
if (!str_is('*:*', $part) || count($properties = Helpers::convertStringToArray($part, ':', 2)) != 2) {
109+
throw new Exception('Each provided property should use the following format "key:value"');
110+
}
111+
list($key, $value) = $properties;
112+
113+
// The renations uses # as a delimiter
114+
$selfParts = Helpers::convertStringToArray($value, '#');
115+
116+
if(LaravelStr::startsWith($key, 'is-')){
117+
$field[$key] = Helpers::stringToBool($value);
118+
} else {
119+
$field[$key] = count($selfParts) > 1 ? $selfParts : $value;
120+
}
121+
122+
123+
if ($key == 'options') {
124+
$options = Helpers::convertStringToArray($value, '|');
99125

100-
foreach ($parts as $part) {
101-
if (!str_is('*:*', $part) || count($properties = Helpers::convertStringToArray($part, ':')) < 2) {
102-
throw new Exception('Each provided property should use the following format "key:value"');
103-
}
104-
list($key, $value) = $properties;
105-
106-
if(LaravelStr::startsWith($key, 'is-')){
107-
$field[$key] = Helpers::stringToBool($value);
108-
} else {
109-
$field[$key] = $value;
126+
if (count($options) == 0) {
127+
throw new Exception('You must provide at least one option where each option is seperated by "|".');
110128
}
111-
112-
113-
if ($key == 'options') {
114-
$options = Helpers::convertStringToArray($value, '|');
115129

116-
if (count($options) == 0) {
117-
throw new Exception('You must provide at least one option where each option is seperated by "|".');
118-
}
119-
120-
$field['options'] = [];
121-
foreach ($options as $option) {
122-
$field['options'][$option] = $option;
123-
}
124-
}
125-
}
126-
} else {
127-
$field['name'] = $fieldName;
128-
}
130+
$field['options'] = [];
131+
foreach ($options as $option) {
132+
$field['options'][$option] = $option;
133+
}
134+
}
135+
}
129136

130137
$fields[] = $field;
131138
}
132-
139+
133140
return self::fromArray($fields, $localeGroup, $languages, $isReadOnly);
134141
}
135142

@@ -170,7 +177,7 @@ protected function __construct(array $properties, $localeGroup, array $languages
170177
$this->localeGroup = $localeGroup;
171178
$this->languages = array_unique($languages);
172179
$this->isReadOnly = $isReadOnly;
173-
}
180+
}
174181

175182
/**
176183
* It get the fields collection
@@ -209,7 +216,7 @@ protected function transfer()
209216
}
210217

211218
$field = Field::fromArray($properties, $this->localeGroup, $this->languages);
212-
219+
213220
$mappers[] = new FieldMapper($field, (array) $rawField);
214221
}
215222

@@ -275,7 +282,7 @@ protected function setOptions(&$properties)
275282
public function presetProperties(array &$properties)
276283
{
277284
$definitions = Config::getCommonDefinitions();
278-
285+
279286
foreach ($definitions as $definition) {
280287
$patterns = $this->getArrayByKey($definition, 'match');
281288

src/Support/Helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,9 @@ public static function trimQuots($str)
578578
*
579579
* @return array
580580
*/
581-
public static function convertStringToArray($str, $seperator = ',')
581+
public static function convertStringToArray($str, $seperator = ',', $limit = PHP_INT_MAX)
582582
{
583-
return self::removeEmptyItems(explode($seperator, $str), function ($param) {
583+
return self::removeEmptyItems(explode($seperator, $str, $limit), function ($param) {
584584
return self::trimQuots($param);
585585
});
586586
}

tests/ForeignRelationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace CrestApps\CodeGeneratorTests;
4+
5+
use Tests\TestCase;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use CrestApps\CodeGenerator\Models\ForeignRelationship;
8+
9+
class ForeignRelationTest extends TestCase
10+
{
11+
/**
12+
* A basic test example.
13+
*
14+
* @return void
15+
*/
16+
/** @test */
17+
public function testAbilityToCreateRelationForSingleField()
18+
{
19+
$relation = ForeignRelationship::fromString("name:fooModel;is-nullable:true;data-type:varchar;foreign-relation:assets#hasMany#App\\Models\\Asset|category_id|id");
20+
21+
// TO DO, asset that the relation is created successfully!
22+
}
23+
}

0 commit comments

Comments
 (0)