Skip to content

Commit

Permalink
Fix #109 and #110
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek committed Mar 17, 2019
1 parent d1734c2 commit 40ae5b6
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/Commands/ResourceFileCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function handle()
}

$fields = FieldTransformer::fromString($this->option('fields'), 'generic', $input->translationFor);

$relations = $this->getRelations($input->relations);
$indexes = $this->getIndexes($input->indexes);

Expand Down
5 changes: 2 additions & 3 deletions src/Models/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -1043,10 +1043,10 @@ public function getOnAction($action)
*/
public function setDataTypeParams(array $properties)
{
if (Helpers::isKeyExists($properties, 'data-type-params') && is_array($properties['data-type-params'])) {
if (Helpers::isKeyExists($properties, 'data-type-params')) {
$this->methodParams = $this->getDataTypeParams((array) $properties['data-type-params']);
}

return $this;
}

Expand All @@ -1060,7 +1060,6 @@ public function setDataTypeParams(array $properties)
public function getDataTypeParams(array $params)
{
$type = $this->getEloquentDataMethod();

if (in_array($type, ['char', 'string']) && isset($params[0]) && ($length = intval($params[0])) > 0) {
return [$length];
}
Expand Down
29 changes: 23 additions & 6 deletions src/Models/ForeignRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ForeignRelationship implements JsonWriter
public function __construct($type, $parameters, $name, $field = null)
{
$this->setType($type);
$this->setParameters((array) $parameters);
$this->setParameters($parameters);
$this->name = $name;
$this->setField($field);
}
Expand All @@ -89,10 +89,14 @@ public function __construct($type, $parameters, $name, $field = null)
*
* @return void
*/
public function setParameters(array $parameters)
public function setParameters($parameters)
{
$this->parameters = [];

$this->parameters = [];

if(!is_array($parameters)){
$parameters = Helpers::convertStringToArray($parameters, '|');
}

foreach ($parameters as $parameter) {
$this->parameters[] = Helpers::eliminateDupilcates($parameter, "\\");
}
Expand Down Expand Up @@ -433,11 +437,24 @@ public function toArray()
*/
public static function get(array $options)
{

if (!array_key_exists('type', $options) || !array_key_exists('params', $options) || !array_key_exists('name', $options)) {

if(count($options) >= 3) {
$values = array_values($options);
$field = isset($values[3]) ? $values[3] : null;
return new ForeignRelationship(
$values[1],
$values[2],
$values[0],
$field
);
}

return null;
}

$field = array_key_exists('field', $options) ? $options['field'] : null;
$field = array_key_exists('field', $options) ? $options['field'] : null;

return new ForeignRelationship(
$options['type'],
Expand Down
87 changes: 47 additions & 40 deletions src/Support/FieldTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,53 +83,60 @@ public static function fromString($str, $localeGroup = 'generic', array $languag
// OR
// name:a;html-type:select;options:first|second|third|fourth
$fields = [];
$fieldNames = array_unique(Helpers::convertStringToArray($str));
$fieldNames = array_unique(Helpers::convertStringToArray($str, ','));

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

if (str_contains($fieldName, ':')) {
// Handle the following format
// name:a;html-type:select;options:first|second|third|fourth
if (!str_is('*name*:*', $fieldName)) {
throw new Exception('The "name" property was not provided and is required!');
}

$parts = Helpers::convertStringToArray($fieldName, ';');
if (!str_contains($fieldName, ':')) {

$field['name'] = $fieldName;

continue;
}

// Handle the following format
// name:a;html-type:select;options:first|second|third|fourth
if (!str_is('*name*:*', $fieldName)) {
throw new Exception('The "name" property was not provided and is required!');
}

$parts = Helpers::convertStringToArray($fieldName, ';');

foreach ($parts as $part) {

if (!str_is('*:*', $part) || count($properties = Helpers::convertStringToArray($part, ':', 2)) != 2) {
throw new Exception('Each provided property should use the following format "key:value"');
}
list($key, $value) = $properties;

// The renations uses # as a delimiter
$selfParts = Helpers::convertStringToArray($value, '#');

if(LaravelStr::startsWith($key, 'is-')){
$field[$key] = Helpers::stringToBool($value);
} else {
$field[$key] = count($selfParts) > 1 ? $selfParts : $value;
}


if ($key == 'options') {
$options = Helpers::convertStringToArray($value, '|');

foreach ($parts as $part) {
if (!str_is('*:*', $part) || count($properties = Helpers::convertStringToArray($part, ':')) < 2) {
throw new Exception('Each provided property should use the following format "key:value"');
}
list($key, $value) = $properties;

if(LaravelStr::startsWith($key, 'is-')){
$field[$key] = Helpers::stringToBool($value);
} else {
$field[$key] = $value;
if (count($options) == 0) {
throw new Exception('You must provide at least one option where each option is seperated by "|".');
}


if ($key == 'options') {
$options = Helpers::convertStringToArray($value, '|');

if (count($options) == 0) {
throw new Exception('You must provide at least one option where each option is seperated by "|".');
}

$field['options'] = [];
foreach ($options as $option) {
$field['options'][$option] = $option;
}
}
}
} else {
$field['name'] = $fieldName;
}
$field['options'] = [];
foreach ($options as $option) {
$field['options'][$option] = $option;
}
}
}

$fields[] = $field;
}

return self::fromArray($fields, $localeGroup, $languages, $isReadOnly);
}

Expand Down Expand Up @@ -170,7 +177,7 @@ protected function __construct(array $properties, $localeGroup, array $languages
$this->localeGroup = $localeGroup;
$this->languages = array_unique($languages);
$this->isReadOnly = $isReadOnly;
}
}

/**
* It get the fields collection
Expand Down Expand Up @@ -209,7 +216,7 @@ protected function transfer()
}

$field = Field::fromArray($properties, $this->localeGroup, $this->languages);

$mappers[] = new FieldMapper($field, (array) $rawField);
}

Expand Down Expand Up @@ -275,7 +282,7 @@ protected function setOptions(&$properties)
public function presetProperties(array &$properties)
{
$definitions = Config::getCommonDefinitions();

foreach ($definitions as $definition) {
$patterns = $this->getArrayByKey($definition, 'match');

Expand Down
4 changes: 2 additions & 2 deletions src/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,9 @@ public static function trimQuots($str)
*
* @return array
*/
public static function convertStringToArray($str, $seperator = ',')
public static function convertStringToArray($str, $seperator = ',', $limit = PHP_INT_MAX)
{
return self::removeEmptyItems(explode($seperator, $str), function ($param) {
return self::removeEmptyItems(explode($seperator, $str, $limit), function ($param) {
return self::trimQuots($param);
});
}
Expand Down
23 changes: 23 additions & 0 deletions tests/ForeignRelationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace CrestApps\CodeGeneratorTests;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use CrestApps\CodeGenerator\Models\ForeignRelationship;

class ForeignRelationTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
/** @test */
public function testAbilityToCreateRelationForSingleField()
{
$relation = ForeignRelationship::fromString("name:fooModel;is-nullable:true;data-type:varchar;foreign-relation:assets#hasMany#App\\Models\\Asset|category_id|id");

// TO DO, asset that the relation is created successfully!
}
}

0 comments on commit 40ae5b6

Please sign in to comment.