Skip to content

Commit 68121bb

Browse files
author
ryan.deng
committed
updated
1 parent 712fc0a commit 68121bb

File tree

8 files changed

+196
-123
lines changed

8 files changed

+196
-123
lines changed

composer.json

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
{
2-
"name": "timehunter/laraveljsontoclassgenerator",
3-
"description": ":description",
4-
"license": "MIT",
5-
"authors": [
6-
{
7-
"name": "Ryan Deng",
8-
"email": "[email protected]",
9-
"homepage": "https://github.com/RyanDaDeng"
10-
}
11-
],
12-
"homepage": "https://github.com/timehunter/laraveljsontoclassgenerator",
13-
"keywords": ["Laravel", "LaravelJsonToClassGenerator"],
14-
"require": {
15-
"illuminate/support": "~5"
16-
},
17-
"require-dev": {
18-
"phpunit/phpunit": "~7.0",
19-
"mockery/mockery": "^1.1",
20-
"orchestra/testbench": "~3.0",
21-
"sempro/phpunit-pretty-print": "^1.0"
22-
},
23-
"autoload": {
24-
"psr-4": {
25-
"TimeHunter\\LaravelJsonToClassGenerator\\": "src/"
26-
}
27-
},
28-
"autoload-dev": {
29-
"psr-4": {
30-
"TimeHunter\\LaravelJsonToClassGenerator\\Tests\\": "tests"
31-
}
32-
},
33-
"extra": {
34-
"laravel": {
35-
"providers": [
36-
"TimeHunter\\LaravelJsonToClassGenerator\\LaravelJsonToClassGeneratorServiceProvider"
37-
],
38-
"aliases": {
39-
"LaravelJsonToClassGenerator": "TimeHunter\\LaravelJsonToClassGenerator\\Facades\\LaravelJsonToClassGenerator"
40-
}
41-
}
2+
"name": "timehunter/laraveljsontoclassgenerator",
3+
"description": "A generator that translates JSON or Array to be classes.",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Ryan Deng",
8+
"email": "[email protected]",
9+
"homepage": "https://github.com/RyanDaDeng"
4210
}
11+
],
12+
"homepage": "https://github.com/timehunter/laraveljsontoclassgenerator",
13+
"keywords": [
14+
"Laravel",
15+
"LaravelJsonToClassGenerator"
16+
],
17+
"require": {
18+
"illuminate/support": "~5"
19+
},
20+
"require-dev": {
21+
"nette/php-generator": "^3.2"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"TimeHunter\\LaravelJsonToClassGenerator\\": "src/"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"TimeHunter\\LaravelJsonToClassGenerator\\Tests\\": "tests"
31+
}
32+
},
33+
"extra": {
34+
"laravel": {
35+
"providers": [
36+
"TimeHunter\\LaravelJsonToClassGenerator\\LaravelJsonToClassGeneratorServiceProvider"
37+
],
38+
"aliases": {
39+
"LaravelJsonToClassGenerator": "TimeHunter\\LaravelJsonToClassGenerator\\Facades\\LaravelJsonToClassGenerator"
40+
}
41+
}
42+
}
4343
}

config/jsontoclassgenerator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
<?php
22

33
return [
4+
// namespace
45
'namespace' => 'App\Test',
6+
// default file store location
57
'file_location' => app_path(),
6-
'data' => [
8+
// this tells the generator which original source format that will be used for translation.
9+
'driver' => 'array',
10+
'array' => [
11+
# Please define an object instead of item
12+
# so each array needs to be defined with a key name, for example, 'message' is a key name
713
'message' => [
814
'author' => [
915
'first_name' => '',
@@ -12,5 +18,7 @@
1218
'text' => '',
1319
'date' => '2019-01-01'
1420
]
15-
]
21+
],
22+
# Remember to define an object with a key name
23+
'json' => "{}"
1624
];

readme.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@ $ php artisan make:jsontoclass
2424
````
2525
4. Check your files under your specified file location
2626

27-
## Change log
28-
29-
Please see the [changelog](changelog.md) for more information on what has changed recently.
30-
31-
32-
## Contributing
33-
34-
Please see [contributing.md](contributing.md) for details and a todolist.
35-
36-
## Security
37-
38-
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
39-
40-
41-
4227
## License
4328

4429
MIT. Please see the [license file](license.md) for more information.

src/Commands/JsonToClassGeneratorCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\Log;
7+
use TimeHunter\LaravelJsonToClassGenerator\JsonGeneratorFactory;
78
use TimeHunter\LaravelJsonToClassGenerator\Services\JsonToClassGenerator;
89

910
class JsonToClassGeneratorCommand extends Command
@@ -39,10 +40,10 @@ public function __construct()
3940
*/
4041
public function handle()
4142
{
42-
try{
43-
JsonToClassGenerator::generate(config('jsontoclassgenerator.data'));
43+
try {
44+
JsonGeneratorFactory::generate(config('jsontoclassgenerator.driver'));
4445
$this->info('Classes generated. Please check them.');
45-
}catch(\Exception $e){
46+
} catch (\Exception $e) {
4647
Log::error($e);
4748
$this->warn('Error happens, please check log file.');
4849
}

src/JsonGeneratorFactory.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace TimeHunter\LaravelJsonToClassGenerator;
4+
5+
use TimeHunter\LaravelJsonToClassGenerator\Services\ArrayToClassGenerator;
6+
7+
use Exception;
8+
use TimeHunter\LaravelJsonToClassGenerator\Services\JsonToClassGenerator;
9+
10+
class JsonGeneratorFactory
11+
{
12+
13+
/**
14+
* @param $driver
15+
* @throws \Exception
16+
*/
17+
public static function generate($driver)
18+
{
19+
20+
switch ($driver) {
21+
case 'array':
22+
(new ArrayToClassGenerator())->generate();
23+
break;
24+
case 'json':
25+
(new JsonToClassGenerator())->generate();
26+
break;
27+
default:
28+
throw new Exception('Driver: ' . $driver . ' is not found.');
29+
}
30+
}
31+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace TimeHunter\LaravelJsonToClassGenerator\Services;
4+
5+
6+
use Illuminate\Support\Facades\File;
7+
use Nette\PhpGenerator\PhpFile;
8+
9+
/**
10+
* Class JsonToClassGenerator
11+
* @package TimeHunter\LaravelJsonToClassGenerator\Services
12+
*/
13+
abstract class AbstractClassGenerator
14+
{
15+
/**
16+
* @return array
17+
*/
18+
abstract protected function getData(): array;
19+
20+
/**
21+
* Generate classes
22+
*/
23+
public function generate()
24+
{
25+
$this->recursiveCreateFile($this->getData(), config('jsontoclassgenerator.namespace'));
26+
}
27+
28+
/**
29+
* @param $sample
30+
* @param $namespaceString
31+
*/
32+
private function recursiveCreateFile($sample, $namespaceString)
33+
{
34+
foreach ($sample as $key => $data) {
35+
$className = $this->convertClassName($key);
36+
$phpFile = new PhpFile();
37+
$namespace = $phpFile->addNamespace($namespaceString);
38+
$class = $namespace->addClass($className);
39+
40+
$toArray = "return [\n";
41+
foreach ($data as $itemKey => $item) {
42+
$camelCase = $this->convert($itemKey);
43+
$class->addProperty($camelCase);
44+
if (is_array($item)) {
45+
46+
$subClassName = $this->convertClassName($itemKey);
47+
48+
$toArray .= " '$itemKey' => " . 'collect($this->' . $camelCase . ')->map(function (' . "$subClassName" . ' $data){
49+
return $data->toArray();
50+
})->toArray()' . ',' . "\n";
51+
$this->recursiveCreateFile([$itemKey => $item], $namespaceString);
52+
} else {
53+
$toArray .= " '$itemKey' => " . '$this->' . $camelCase . ',' . "\n";
54+
}
55+
}
56+
$toArray .= "];";
57+
$class->addMethod('toArray')->setReturnType('array')->setBody($toArray); // method return type;
58+
59+
$file = $className . '.php';
60+
61+
$location = config('jsontoclassgenerator.file_location') . '/' . $file;
62+
File::put($location, $phpFile);
63+
}
64+
}
65+
66+
/**
67+
* @param $string
68+
* @return string
69+
*/
70+
private function convert($string)
71+
{
72+
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
73+
74+
return lcfirst($str);
75+
}
76+
77+
/**
78+
* @param $string
79+
* @return mixed
80+
*/
81+
private function convertClassName($string)
82+
{
83+
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
84+
85+
return $str;
86+
}
87+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace TimeHunter\LaravelJsonToClassGenerator\Services;
4+
5+
6+
/**
7+
* Class JsonToClassGenerator
8+
* @package TimeHunter\LaravelJsonToClassGenerator\Services
9+
*/
10+
class ArrayToClassGenerator extends AbstractClassGenerator
11+
{
12+
13+
/**
14+
* @return array
15+
*/
16+
public function getData(): array
17+
{
18+
return config('jsontoclassgenerator.array');
19+
}
20+
}

src/Services/JsonToClassGenerator.php

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,15 @@
1010
* Class JsonToClassGenerator
1111
* @package TimeHunter\LaravelJsonToClassGenerator\Services
1212
*/
13-
class JsonToClassGenerator
13+
class JsonToClassGenerator extends AbstractClassGenerator
1414
{
1515

1616
/**
17-
* @param $data
17+
* @return array
1818
*/
19-
public static function generate($data)
19+
public function getData(): array
2020
{
21-
(new self)->recursiveCreateFile($data, config('jsontoclassgenerator.namespace'));
21+
return json_decode(config('jsontoclassgenerator.json'),1);
2222
}
2323

24-
/**
25-
* @param $sample
26-
* @param $namespaceString
27-
*/
28-
public function recursiveCreateFile($sample, $namespaceString)
29-
{
30-
foreach ($sample as $key => $data) {
31-
$className = $this->convertClassName($key);
32-
$phpFile = new PhpFile();
33-
$namespace = $phpFile->addNamespace($namespaceString);
34-
$class = $namespace->addClass($className);
35-
36-
$toArray = "return [\n";
37-
foreach ($data as $itemKey => $item) {
38-
$camelCase = $this->convert($itemKey);
39-
$class->addProperty($camelCase);
40-
if (is_array($item)) {
41-
42-
$subClassName = $this->convertClassName($itemKey);
43-
44-
$toArray .= " '$itemKey' => " . 'collect($this->' . $camelCase . ')->map(function (' . "$subClassName" . ' $data){
45-
return $data->toArray();
46-
})->toArray()' . ',' . "\n";
47-
$this->recursiveCreateFile([$itemKey => $item], $namespaceString);
48-
} else {
49-
$toArray .= " '$itemKey' => " . '$this->' . $camelCase . ',' . "\n";
50-
}
51-
}
52-
$toArray .= "];";
53-
$class->addMethod('toArray')->setReturnType('array')->setBody($toArray); // method return type;
54-
55-
$file = $className . '.php';
56-
57-
$location = config('jsontoclassgenerator.file_location') . '/' . $file;
58-
File::put($location, $phpFile);
59-
}
60-
}
61-
62-
/**
63-
* @param $string
64-
* @return string
65-
*/
66-
public function convert($string)
67-
{
68-
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
69-
70-
return lcfirst($str);
71-
}
72-
73-
/**
74-
* @param $string
75-
* @return mixed
76-
*/
77-
public function convertClassName($string)
78-
{
79-
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
80-
81-
return $str;
82-
}
8324
}

0 commit comments

Comments
 (0)