@@ -3,32 +3,32 @@ |
||
3 | 3 | |
4 | 4 | class ControllerCommand extends BaseCommand { |
5 | 5 | |
6 | - protected $signature = 'wn:controller |
|
6 | + protected $signature = 'wn:controller |
|
7 | 7 | {model : Name of the model (with namespace if not App)} |
8 | 8 | {--no-routes= : without routes} |
9 | 9 | {--force= : override the existing files} |
10 | 10 | '; |
11 | 11 | |
12 | - protected $description = 'Generates RESTful controller using the RESTActions trait'; |
|
12 | + protected $description = 'Generates RESTful controller using the RESTActions trait'; |
|
13 | 13 | |
14 | 14 | public function handle() |
15 | 15 | { |
16 | - $model = $this->argument('model'); |
|
17 | - $name = ''; |
|
18 | - if(strrpos($model, "\\") === false){ |
|
19 | - $name = $model; |
|
20 | - $model = "App\\" . $model; |
|
21 | - } else { |
|
22 | - $name = explode("\\", $model); |
|
23 | - $name = $name[count($name) - 1]; |
|
24 | - } |
|
16 | + $model = $this->argument('model'); |
|
17 | + $name = ''; |
|
18 | + if(strrpos($model, "\\") === false){ |
|
19 | + $name = $model; |
|
20 | + $model = "App\\" . $model; |
|
21 | + } else { |
|
22 | + $name = explode("\\", $model); |
|
23 | + $name = $name[count($name) - 1]; |
|
24 | + } |
|
25 | 25 | $controller = ucwords(str_plural($name)) . 'Controller'; |
26 | 26 | $content = $this->getTemplate('controller') |
27 | - ->with([ |
|
28 | - 'name' => $controller, |
|
29 | - 'model' => $model |
|
30 | - ]) |
|
31 | - ->get(); |
|
27 | + ->with([ |
|
28 | + 'name' => $controller, |
|
29 | + 'model' => $model |
|
30 | + ]) |
|
31 | + ->get(); |
|
32 | 32 | |
33 | 33 | $this->save($content, "./app/Http/Controllers/{$controller}.php", "{$controller}"); |
34 | 34 |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | { |
16 | 16 | $model = $this->argument('model'); |
17 | 17 | $name = ''; |
18 | - if(strrpos($model, "\\") === false){ |
|
18 | + if (strrpos($model, "\\") === false) { |
|
19 | 19 | $name = $model; |
20 | 20 | $model = "App\\" . $model; |
21 | 21 | } else { |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | |
33 | 33 | $this->save($content, "./app/Http/Controllers/{$controller}.php", "{$controller}"); |
34 | 34 | |
35 | - if(! $this->option('no-routes')){ |
|
35 | + if (!$this->option('no-routes')) { |
|
36 | 36 | $this->call('wn:route', [ |
37 | 37 | 'resource' => snake_case($name, '-'), |
38 | 38 | '--controller' => $controller |
@@ -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 | { |
@@ -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 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | |
4 | 4 | class MigrationCommand extends BaseCommand { |
5 | 5 | |
6 | - protected $signature = 'wn:migration |
|
6 | + protected $signature = 'wn:migration |
|
7 | 7 | {table : The table name.} |
8 | 8 | {--schema= : the schema.} |
9 | 9 | {--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.} |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | // {action : One of create, add, remove or drop options.} |
16 | 16 | // The action is only create for the moment |
17 | 17 | |
18 | - protected $description = 'Generates a migration to create a table with schema'; |
|
18 | + protected $description = 'Generates a migration to create a table with schema'; |
|
19 | 19 | |
20 | 20 | public function handle() |
21 | 21 | { |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | ->get(); |
34 | 34 | |
35 | 35 | $file = $this->option('file'); |
36 | - if(! $file){ |
|
36 | + if (!$file) { |
|
37 | 37 | $file = date('Y_m_d_His_') . snake_case($name) . '_table'; |
38 | 38 | } |
39 | 39 | |
@@ -43,12 +43,12 @@ discard block |
||
43 | 43 | protected function getSchema() |
44 | 44 | { |
45 | 45 | $schema = $this->option('schema'); |
46 | - if(! $schema){ |
|
46 | + if (!$schema) { |
|
47 | 47 | return $this->spaces(12) . "// Schema declaration"; |
48 | 48 | } |
49 | 49 | |
50 | 50 | $items = $schema; |
51 | - if( ! $this->option('parsed')){ |
|
51 | + if (!$this->option('parsed')) { |
|
52 | 52 | $items = $this->getArgumentParser('schema')->parse($schema); |
53 | 53 | } |
54 | 54 | |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $name = $parts[0]['name']; |
83 | 83 | $parts[1]['args'] = array_merge(["'{$name}'"], $parts[1]['args']); |
84 | 84 | unset($parts[0]); |
85 | - $parts = array_map(function($part){ |
|
85 | + $parts = array_map(function($part) { |
|
86 | 86 | return '->' . $part['name'] . '(' . implode(', ', $part['args']) . ')'; |
87 | 87 | }, $parts); |
88 | 88 | return " \$table" . implode('', $parts) . ';'; |
@@ -91,12 +91,12 @@ discard block |
||
91 | 91 | protected function getConstraints() |
92 | 92 | { |
93 | 93 | $keys = $this->option('keys'); |
94 | - if(! $keys){ |
|
94 | + if (!$keys) { |
|
95 | 95 | return $this->spaces(12) . "// Constraints declaration"; |
96 | 96 | } |
97 | 97 | |
98 | 98 | $items = $keys; |
99 | - if(! $this->option('parsed')){ |
|
99 | + if (!$this->option('parsed')) { |
|
100 | 100 | $items = $this->getArgumentParser('foreign-keys')->parse($keys); |
101 | 101 | } |
102 | 102 | |
@@ -110,10 +110,10 @@ discard block |
||
110 | 110 | |
111 | 111 | protected function getConstraintDeclaration($key) |
112 | 112 | { |
113 | - if(! $key['column']){ |
|
113 | + if (!$key['column']) { |
|
114 | 114 | $key['column'] = 'id'; |
115 | 115 | } |
116 | - if(! $key['table']){ |
|
116 | + if (!$key['table']) { |
|
117 | 117 | $key['table'] = str_plural(substr($key['name'], 0, count($key['name']) - 4)); |
118 | 118 | } |
119 | 119 | |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | ]) |
126 | 126 | ->get(); |
127 | 127 | |
128 | - if($key['on_delete']){ |
|
128 | + if ($key['on_delete']) { |
|
129 | 129 | $constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint') |
130 | 130 | ->with([ |
131 | 131 | 'event' => 'Delete', |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | ->get(); |
135 | 135 | } |
136 | 136 | |
137 | - if($key['on_update']){ |
|
137 | + if ($key['on_update']) { |
|
138 | 138 | $constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint') |
139 | 139 | ->with([ |
140 | 140 | 'event' => 'Update', |
@@ -21,85 +21,85 @@ |
||
21 | 21 | // $this->registerTestCommand(); |
22 | 22 | } |
23 | 23 | |
24 | - protected function registerModelCommand(){ |
|
25 | - $this->app->singleton('command.wn.model', function($app){ |
|
24 | + protected function registerModelCommand() { |
|
25 | + $this->app->singleton('command.wn.model', function($app) { |
|
26 | 26 | return $app['Wn\Generators\Commands\ModelCommand']; |
27 | 27 | }); |
28 | 28 | $this->commands('command.wn.model'); |
29 | 29 | } |
30 | 30 | |
31 | - protected function registerControllerRestActionsCommand(){ |
|
32 | - $this->app->singleton('command.wn.controller.rest-actions', function($app){ |
|
31 | + protected function registerControllerRestActionsCommand() { |
|
32 | + $this->app->singleton('command.wn.controller.rest-actions', function($app) { |
|
33 | 33 | return $app['Wn\Generators\Commands\ControllerRestActionsCommand']; |
34 | 34 | }); |
35 | 35 | $this->commands('command.wn.controller.rest-actions'); |
36 | 36 | } |
37 | 37 | |
38 | - protected function registerControllerCommand(){ |
|
39 | - $this->app->singleton('command.wn.controller', function($app){ |
|
38 | + protected function registerControllerCommand() { |
|
39 | + $this->app->singleton('command.wn.controller', function($app) { |
|
40 | 40 | return $app['Wn\Generators\Commands\ControllerCommand']; |
41 | 41 | }); |
42 | 42 | $this->commands('command.wn.controller'); |
43 | 43 | } |
44 | 44 | |
45 | - protected function registerMigrationCommand(){ |
|
46 | - $this->app->singleton('command.wn.migration', function($app){ |
|
45 | + protected function registerMigrationCommand() { |
|
46 | + $this->app->singleton('command.wn.migration', function($app) { |
|
47 | 47 | return $app['Wn\Generators\Commands\MigrationCommand']; |
48 | 48 | }); |
49 | 49 | $this->commands('command.wn.migration'); |
50 | 50 | } |
51 | 51 | |
52 | - protected function registerRouteCommand(){ |
|
53 | - $this->app->singleton('command.wn.route', function($app){ |
|
52 | + protected function registerRouteCommand() { |
|
53 | + $this->app->singleton('command.wn.route', function($app) { |
|
54 | 54 | return $app['Wn\Generators\Commands\RouteCommand']; |
55 | 55 | }); |
56 | 56 | $this->commands('command.wn.route'); |
57 | 57 | } |
58 | 58 | |
59 | - protected function registerTestCommand(){ |
|
60 | - $this->app->singleton('command.wn.test', function($app){ |
|
59 | + protected function registerTestCommand() { |
|
60 | + $this->app->singleton('command.wn.test', function($app) { |
|
61 | 61 | return $app['Wn\Generators\Commands\TestCommand']; |
62 | 62 | }); |
63 | 63 | $this->commands('command.wn.test'); |
64 | 64 | } |
65 | 65 | |
66 | - protected function registerResourceCommand(){ |
|
67 | - $this->app->singleton('command.wn.resource', function($app){ |
|
66 | + protected function registerResourceCommand() { |
|
67 | + $this->app->singleton('command.wn.resource', function($app) { |
|
68 | 68 | return $app['Wn\Generators\Commands\ResourceCommand']; |
69 | 69 | }); |
70 | 70 | $this->commands('command.wn.resource'); |
71 | 71 | } |
72 | 72 | |
73 | - protected function registerResourcesCommand(){ |
|
74 | - $this->app->singleton('command.wn.resources', function($app){ |
|
73 | + protected function registerResourcesCommand() { |
|
74 | + $this->app->singleton('command.wn.resources', function($app) { |
|
75 | 75 | return $app['Wn\Generators\Commands\ResourcesCommand']; |
76 | 76 | }); |
77 | 77 | $this->commands('command.wn.resources'); |
78 | 78 | } |
79 | 79 | |
80 | - protected function registerPivotTableCommand(){ |
|
81 | - $this->app->singleton('command.wn.pivot-table', function($app){ |
|
80 | + protected function registerPivotTableCommand() { |
|
81 | + $this->app->singleton('command.wn.pivot-table', function($app) { |
|
82 | 82 | return $app['Wn\Generators\Commands\PivotTableCommand']; |
83 | 83 | }); |
84 | 84 | $this->commands('command.wn.pivot-table'); |
85 | 85 | } |
86 | 86 | |
87 | - protected function registerFactoryCommand(){ |
|
88 | - $this->app->singleton('command.wn.factory', function($app){ |
|
87 | + protected function registerFactoryCommand() { |
|
88 | + $this->app->singleton('command.wn.factory', function($app) { |
|
89 | 89 | return $app['Wn\Generators\Commands\FactoryCommand']; |
90 | 90 | }); |
91 | 91 | $this->commands('command.wn.factory'); |
92 | 92 | } |
93 | 93 | |
94 | - protected function registerSeederCommand(){ |
|
95 | - $this->app->singleton('command.wn.seeder', function($app){ |
|
94 | + protected function registerSeederCommand() { |
|
95 | + $this->app->singleton('command.wn.seeder', function($app) { |
|
96 | 96 | return $app['Wn\Generators\Commands\SeederCommand']; |
97 | 97 | }); |
98 | 98 | $this->commands('command.wn.seeder'); |
99 | 99 | } |
100 | 100 | |
101 | - protected function registerPivotSeederCommand(){ |
|
102 | - $this->app->singleton('command.wn.pivot.seeder', function($app){ |
|
101 | + protected function registerPivotSeederCommand() { |
|
102 | + $this->app->singleton('command.wn.pivot.seeder', function($app) { |
|
103 | 103 | return $app['Wn\Generators\Commands\PivotSeederCommand']; |
104 | 104 | }); |
105 | 105 | $this->commands('command.wn.pivot.seeder'); |
@@ -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 = (@$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 = (@$matches[2][0] === '['); |
|
142 | 142 | |
143 | - return [$attr, $type, $isArray]; |
|
144 | - } |
|
143 | + return [$attr, $type, $isArray]; |
|
144 | + } |
|
145 | 145 | |
146 | 146 | } |
147 | 147 | \ No newline at end of file |
@@ -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 |
@@ -6,110 +6,110 @@ |
||
6 | 6 | |
7 | 7 | class ArgumentParser { |
8 | 8 | |
9 | - protected $format; |
|
10 | - |
|
11 | - public function __construct(ArgumentFormat $format) |
|
12 | - { |
|
13 | - $this->format = $format; |
|
14 | - } |
|
15 | - |
|
16 | - public function parse($args) |
|
17 | - { |
|
18 | - return $this->parseToken($args, $this->format); |
|
19 | - } |
|
20 | - |
|
21 | - protected function parseToken($token, ArgumentFormat $format){ |
|
22 | - switch($format->type) { |
|
23 | - case 'string': |
|
24 | - return $token; |
|
25 | - case 'number': |
|
26 | - return $this->parseNumber($token); |
|
27 | - case 'boolean': |
|
28 | - return $this->parseBoolean($token, $format->name); |
|
29 | - case 'array': |
|
30 | - return $this->parseArray($token, $format->separator, $format->format); |
|
31 | - case 'object': |
|
32 | - return $this->parseObject($token, $format->separator, $format->format); |
|
33 | - default: |
|
34 | - throw new ArgumentParserException("Unknown format type: '{$format->type}'"); |
|
35 | - } |
|
36 | - } |
|
37 | - |
|
38 | - protected function parseNumber($token) |
|
39 | - { |
|
40 | - if(! is_numeric($token)) { |
|
41 | - throw new ArgumentParserException("Unable to parse '{$token}' as number"); |
|
42 | - } |
|
43 | - return $token + 0; |
|
44 | - } |
|
45 | - |
|
46 | - protected function parseBoolean($token, $name) |
|
47 | - { |
|
48 | - if(in_array($token, ['yes', 'true', '1', $name])) { |
|
49 | - return true; |
|
50 | - } else if(in_array($token, ['no', 'false', '0', "!{$name}"])){ |
|
51 | - return false; |
|
52 | - } else { |
|
53 | - return null; |
|
54 | - } |
|
55 | - } |
|
56 | - |
|
57 | - protected function parseArray($token, $separator, ArgumentFormat $format) |
|
58 | - { |
|
59 | - $result = []; |
|
60 | - $tokens = explode($separator, $token); |
|
61 | - foreach($tokens as $value) { |
|
62 | - array_push($result, $this->parseToken($value, $format)); |
|
63 | - } |
|
64 | - return $result; |
|
65 | - } |
|
66 | - |
|
67 | - protected function parseObject($token, $separator, $fields) |
|
68 | - { |
|
69 | - $result = []; |
|
70 | - $tokens = explode($separator, $token); |
|
71 | - $tokensNumber = count($tokens); |
|
72 | - |
|
73 | - $requiredFieldsIndexes = []; |
|
74 | - $optionalFieldsIndexes = []; |
|
75 | - foreach($fields as $index => $format) { |
|
76 | - if($format->default === null) { |
|
77 | - array_push($requiredFieldsIndexes, $index); |
|
78 | - } else { |
|
79 | - array_push($optionalFieldsIndexes, $index); |
|
80 | - } |
|
81 | - } |
|
82 | - $requiredFieldsIndexesNumber = count($requiredFieldsIndexes); |
|
83 | - |
|
84 | - if($tokensNumber < $requiredFieldsIndexesNumber) { |
|
85 | - $requiredFields = array_map(function($index) use ($fields) { |
|
86 | - return $fields[$index]->name; |
|
87 | - }, $requiredFieldsIndexes); |
|
88 | - $requiredFields = implode($separator, $requiredFields); |
|
89 | - throw new ArgumentParserException("Required field missing: {$tokensNumber} given " |
|
90 | - . "({$token}) but {$requiredFieldsIndexesNumber} required ({$requiredFields})"); |
|
91 | - } |
|
92 | - |
|
93 | - $givenOptionalFieldsIndexes = array_slice( |
|
94 | - $optionalFieldsIndexes, 0, $tokensNumber - $requiredFieldsIndexesNumber); |
|
95 | - $notPresentFieldsIndexes = array_slice( |
|
96 | - $optionalFieldsIndexes, $tokensNumber - $requiredFieldsIndexesNumber); |
|
97 | - $givenFieldsIndexes = array_merge($requiredFieldsIndexes, $givenOptionalFieldsIndexes); |
|
98 | - sort($givenFieldsIndexes); |
|
99 | - |
|
100 | - // Fill the given fields |
|
101 | - for($i = 0; $i < $tokensNumber; $i ++) { |
|
102 | - $fieldFormat = $fields[$givenFieldsIndexes[$i]]; |
|
103 | - $result[$fieldFormat->name] = $this->parseToken($tokens[$i], $fieldFormat); |
|
104 | - } |
|
105 | - |
|
106 | - // Fill other fields with default values |
|
107 | - foreach($notPresentFieldsIndexes as $index) { |
|
108 | - $fieldFormat = $fields[$index]; |
|
109 | - $result[$fieldFormat->name] = $fieldFormat->default; |
|
110 | - } |
|
111 | - |
|
112 | - return $result; |
|
113 | - } |
|
9 | + protected $format; |
|
10 | + |
|
11 | + public function __construct(ArgumentFormat $format) |
|
12 | + { |
|
13 | + $this->format = $format; |
|
14 | + } |
|
15 | + |
|
16 | + public function parse($args) |
|
17 | + { |
|
18 | + return $this->parseToken($args, $this->format); |
|
19 | + } |
|
20 | + |
|
21 | + protected function parseToken($token, ArgumentFormat $format){ |
|
22 | + switch($format->type) { |
|
23 | + case 'string': |
|
24 | + return $token; |
|
25 | + case 'number': |
|
26 | + return $this->parseNumber($token); |
|
27 | + case 'boolean': |
|
28 | + return $this->parseBoolean($token, $format->name); |
|
29 | + case 'array': |
|
30 | + return $this->parseArray($token, $format->separator, $format->format); |
|
31 | + case 'object': |
|
32 | + return $this->parseObject($token, $format->separator, $format->format); |
|
33 | + default: |
|
34 | + throw new ArgumentParserException("Unknown format type: '{$format->type}'"); |
|
35 | + } |
|
36 | + } |
|
37 | + |
|
38 | + protected function parseNumber($token) |
|
39 | + { |
|
40 | + if(! is_numeric($token)) { |
|
41 | + throw new ArgumentParserException("Unable to parse '{$token}' as number"); |
|
42 | + } |
|
43 | + return $token + 0; |
|
44 | + } |
|
45 | + |
|
46 | + protected function parseBoolean($token, $name) |
|
47 | + { |
|
48 | + if(in_array($token, ['yes', 'true', '1', $name])) { |
|
49 | + return true; |
|
50 | + } else if(in_array($token, ['no', 'false', '0', "!{$name}"])){ |
|
51 | + return false; |
|
52 | + } else { |
|
53 | + return null; |
|
54 | + } |
|
55 | + } |
|
56 | + |
|
57 | + protected function parseArray($token, $separator, ArgumentFormat $format) |
|
58 | + { |
|
59 | + $result = []; |
|
60 | + $tokens = explode($separator, $token); |
|
61 | + foreach($tokens as $value) { |
|
62 | + array_push($result, $this->parseToken($value, $format)); |
|
63 | + } |
|
64 | + return $result; |
|
65 | + } |
|
66 | + |
|
67 | + protected function parseObject($token, $separator, $fields) |
|
68 | + { |
|
69 | + $result = []; |
|
70 | + $tokens = explode($separator, $token); |
|
71 | + $tokensNumber = count($tokens); |
|
72 | + |
|
73 | + $requiredFieldsIndexes = []; |
|
74 | + $optionalFieldsIndexes = []; |
|
75 | + foreach($fields as $index => $format) { |
|
76 | + if($format->default === null) { |
|
77 | + array_push($requiredFieldsIndexes, $index); |
|
78 | + } else { |
|
79 | + array_push($optionalFieldsIndexes, $index); |
|
80 | + } |
|
81 | + } |
|
82 | + $requiredFieldsIndexesNumber = count($requiredFieldsIndexes); |
|
83 | + |
|
84 | + if($tokensNumber < $requiredFieldsIndexesNumber) { |
|
85 | + $requiredFields = array_map(function($index) use ($fields) { |
|
86 | + return $fields[$index]->name; |
|
87 | + }, $requiredFieldsIndexes); |
|
88 | + $requiredFields = implode($separator, $requiredFields); |
|
89 | + throw new ArgumentParserException("Required field missing: {$tokensNumber} given " |
|
90 | + . "({$token}) but {$requiredFieldsIndexesNumber} required ({$requiredFields})"); |
|
91 | + } |
|
92 | + |
|
93 | + $givenOptionalFieldsIndexes = array_slice( |
|
94 | + $optionalFieldsIndexes, 0, $tokensNumber - $requiredFieldsIndexesNumber); |
|
95 | + $notPresentFieldsIndexes = array_slice( |
|
96 | + $optionalFieldsIndexes, $tokensNumber - $requiredFieldsIndexesNumber); |
|
97 | + $givenFieldsIndexes = array_merge($requiredFieldsIndexes, $givenOptionalFieldsIndexes); |
|
98 | + sort($givenFieldsIndexes); |
|
99 | + |
|
100 | + // Fill the given fields |
|
101 | + for($i = 0; $i < $tokensNumber; $i ++) { |
|
102 | + $fieldFormat = $fields[$givenFieldsIndexes[$i]]; |
|
103 | + $result[$fieldFormat->name] = $this->parseToken($tokens[$i], $fieldFormat); |
|
104 | + } |
|
105 | + |
|
106 | + // Fill other fields with default values |
|
107 | + foreach($notPresentFieldsIndexes as $index) { |
|
108 | + $fieldFormat = $fields[$index]; |
|
109 | + $result[$fieldFormat->name] = $fieldFormat->default; |
|
110 | + } |
|
111 | + |
|
112 | + return $result; |
|
113 | + } |
|
114 | 114 | |
115 | 115 | } |
116 | 116 | \ No newline at end of file |
@@ -18,8 +18,8 @@ discard block |
||
18 | 18 | return $this->parseToken($args, $this->format); |
19 | 19 | } |
20 | 20 | |
21 | - protected function parseToken($token, ArgumentFormat $format){ |
|
22 | - switch($format->type) { |
|
21 | + protected function parseToken($token, ArgumentFormat $format) { |
|
22 | + switch ($format->type) { |
|
23 | 23 | case 'string': |
24 | 24 | return $token; |
25 | 25 | case 'number': |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | |
38 | 38 | protected function parseNumber($token) |
39 | 39 | { |
40 | - if(! is_numeric($token)) { |
|
40 | + if (!is_numeric($token)) { |
|
41 | 41 | throw new ArgumentParserException("Unable to parse '{$token}' as number"); |
42 | 42 | } |
43 | 43 | return $token + 0; |
@@ -45,9 +45,9 @@ discard block |
||
45 | 45 | |
46 | 46 | protected function parseBoolean($token, $name) |
47 | 47 | { |
48 | - if(in_array($token, ['yes', 'true', '1', $name])) { |
|
48 | + if (in_array($token, ['yes', 'true', '1', $name])) { |
|
49 | 49 | return true; |
50 | - } else if(in_array($token, ['no', 'false', '0', "!{$name}"])){ |
|
50 | + } else if (in_array($token, ['no', 'false', '0', "!{$name}"])) { |
|
51 | 51 | return false; |
52 | 52 | } else { |
53 | 53 | return null; |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | { |
59 | 59 | $result = []; |
60 | 60 | $tokens = explode($separator, $token); |
61 | - foreach($tokens as $value) { |
|
61 | + foreach ($tokens as $value) { |
|
62 | 62 | array_push($result, $this->parseToken($value, $format)); |
63 | 63 | } |
64 | 64 | return $result; |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | |
73 | 73 | $requiredFieldsIndexes = []; |
74 | 74 | $optionalFieldsIndexes = []; |
75 | - foreach($fields as $index => $format) { |
|
76 | - if($format->default === null) { |
|
75 | + foreach ($fields as $index => $format) { |
|
76 | + if ($format->default === null) { |
|
77 | 77 | array_push($requiredFieldsIndexes, $index); |
78 | 78 | } else { |
79 | 79 | array_push($optionalFieldsIndexes, $index); |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | } |
82 | 82 | $requiredFieldsIndexesNumber = count($requiredFieldsIndexes); |
83 | 83 | |
84 | - if($tokensNumber < $requiredFieldsIndexesNumber) { |
|
84 | + if ($tokensNumber < $requiredFieldsIndexesNumber) { |
|
85 | 85 | $requiredFields = array_map(function($index) use ($fields) { |
86 | 86 | return $fields[$index]->name; |
87 | 87 | }, $requiredFieldsIndexes); |
@@ -98,13 +98,13 @@ discard block |
||
98 | 98 | sort($givenFieldsIndexes); |
99 | 99 | |
100 | 100 | // Fill the given fields |
101 | - for($i = 0; $i < $tokensNumber; $i ++) { |
|
101 | + for ($i = 0; $i < $tokensNumber; $i++) { |
|
102 | 102 | $fieldFormat = $fields[$givenFieldsIndexes[$i]]; |
103 | 103 | $result[$fieldFormat->name] = $this->parseToken($tokens[$i], $fieldFormat); |
104 | 104 | } |
105 | 105 | |
106 | 106 | // Fill other fields with default values |
107 | - foreach($notPresentFieldsIndexes as $index) { |
|
107 | + foreach ($notPresentFieldsIndexes as $index) { |
|
108 | 108 | $fieldFormat = $fields[$index]; |
109 | 109 | $result[$fieldFormat->name] = $fieldFormat->default; |
110 | 110 | } |