Skip to content

Commit 4e66eea

Browse files
Generate basic code for Eloquent update statements (#190)
1 parent 8704357 commit 4e66eea

File tree

7 files changed

+83
-3
lines changed

7 files changed

+83
-3
lines changed

src/Lexers/StatementLexer.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ public function analyze(array $tokens): array
5353
$statements[] = $this->analyzeResource($statement);
5454
break;
5555
case 'save':
56-
case 'update':
5756
case 'delete':
5857
case 'find':
5958
$statements[] = new EloquentStatement($command, $statement);
6059
break;
60+
case 'update':
61+
$statements[] = $this->analyzeUpdate($statement);
62+
break;
6163
case 'flash':
6264
case 'store':
6365
$statements[] = new SessionStatement($command, $statement);
@@ -179,4 +181,15 @@ private function analyzeResource($statement)
179181

180182
return new ResourceStatement($reference, !is_null($collection), $collection === 'paginate');
181183
}
184+
185+
private function analyzeUpdate($statement)
186+
{
187+
if (!Str::contains($statement, ',')) {
188+
return new EloquentStatement('update', $statement);
189+
}
190+
191+
$columns = preg_split('/,([ \t]+)?/', $statement);
192+
193+
return new EloquentStatement('update', null, $columns);
194+
}
182195
}

src/Models/Statements/EloquentStatement.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,33 @@ class EloquentStatement
1717
*/
1818
private $reference;
1919

20-
public function __construct(string $operation, string $reference)
20+
/**
21+
* @var array
22+
*/
23+
private $columns;
24+
25+
public function __construct(string $operation, ?string $reference, array $columns = [])
2126
{
2227
$this->operation = $operation;
2328
$this->reference = $reference;
29+
$this->columns = $columns;
2430
}
2531

2632
public function operation(): string
2733
{
2834
return $this->operation;
2935
}
3036

31-
public function reference(): string
37+
public function reference(): ?string
3238
{
3339
return $this->reference;
3440
}
3541

42+
public function columns(): array
43+
{
44+
return $this->columns;
45+
}
46+
3647
public function output(string $controller_prefix, string $context): string
3748
{
3849
$model = $this->determineModel($controller_prefix);
@@ -49,6 +60,17 @@ public function output(string $controller_prefix, string $context): string
4960
}
5061
}
5162

63+
if ($this->operation() == 'update') {
64+
$columns = '';
65+
if (!empty($this->columns())) {
66+
$columns = implode(', ', array_map(function ($column) {
67+
return sprintf("'%s' => \$%s", $column, $column);
68+
}, $this->columns()));
69+
}
70+
71+
$code = "$" . Str::camel($model) . '->update([' . $columns . ']);';
72+
}
73+
5274
if ($this->operation() == 'find') {
5375
if ($this->usesQualifiedReference()) {
5476
$model = $this->extractModel();

tests/Feature/Lexers/StatementLexerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,25 @@ public function it_returns_an_eloquent_statement($operation, $reference)
261261
$this->assertSame($reference, $actual[0]->reference());
262262
}
263263

264+
/**
265+
* @test
266+
*/
267+
public function it_returns_an_update_eloquent_statement_with_columns()
268+
{
269+
$tokens = [
270+
'update' => 'name, title, age'
271+
];
272+
273+
$actual = $this->subject->analyze($tokens);
274+
275+
$this->assertCount(1, $actual);
276+
$this->assertInstanceOf(EloquentStatement::class, $actual[0]);
277+
278+
$this->assertSame('update', $actual[0]->operation());
279+
$this->assertNull($actual[0]->reference());
280+
$this->assertSame(['name', 'title', 'age'], $actual[0]->columns());
281+
}
282+
264283
/**
265284
* @test
266285
* @dataProvider sessionTokensProvider

tests/Unit/Models/Statements/EloquentStatementTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ public function output_generates_code_for_save_using_create()
5050
$this->assertEquals('$post = Post::create($request->all());', $subject->output('', 'store'));
5151
}
5252

53+
/**
54+
* @test
55+
*/
56+
public function output_generates_code_for_update_using_model()
57+
{
58+
$subject = new EloquentStatement('update', 'post');
59+
60+
$this->assertEquals('$post->update([]);', $subject->output('', ''));
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function output_generates_code_for_update_using_column_list()
67+
{
68+
$subject = new EloquentStatement('update', null, ['name', 'title', 'age']);
69+
70+
$this->assertEquals('$user->update([\'name\' => $name, \'title\' => $title, \'age\' => $age]);', $subject->output('User', ''));
71+
}
72+
5373
/**
5474
* @test
5575
*/

tests/fixtures/controllers/certificate-controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function show(Request $request, Certificate $certificate)
5050
*/
5151
public function update(CertificateUpdateRequest $request, Certificate $certificate)
5252
{
53+
$certificate->update([]);
54+
5355
return new CertificateResource($certificate);
5456
}
5557

tests/fixtures/controllers/certificate-type-controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function show(Request $request, CertificateType $certificateType)
5050
*/
5151
public function update(CertificateTypeUpdateRequest $request, CertificateType $certificateType)
5252
{
53+
$certificateType->update([]);
54+
5355
return new CertificateTypeResource($certificateType);
5456
}
5557

tests/fixtures/controllers/custom-models-namespace.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function show(Request $request, Tag $tag)
5050
*/
5151
public function update(TagUpdateRequest $request, Tag $tag)
5252
{
53+
$tag->update([]);
54+
5355
return new TagResource($tag);
5456
}
5557

0 commit comments

Comments
 (0)