Skip to content

Commit

Permalink
Update regular expression to support windows (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Esayeas authored Apr 27, 2020
1 parent 3dc2b8e commit 4dd672a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ public static function relativeNamespace(string $fullyQualifiedClassName)

public function parse($content)
{
$content = str_replace(["\r\n", "\r"], "\n", $content);

$content = preg_replace_callback('/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi', function ($matches) {
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
return $matches[1].strtolower($matches[2]).': '.$matches[2];
}, $content);

$content = preg_replace_callback('/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?): true$/mi', function ($matches) {
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
return $matches[1].strtolower($matches[2]).': '.$matches[2];
}, $content);

$content = preg_replace_callback('/^(\s+)resource(: true)?$/mi', function ($matches) {
return $matches[1] . 'resource: all';
return $matches[1].'resource: all';
}, $content);

$content = preg_replace_callback('/^(\s+)uuid(: true)?$/mi', function ($matches) {
return $matches[1] . 'id: uuid primary';
return $matches[1].'id: uuid primary';
}, $content);

return Yaml::parse($content);
Expand Down
45 changes: 45 additions & 0 deletions tests/Feature/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,51 @@ public function it_parses_the_readme_example()
], $this->subject->parse($blueprint));
}

/**
* @test
*/
public function it_parses_the_readme_example_with_different_platform_eols()
{
$definition = $this->fixture('definitions/readme-example.bp');

$LF = "\n";
$CR = "\r";
$CRLF = "\r\n";

$definition_mac_eol = str_replace($LF, $CR, $definition);
$definition_windows_eol = str_replace($LF, $CRLF, $definition);

$expected = [
'models' => [
'Post' => [
'title' => 'string:400',
'content' => 'longtext',
'published_at' => 'nullable timestamp',
],
],
'controllers' => [
'Post' => [
'index' => [
'query' => 'all:posts',
'render' => 'post.index with:posts',
],
'store' => [
'validate' => 'title, content',
'save' => 'post',
'send' => 'ReviewNotification to:post.author with:post',
'dispatch' => 'SyncMedia with:post',
'fire' => 'NewPost with:post',
'flash' => 'post.title',
'redirect' => 'post.index',
],
],
],
];

$this->assertEquals($expected, $this->subject->parse($definition_mac_eol));
$this->assertEquals($expected, $this->subject->parse($definition_windows_eol));
}

/**
* @test
*/
Expand Down

0 comments on commit 4dd672a

Please sign in to comment.