Skip to content

Commit 4dd672a

Browse files
author
Nathan Esayeas
authored
Update regular expression to support windows (#146)
1 parent 3dc2b8e commit 4dd672a

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/Blueprint.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@ public static function relativeNamespace(string $fullyQualifiedClassName)
2626

2727
public function parse($content)
2828
{
29+
$content = str_replace(["\r\n", "\r"], "\n", $content);
30+
2931
$content = preg_replace_callback('/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi', function ($matches) {
30-
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
32+
return $matches[1].strtolower($matches[2]).': '.$matches[2];
3133
}, $content);
3234

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

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

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

4547
return Yaml::parse($content);

tests/Feature/BlueprintTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,51 @@ public function it_parses_the_readme_example()
213213
], $this->subject->parse($blueprint));
214214
}
215215

216+
/**
217+
* @test
218+
*/
219+
public function it_parses_the_readme_example_with_different_platform_eols()
220+
{
221+
$definition = $this->fixture('definitions/readme-example.bp');
222+
223+
$LF = "\n";
224+
$CR = "\r";
225+
$CRLF = "\r\n";
226+
227+
$definition_mac_eol = str_replace($LF, $CR, $definition);
228+
$definition_windows_eol = str_replace($LF, $CRLF, $definition);
229+
230+
$expected = [
231+
'models' => [
232+
'Post' => [
233+
'title' => 'string:400',
234+
'content' => 'longtext',
235+
'published_at' => 'nullable timestamp',
236+
],
237+
],
238+
'controllers' => [
239+
'Post' => [
240+
'index' => [
241+
'query' => 'all:posts',
242+
'render' => 'post.index with:posts',
243+
],
244+
'store' => [
245+
'validate' => 'title, content',
246+
'save' => 'post',
247+
'send' => 'ReviewNotification to:post.author with:post',
248+
'dispatch' => 'SyncMedia with:post',
249+
'fire' => 'NewPost with:post',
250+
'flash' => 'post.title',
251+
'redirect' => 'post.index',
252+
],
253+
],
254+
],
255+
];
256+
257+
$this->assertEquals($expected, $this->subject->parse($definition_mac_eol));
258+
$this->assertEquals($expected, $this->subject->parse($definition_windows_eol));
259+
}
260+
216261
/**
217262
* @test
218263
*/

0 commit comments

Comments
 (0)