/
StatementGenerator.php
78 lines (65 loc) · 2.19 KB
/
StatementGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Blueprint\Generators;
use Blueprint\Contracts\Generator;
abstract class StatementGenerator extends AbstractClassGenerator implements Generator
{
protected function populateConstructor(string $type, $statement): string
{
$constructor = str_replace('{{ type }}', $type, $this->filesystem->stub('constructor.stub'));
if (empty($statement->data())) {
$stub = str_replace('{{ body }}', '//', $constructor);
} else {
$stub = $this->buildProperties($statement->data()) . PHP_EOL . PHP_EOL;
$stub .= str_replace('__construct()', '__construct(' . $this->buildParameters($statement->data()) . ')', $constructor);
$stub = str_replace('{{ body }}', $this->buildAssignments($statement->data()), $stub);
}
return trim($stub);
}
protected function buildProperties(array $data): string
{
if (config('blueprint.property_promotion')) {
return '';
}
return trim(
array_reduce(
$data,
function ($output, $property) {
$output .= ' public $' . $property . ';' . PHP_EOL . PHP_EOL;
return $output;
},
''
)
);
}
protected function buildParameters(array $data): string
{
if (config('blueprint.property_promotion')) {
$parameters = array_map(
fn ($parameter) => 'public $' . $parameter,
$data
);
} else {
$parameters = array_map(
fn ($parameter) => '$' . $parameter,
$data
);
}
return implode(', ', $parameters);
}
protected function buildAssignments(array $data): string
{
if (config('blueprint.property_promotion')) {
return '//';
}
return trim(
array_reduce(
$data,
function ($output, $property) {
$output .= ' $this->' . $property . ' = $' . $property . ';' . PHP_EOL;
return $output;
},
''
)
);
}
}