@@ -111,6 +111,9 @@ |
||
| 111 | 111 | |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | + /** |
|
| 115 | + * @param string $tag |
|
| 116 | + */ |
|
| 114 | 117 | protected function fieldsHavingTag($tag) |
| 115 | 118 | { |
| 116 | 119 | return array_map(function($field){ |
@@ -1,8 +1,6 @@ |
||
| 1 | 1 | <?php namespace Wn\Generators\Commands; |
| 2 | 2 | |
| 3 | 3 | |
| 4 | -use InvalidArgumentException; |
|
| 5 | - |
|
| 6 | 4 | class ResourceCommand extends BaseCommand { |
| 7 | 5 | |
| 8 | 6 | protected $signature = 'wn:resource |
@@ -74,9 +74,9 @@ discard block |
||
| 74 | 74 | '--parsed' => true |
| 75 | 75 | ]); |
| 76 | 76 | |
| 77 | - if (! $this->option('no-controller')) { |
|
| 77 | + if (!$this->option('no-controller')) { |
|
| 78 | 78 | // generating REST actions trait if doesn't exist |
| 79 | - if(! $this->fs->exists('./app/Http/Controllers/RESTActions.php')){ |
|
| 79 | + if (!$this->fs->exists('./app/Http/Controllers/RESTActions.php')) { |
|
| 80 | 80 | $this->call('wn:controller:rest-actions'); |
| 81 | 81 | } |
| 82 | 82 | |
@@ -100,8 +100,8 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | // generating model factory |
| 102 | 102 | $this->call('wn:factory', [ |
| 103 | - 'model' => $this->getNamespace().'\\'.$modelName, |
|
| 104 | - '--file' => './database/factories/'.str_plural($modelName).'.php', |
|
| 103 | + 'model' => $this->getNamespace() . '\\' . $modelName, |
|
| 104 | + '--file' => './database/factories/' . str_plural($modelName) . '.php', |
|
| 105 | 105 | '--fields' => $this->factoryFields(), |
| 106 | 106 | '--force' => $this->option('force'), |
| 107 | 107 | '--parsed' => true |
@@ -117,10 +117,10 @@ discard block |
||
| 117 | 117 | protected function parseFields() |
| 118 | 118 | { |
| 119 | 119 | $fields = $this->argument('fields'); |
| 120 | - if($this->option('parsed')){ |
|
| 120 | + if ($this->option('parsed')) { |
|
| 121 | 121 | $this->fields = $fields; |
| 122 | 122 | } else { |
| 123 | - if(! $fields){ |
|
| 123 | + if (!$fields) { |
|
| 124 | 124 | $this->fields = []; |
| 125 | 125 | } else { |
| 126 | 126 | $this->fields = $this->getArgumentParser('fields') |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | if ($name['nullable']) { |
| 152 | 152 | $return['schema'][] = ['name' => 'nullable', 'args' => []]; |
| 153 | 153 | } else { |
| 154 | - $return['rules'] = 'required'.(!empty($return['rules'])?'|'.$return['rules']:''); |
|
| 154 | + $return['rules'] = 'required' . (!empty($return['rules']) ? '|' . $return['rules'] : ''); |
|
| 155 | 155 | } |
| 156 | 156 | } else { |
| 157 | 157 | $return['schema'] = [ |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | |
| 171 | 171 | protected function fieldsHavingTag($tag) |
| 172 | 172 | { |
| 173 | - return array_map(function($field){ |
|
| 173 | + return array_map(function($field) { |
|
| 174 | 174 | return $field['name']; |
| 175 | 175 | }, array_filter($this->fields, function($field) use($tag){ |
| 176 | 176 | return in_array($tag, $field['tags']); |
@@ -179,19 +179,19 @@ discard block |
||
| 179 | 179 | |
| 180 | 180 | protected function rules() |
| 181 | 181 | { |
| 182 | - return array_map(function($field){ |
|
| 182 | + return array_map(function($field) { |
|
| 183 | 183 | return [ |
| 184 | 184 | 'name' => $field['name'], |
| 185 | 185 | 'rule' => $field['rules'] |
| 186 | 186 | ]; |
| 187 | - }, array_filter($this->fields, function($field){ |
|
| 187 | + }, array_filter($this->fields, function($field) { |
|
| 188 | 188 | return !empty($field['rules']); |
| 189 | 189 | })); |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | protected function schema() |
| 193 | 193 | { |
| 194 | - return array_map(function($field){ |
|
| 194 | + return array_map(function($field) { |
|
| 195 | 195 | return array_merge([[ |
| 196 | 196 | 'name' => $field['name'], |
| 197 | 197 | 'args' => [] |
@@ -204,19 +204,19 @@ discard block |
||
| 204 | 204 | $belongsTo = $this->option('belongs-to'); |
| 205 | 205 | $morphTo = $this->option('morph-to'); |
| 206 | 206 | |
| 207 | - if(! $belongsTo && (! $withMorph || ! $morphTo)) { |
|
| 207 | + if (!$belongsTo && (!$withMorph || !$morphTo)) { |
|
| 208 | 208 | return []; |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | $belongsTo = $belongsTo ? $this->getArgumentParser('relations')->parse($belongsTo) : []; |
| 212 | 212 | |
| 213 | - $belongsTo = array_map(function($relation){ |
|
| 213 | + $belongsTo = array_map(function($relation) { |
|
| 214 | 214 | return array("model" => camel_case(str_singular($this->extractClassName($relation['model'] ? $relation['model'] : $relation['name']))), "name" => snake_case(str_singular($this->extractClassName($relation['name']))) . '_id', "type" => "belongsTo"); |
| 215 | 215 | }, $belongsTo); |
| 216 | 216 | |
| 217 | 217 | if ($withMorph) { |
| 218 | 218 | $morphTo = $morphTo ? $this->getArgumentParser('relations-morphTo')->parse($morphTo) : []; |
| 219 | - $morphTo = array_map(function($relation){ |
|
| 219 | + $morphTo = array_map(function($relation) { |
|
| 220 | 220 | $name = snake_case(str_singular($relation['name'])); |
| 221 | 221 | return array(array("name" => $name . '_id', "type" => "morphTo", "nullable" => $relation['nullable']), array("name" => $name . '_type', "type" => "morphTo", "nullable" => $relation['nullable'])); |
| 222 | 222 | }, $morphTo); |
@@ -245,12 +245,12 @@ discard block |
||
| 245 | 245 | |
| 246 | 246 | protected function factoryFields() |
| 247 | 247 | { |
| 248 | - return array_map(function($field){ |
|
| 248 | + return array_map(function($field) { |
|
| 249 | 249 | return [ |
| 250 | 250 | 'name' => $field['name'], |
| 251 | 251 | 'type' => $field['factory'] |
| 252 | 252 | ]; |
| 253 | - }, array_filter($this->fields, function($field){ |
|
| 253 | + }, array_filter($this->fields, function($field) { |
|
| 254 | 254 | return isset($field['factory']) && $field['factory']; |
| 255 | 255 | })); |
| 256 | 256 | } |
@@ -19,11 +19,11 @@ |
||
| 19 | 19 | |
| 20 | 20 | public function load($name) |
| 21 | 21 | { |
| 22 | - if(! isset($this->loaded[$name])){ |
|
| 22 | + if (!isset($this->loaded[$name])) { |
|
| 23 | 23 | $path = __DIR__ . "/../../templates/{$name}.wnt"; |
| 24 | 24 | try { |
| 25 | 25 | $this->loaded[$name] = $this->fs->get($path); |
| 26 | - } catch(\Exception $e) { |
|
| 26 | + } catch (\Exception $e) { |
|
| 27 | 27 | throw new TemplateException("Unable to read the file '{$path}'"); |
| 28 | 28 | } |
| 29 | 29 | } |
@@ -7,27 +7,27 @@ |
||
| 7 | 7 | |
| 8 | 8 | class TemplateLoader { |
| 9 | 9 | |
| 10 | - protected $fs; |
|
| 10 | + protected $fs; |
|
| 11 | 11 | |
| 12 | - protected $loaded; |
|
| 12 | + protected $loaded; |
|
| 13 | 13 | |
| 14 | - public function __construct(Filesystem $fs) |
|
| 15 | - { |
|
| 16 | - $this->fs = $fs; |
|
| 17 | - $this->loaded = []; |
|
| 18 | - } |
|
| 14 | + public function __construct(Filesystem $fs) |
|
| 15 | + { |
|
| 16 | + $this->fs = $fs; |
|
| 17 | + $this->loaded = []; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - public function load($name) |
|
| 21 | - { |
|
| 22 | - if(! isset($this->loaded[$name])){ |
|
| 23 | - $path = __DIR__ . "/../../templates/{$name}.wnt"; |
|
| 24 | - try { |
|
| 25 | - $this->loaded[$name] = $this->fs->get($path); |
|
| 26 | - } catch(\Exception $e) { |
|
| 27 | - throw new TemplateException("Unable to read the file '{$path}'"); |
|
| 28 | - } |
|
| 29 | - } |
|
| 30 | - return new Template($this, $this->loaded[$name]); |
|
| 31 | - } |
|
| 20 | + public function load($name) |
|
| 21 | + { |
|
| 22 | + if(! isset($this->loaded[$name])){ |
|
| 23 | + $path = __DIR__ . "/../../templates/{$name}.wnt"; |
|
| 24 | + try { |
|
| 25 | + $this->loaded[$name] = $this->fs->get($path); |
|
| 26 | + } catch(\Exception $e) { |
|
| 27 | + throw new TemplateException("Unable to read the file '{$path}'"); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | + return new Template($this, $this->loaded[$name]); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | 33 | } |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | public function with($data = []) |
| 35 | 35 | { |
| 36 | - if($data) |
|
| 36 | + if ($data) |
|
| 37 | 37 | $this->dirty = true; |
| 38 | 38 | foreach ($data as $key => $value) { |
| 39 | 39 | $this->data[$key] = $value; |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | public function get() |
| 45 | 45 | { |
| 46 | - if($this->dirty){ |
|
| 46 | + if ($this->dirty) { |
|
| 47 | 47 | $this->compile(); |
| 48 | 48 | $this->dirty = false; |
| 49 | 49 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | public function compile() |
| 54 | 54 | { |
| 55 | 55 | $this->compiled = $this->text; |
| 56 | - foreach($this->data as $key => $value){ |
|
| 56 | + foreach ($this->data as $key => $value) { |
|
| 57 | 57 | $this->compiled = str_replace('{{' . $key . '}}', $value, $this->compiled); |
| 58 | 58 | } |
| 59 | 59 | return $this; |
@@ -33,8 +33,9 @@ |
||
| 33 | 33 | |
| 34 | 34 | public function with($data = []) |
| 35 | 35 | { |
| 36 | - if($data) |
|
| 37 | - $this->dirty = true; |
|
| 36 | + if($data) { |
|
| 37 | + $this->dirty = true; |
|
| 38 | + } |
|
| 38 | 39 | foreach ($data as $key => $value) { |
| 39 | 40 | $this->data[$key] = $value; |
| 40 | 41 | } |
@@ -5,58 +5,58 @@ |
||
| 5 | 5 | |
| 6 | 6 | class Template { |
| 7 | 7 | |
| 8 | - protected $loader; |
|
| 9 | - |
|
| 10 | - protected $text; |
|
| 11 | - |
|
| 12 | - protected $data; |
|
| 13 | - |
|
| 14 | - protected $compiled; |
|
| 15 | - |
|
| 16 | - protected $dirty; |
|
| 17 | - |
|
| 18 | - public function __construct(TemplateLoader $loader, $text) |
|
| 19 | - { |
|
| 20 | - $this->loader = $loader; |
|
| 21 | - $this->text = $text; |
|
| 22 | - $this->compiled = ''; |
|
| 23 | - $this->data = []; |
|
| 24 | - $this->dirty = true; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - public function clean() |
|
| 28 | - { |
|
| 29 | - $this->data = []; |
|
| 30 | - $this->dirty = false; |
|
| 31 | - return $this; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - public function with($data = []) |
|
| 35 | - { |
|
| 36 | - if($data) |
|
| 37 | - $this->dirty = true; |
|
| 38 | - foreach ($data as $key => $value) { |
|
| 39 | - $this->data[$key] = $value; |
|
| 40 | - } |
|
| 41 | - return $this; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - public function get() |
|
| 45 | - { |
|
| 46 | - if($this->dirty){ |
|
| 47 | - $this->compile(); |
|
| 48 | - $this->dirty = false; |
|
| 49 | - } |
|
| 50 | - return $this->compiled; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function compile() |
|
| 54 | - { |
|
| 55 | - $this->compiled = $this->text; |
|
| 56 | - foreach($this->data as $key => $value){ |
|
| 57 | - $this->compiled = str_replace('{{' . $key . '}}', $value, $this->compiled); |
|
| 58 | - } |
|
| 59 | - return $this; |
|
| 60 | - } |
|
| 8 | + protected $loader; |
|
| 9 | + |
|
| 10 | + protected $text; |
|
| 11 | + |
|
| 12 | + protected $data; |
|
| 13 | + |
|
| 14 | + protected $compiled; |
|
| 15 | + |
|
| 16 | + protected $dirty; |
|
| 17 | + |
|
| 18 | + public function __construct(TemplateLoader $loader, $text) |
|
| 19 | + { |
|
| 20 | + $this->loader = $loader; |
|
| 21 | + $this->text = $text; |
|
| 22 | + $this->compiled = ''; |
|
| 23 | + $this->data = []; |
|
| 24 | + $this->dirty = true; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + public function clean() |
|
| 28 | + { |
|
| 29 | + $this->data = []; |
|
| 30 | + $this->dirty = false; |
|
| 31 | + return $this; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + public function with($data = []) |
|
| 35 | + { |
|
| 36 | + if($data) |
|
| 37 | + $this->dirty = true; |
|
| 38 | + foreach ($data as $key => $value) { |
|
| 39 | + $this->data[$key] = $value; |
|
| 40 | + } |
|
| 41 | + return $this; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + public function get() |
|
| 45 | + { |
|
| 46 | + if($this->dirty){ |
|
| 47 | + $this->compile(); |
|
| 48 | + $this->dirty = false; |
|
| 49 | + } |
|
| 50 | + return $this->compiled; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function compile() |
|
| 54 | + { |
|
| 55 | + $this->compiled = $this->text; |
|
| 56 | + foreach($this->data as $key => $value){ |
|
| 57 | + $this->compiled = str_replace('{{' . $key . '}}', $value, $this->compiled); |
|
| 58 | + } |
|
| 59 | + return $this; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | } |
@@ -3,10 +3,10 @@ |
||
| 3 | 3 | |
| 4 | 4 | class ControllerRestActionsCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:controller:rest-actions |
|
| 6 | + protected $signature = 'wn:controller:rest-actions |
|
| 7 | 7 | {--force= : override the existing files}'; |
| 8 | 8 | |
| 9 | - protected $description = 'Generates REST actions trait to use into controllers'; |
|
| 9 | + protected $description = 'Generates REST actions trait to use into controllers'; |
|
| 10 | 10 | |
| 11 | 11 | public function handle() |
| 12 | 12 | { |
@@ -3,13 +3,13 @@ |
||
| 3 | 3 | |
| 4 | 4 | class SeederCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:seeder |
|
| 6 | + protected $signature = 'wn:seeder |
|
| 7 | 7 | {model : full qualified name of the model.} |
| 8 | 8 | {--count=10 : number of elements to add in database.} |
| 9 | 9 | {--force= : override the existing files} |
| 10 | 10 | '; |
| 11 | 11 | |
| 12 | - protected $description = 'Generates a seeder'; |
|
| 12 | + protected $description = 'Generates a seeder'; |
|
| 13 | 13 | |
| 14 | 14 | public function handle() |
| 15 | 15 | { |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | |
| 4 | 4 | class FactoryCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:factory |
|
| 6 | + protected $signature = 'wn:factory |
|
| 7 | 7 | {model : full qualified name of the model.} |
| 8 | 8 | {--fields= : the fields to generate.} |
| 9 | 9 | {--file= : the factories file.} |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | {--force= : override the existing files} |
| 12 | 12 | '; |
| 13 | 13 | |
| 14 | - protected $description = 'Generates a model factory'; |
|
| 14 | + protected $description = 'Generates a model factory'; |
|
| 15 | 15 | |
| 16 | 16 | public function handle() |
| 17 | 17 | { |
@@ -19,9 +19,9 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | $file = $this->getFile(); |
| 21 | 21 | |
| 22 | - $content = ! $this->fs->exists($file) ? '' : $this->fs->get($file); |
|
| 22 | + $content = !$this->fs->exists($file) ? '' : $this->fs->get($file); |
|
| 23 | 23 | |
| 24 | - $content .= $this->getTemplate(! $this->fs->exists($file) ? 'factory-create' : 'factory') |
|
| 24 | + $content .= $this->getTemplate(!$this->fs->exists($file) ? 'factory-create' : 'factory') |
|
| 25 | 25 | ->with([ |
| 26 | 26 | 'model' => $model, |
| 27 | 27 | 'factory_fields' => $this->getFieldsContent() |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | protected function getFile() |
| 35 | 35 | { |
| 36 | 36 | $file = $this->option('file'); |
| 37 | - if(! $file){ |
|
| 37 | + if (!$file) { |
|
| 38 | 38 | $file = './database/factories/ModelFactory.php'; |
| 39 | 39 | } |
| 40 | 40 | return $file; |
@@ -46,12 +46,12 @@ discard block |
||
| 46 | 46 | |
| 47 | 47 | $fields = $this->option('fields'); |
| 48 | 48 | |
| 49 | - if($fields){ |
|
| 50 | - if(! $this->option('parsed')){ |
|
| 49 | + if ($fields) { |
|
| 50 | + if (!$this->option('parsed')) { |
|
| 51 | 51 | $fields = $this->getArgumentParser('factory-fields')->parse($fields); |
| 52 | 52 | } |
| 53 | 53 | $template = $this->getTemplate('factory/field'); |
| 54 | - foreach($fields as $field){ |
|
| 54 | + foreach ($fields as $field) { |
|
| 55 | 55 | $content[] = $template->with($field)->get(); |
| 56 | 56 | } |
| 57 | 57 | $content = implode(PHP_EOL, $content); |
@@ -3,14 +3,14 @@ |
||
| 3 | 3 | |
| 4 | 4 | class PivotSeederCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:pivot-seeder |
|
| 6 | + protected $signature = 'wn:pivot-seeder |
|
| 7 | 7 | {model1 : Name of the first model or table} |
| 8 | 8 | {model2 : Name of the second model or table} |
| 9 | 9 | {--count=10 : number of elements to add in database.} |
| 10 | 10 | {--force= : override the existing files} |
| 11 | 11 | '; |
| 12 | 12 | |
| 13 | - protected $description = 'Generates seeder for pivot table'; |
|
| 13 | + protected $description = 'Generates seeder for pivot table'; |
|
| 14 | 14 | |
| 15 | 15 | public function handle() |
| 16 | 16 | { |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | protected function getSeederName($resources) { |
| 48 | - $resources = array_map(function($resource){ |
|
| 48 | + $resources = array_map(function($resource) { |
|
| 49 | 49 | return ucwords(camel_case($resource)); |
| 50 | 50 | }, $resources); |
| 51 | 51 | return implode('', $resources) . 'TableSeeder'; |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | |
| 4 | 4 | class PivotTableCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:pivot-table |
|
| 6 | + protected $signature = 'wn:pivot-table |
|
| 7 | 7 | {model1 : Name of the first model or table} |
| 8 | 8 | {model2 : Name of the second model or table} |
| 9 | 9 | {--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.} |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | {--force= : override the existing files} |
| 12 | 12 | '; |
| 13 | 13 | |
| 14 | - protected $description = 'Generates creation migration for a pivot table'; |
|
| 14 | + protected $description = 'Generates creation migration for a pivot table'; |
|
| 15 | 15 | |
| 16 | 16 | protected $tables; |
| 17 | 17 | |
@@ -41,14 +41,14 @@ |
||
| 41 | 41 | |
| 42 | 42 | protected function schema() |
| 43 | 43 | { |
| 44 | - return implode(' ', array_map(function($table){ |
|
| 44 | + return implode(' ', array_map(function($table) { |
|
| 45 | 45 | return $table . '_id:integer:unsigned:index'; |
| 46 | 46 | }, $this->tables)); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | protected function keys() |
| 50 | 50 | { |
| 51 | - return implode(' ', array_map(function($table){ |
|
| 51 | + return implode(' ', array_map(function($table) { |
|
| 52 | 52 | return $table . '_id'; |
| 53 | 53 | }, $this->tables)); |
| 54 | 54 | } |
@@ -19,15 +19,15 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | public function load($name) |
| 21 | 21 | { |
| 22 | - if(! isset($this->loaded[$name])){ |
|
| 22 | + if (!isset($this->loaded[$name])) { |
|
| 23 | 23 | $path = __DIR__ . "/../../formats/{$name}.json"; |
| 24 | 24 | $json = ""; |
| 25 | 25 | try { |
| 26 | 26 | $json = json_decode($this->fs->get($path)); |
| 27 | - } catch(\Exception $e) { |
|
| 27 | + } catch (\Exception $e) { |
|
| 28 | 28 | throw new ArgumentFormatException("Unable to read the file '{$path}'"); |
| 29 | 29 | } |
| 30 | - if (json_last_error() !== JSON_ERROR_NONE){ |
|
| 30 | + if (json_last_error() !== JSON_ERROR_NONE) { |
|
| 31 | 31 | throw new ArgumentFormatException("Error while parsing the JSON file '{$path}'"); |
| 32 | 32 | } |
| 33 | 33 | $this->loaded[$name] = $this->buildFormat($json); |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | return $format; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - protected function fillObject($obj, $firstLevel=false) |
|
| 84 | + protected function fillObject($obj, $firstLevel = false) |
|
| 85 | 85 | { |
| 86 | 86 | $format = new ArgumentFormat; |
| 87 | 87 | |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | if (isset($obj->separator)) { |
| 108 | 108 | $format->separator = $obj->separator; |
| 109 | 109 | } elseif ($firstLevel) { |
| 110 | - $format->separator = ($format->type === 'object') ? ':':','; |
|
| 110 | + $format->separator = ($format->type === 'object') ? ':' : ','; |
|
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | |
@@ -7,140 +7,140 @@ |
||
| 7 | 7 | |
| 8 | 8 | class ArgumentFormatLoader { |
| 9 | 9 | |
| 10 | - protected $fs; |
|
| 11 | - |
|
| 12 | - protected $loaded; |
|
| 13 | - |
|
| 14 | - public function __construct(Filesystem $fs) |
|
| 15 | - { |
|
| 16 | - $this->fs = $fs; |
|
| 17 | - $this->loaded = []; |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - public function load($name) |
|
| 21 | - { |
|
| 22 | - if(! isset($this->loaded[$name])){ |
|
| 23 | - $path = __DIR__ . "/../../formats/{$name}.json"; |
|
| 24 | - $json = ""; |
|
| 25 | - try { |
|
| 26 | - $json = json_decode($this->fs->get($path)); |
|
| 27 | - } catch(\Exception $e) { |
|
| 28 | - throw new ArgumentFormatException("Unable to read the file '{$path}'"); |
|
| 29 | - } |
|
| 30 | - if (json_last_error() !== JSON_ERROR_NONE){ |
|
| 31 | - throw new ArgumentFormatException("Error while parsing the JSON file '{$path}'"); |
|
| 32 | - } |
|
| 33 | - $this->loaded[$name] = $this->buildFormat($json); |
|
| 34 | - } |
|
| 35 | - return $this->loaded[$name]; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - protected function buildFormat($obj) |
|
| 39 | - { |
|
| 40 | - return $this->fillFirstLevel($obj); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - protected function fillFirstLevel($obj) |
|
| 44 | - { |
|
| 45 | - return $this->fill($obj, true); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - protected function fill($obj, $firstLevel = false) |
|
| 49 | - { |
|
| 50 | - if (is_string($obj)) { |
|
| 51 | - return $this->fillString($obj, $firstLevel); |
|
| 52 | - } else { |
|
| 53 | - return $this->fillObject($obj, $firstLevel); |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - protected function fillString($string, $firstLevel = false) |
|
| 58 | - { |
|
| 59 | - list($name, $type, $isArray) = $this->parseName($string); |
|
| 10 | + protected $fs; |
|
| 11 | + |
|
| 12 | + protected $loaded; |
|
| 13 | + |
|
| 14 | + public function __construct(Filesystem $fs) |
|
| 15 | + { |
|
| 16 | + $this->fs = $fs; |
|
| 17 | + $this->loaded = []; |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + public function load($name) |
|
| 21 | + { |
|
| 22 | + if(! isset($this->loaded[$name])){ |
|
| 23 | + $path = __DIR__ . "/../../formats/{$name}.json"; |
|
| 24 | + $json = ""; |
|
| 25 | + try { |
|
| 26 | + $json = json_decode($this->fs->get($path)); |
|
| 27 | + } catch(\Exception $e) { |
|
| 28 | + throw new ArgumentFormatException("Unable to read the file '{$path}'"); |
|
| 29 | + } |
|
| 30 | + if (json_last_error() !== JSON_ERROR_NONE){ |
|
| 31 | + throw new ArgumentFormatException("Error while parsing the JSON file '{$path}'"); |
|
| 32 | + } |
|
| 33 | + $this->loaded[$name] = $this->buildFormat($json); |
|
| 34 | + } |
|
| 35 | + return $this->loaded[$name]; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + protected function buildFormat($obj) |
|
| 39 | + { |
|
| 40 | + return $this->fillFirstLevel($obj); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + protected function fillFirstLevel($obj) |
|
| 44 | + { |
|
| 45 | + return $this->fill($obj, true); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + protected function fill($obj, $firstLevel = false) |
|
| 49 | + { |
|
| 50 | + if (is_string($obj)) { |
|
| 51 | + return $this->fillString($obj, $firstLevel); |
|
| 52 | + } else { |
|
| 53 | + return $this->fillObject($obj, $firstLevel); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + protected function fillString($string, $firstLevel = false) |
|
| 58 | + { |
|
| 59 | + list($name, $type, $isArray) = $this->parseName($string); |
|
| 60 | 60 | |
| 61 | - $format = new ArgumentFormat; |
|
| 62 | - $format->name = $name; |
|
| 61 | + $format = new ArgumentFormat; |
|
| 62 | + $format->name = $name; |
|
| 63 | 63 | |
| 64 | - if ($isArray) { |
|
| 65 | - $format->type = 'array'; |
|
| 66 | - $subFormat = new ArgumentFormat; |
|
| 67 | - $subFormat->type = $type ?: 'string'; |
|
| 68 | - $format->format = $subFormat; |
|
| 69 | - } else { |
|
| 70 | - $format->type = 'string'; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if ($firstLevel) { |
|
| 74 | - if ($format->type === 'object') { |
|
| 75 | - $format->separator = ':'; |
|
| 76 | - } elseif ($format->type === 'array') { |
|
| 77 | - $format->separator = ','; |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - return $format; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - protected function fillObject($obj, $firstLevel=false) |
|
| 85 | - { |
|
| 86 | - $format = new ArgumentFormat; |
|
| 87 | - |
|
| 88 | - // Resolve type from name |
|
| 89 | - if (isset($obj->name)) { |
|
| 90 | - list($name, $type) = $this->parseName($obj->name); |
|
| 91 | - $format->name = $name; |
|
| 92 | - $format->type = $type; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // Fill type if set |
|
| 96 | - if (isset($obj->type)) { |
|
| 97 | - $format->type = $obj->type; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // Fill default if set |
|
| 101 | - if (isset($obj->default)) { |
|
| 102 | - $format->default = $obj->default; |
|
| 103 | - } |
|
| 64 | + if ($isArray) { |
|
| 65 | + $format->type = 'array'; |
|
| 66 | + $subFormat = new ArgumentFormat; |
|
| 67 | + $subFormat->type = $type ?: 'string'; |
|
| 68 | + $format->format = $subFormat; |
|
| 69 | + } else { |
|
| 70 | + $format->type = 'string'; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if ($firstLevel) { |
|
| 74 | + if ($format->type === 'object') { |
|
| 75 | + $format->separator = ':'; |
|
| 76 | + } elseif ($format->type === 'array') { |
|
| 77 | + $format->separator = ','; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + return $format; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + protected function fillObject($obj, $firstLevel=false) |
|
| 85 | + { |
|
| 86 | + $format = new ArgumentFormat; |
|
| 87 | + |
|
| 88 | + // Resolve type from name |
|
| 89 | + if (isset($obj->name)) { |
|
| 90 | + list($name, $type) = $this->parseName($obj->name); |
|
| 91 | + $format->name = $name; |
|
| 92 | + $format->type = $type; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // Fill type if set |
|
| 96 | + if (isset($obj->type)) { |
|
| 97 | + $format->type = $obj->type; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Fill default if set |
|
| 101 | + if (isset($obj->default)) { |
|
| 102 | + $format->default = $obj->default; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - // Set separator, default to ':' for objects and ',' for arrays in first level |
|
| 106 | - if (in_array($format->type, ['object', 'array'])) { |
|
| 107 | - if (isset($obj->separator)) { |
|
| 108 | - $format->separator = $obj->separator; |
|
| 109 | - } elseif ($firstLevel) { |
|
| 110 | - $format->separator = ($format->type === 'object') ? ':':','; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - // Build format recursively |
|
| 115 | - if (isset($obj->fields)) { |
|
| 116 | - if ($firstLevel) { |
|
| 117 | - if (is_array($obj->fields)) { |
|
| 118 | - $format->format = array_map([$this, 'fillFirstLevel'], $obj->fields); |
|
| 119 | - } else { |
|
| 120 | - $format->format = $this->fill($obj->fields, true); |
|
| 121 | - } |
|
| 122 | - } else { |
|
| 123 | - if (is_array($obj->fields)) { |
|
| 124 | - $format->format = array_map([$this, 'fill'], $obj->fields); |
|
| 125 | - } else { |
|
| 126 | - $format->format = $this->fill($obj->fields); |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - return $format; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - protected function parseName($name) |
|
| 135 | - { |
|
| 136 | - $pattern = '/^(?P<attr>\w+)(\[(?P<type>\w+)?\])?/'; |
|
| 137 | - preg_match($pattern, $name, $matches); |
|
| 138 | - |
|
| 139 | - $attr = $matches['attr']; |
|
| 140 | - $type = empty($matches['type']) ? null : $matches['type']; |
|
| 141 | - $isArray = (isset($matches[2][0]) && $matches[2][0] === '['); |
|
| 105 | + // Set separator, default to ':' for objects and ',' for arrays in first level |
|
| 106 | + if (in_array($format->type, ['object', 'array'])) { |
|
| 107 | + if (isset($obj->separator)) { |
|
| 108 | + $format->separator = $obj->separator; |
|
| 109 | + } elseif ($firstLevel) { |
|
| 110 | + $format->separator = ($format->type === 'object') ? ':':','; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + // Build format recursively |
|
| 115 | + if (isset($obj->fields)) { |
|
| 116 | + if ($firstLevel) { |
|
| 117 | + if (is_array($obj->fields)) { |
|
| 118 | + $format->format = array_map([$this, 'fillFirstLevel'], $obj->fields); |
|
| 119 | + } else { |
|
| 120 | + $format->format = $this->fill($obj->fields, true); |
|
| 121 | + } |
|
| 122 | + } else { |
|
| 123 | + if (is_array($obj->fields)) { |
|
| 124 | + $format->format = array_map([$this, 'fill'], $obj->fields); |
|
| 125 | + } else { |
|
| 126 | + $format->format = $this->fill($obj->fields); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + return $format; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + protected function parseName($name) |
|
| 135 | + { |
|
| 136 | + $pattern = '/^(?P<attr>\w+)(\[(?P<type>\w+)?\])?/'; |
|
| 137 | + preg_match($pattern, $name, $matches); |
|
| 138 | + |
|
| 139 | + $attr = $matches['attr']; |
|
| 140 | + $type = empty($matches['type']) ? null : $matches['type']; |
|
| 141 | + $isArray = (isset($matches[2][0]) && $matches[2][0] === '['); |
|
| 142 | 142 | |
| 143 | - return [$attr, $type, $isArray]; |
|
| 144 | - } |
|
| 143 | + return [$attr, $type, $isArray]; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | 146 | } |