Skip to content

Commit fa654bf

Browse files
committed
user fields are now respected
1 parent 36017a6 commit fa654bf

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

src/LaraPress.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class LaraPress
1212
*/
1313
protected $meta = [];
1414

15+
/**
16+
* @var array
17+
*/
18+
protected $fields = [];
19+
1520
/**
1621
* LaraPress constructor.
1722
*/
@@ -111,4 +116,19 @@ public function meta($attributes)
111116

112117
return (isset($this->meta[$attributes])) ? $this->meta[$attributes] : '';
113118
}
119+
120+
/**
121+
* Bootstrap Field parsers.
122+
*
123+
* @param array $fields
124+
*/
125+
public function fields(array $fields)
126+
{
127+
$this->fields = array_merge($this->fields, $fields);
128+
}
129+
130+
public function availableFields()
131+
{
132+
return $this->fields;
133+
}
114134
}

src/LaraPressBaseServiceProvider.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function registerResources()
3535
$this->registerHelpers();
3636
$this->registerFacades();
3737
$this->registerRoutes();
38+
$this->registerFields();
3839
}
3940

4041
/**
@@ -64,8 +65,29 @@ protected function registerRoutes()
6465
});
6566
}
6667

68+
/**
69+
* Bootstrap package fields.
70+
*
71+
* @return void
72+
*/
73+
protected function registerFields()
74+
{
75+
LaraPress::fields(array_merge([
76+
Field\Body::class,
77+
Field\Date::class,
78+
Field\Extra::class,
79+
Field\Identifier::class,
80+
Field\Permalink::class,
81+
Field\Series::class,
82+
Field\Tags::class,
83+
Field\Title::class,
84+
], $this->fields()));
85+
}
86+
6787
/**
6888
* Register the additional helpers needed.
89+
*
90+
* @return void
6991
*/
7092
protected function registerHelpers()
7193
{
@@ -76,6 +98,8 @@ protected function registerHelpers()
7698

7799
/**
78100
* Register any bindings to the app.
101+
*
102+
* @return void
79103
*/
80104
protected function registerFacades()
81105
{

src/LaraPressServiceProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@ public function register()
2727

2828
//
2929
}
30+
31+
/**
32+
* Bootstrap any additional custom field parsers.
33+
*
34+
* @return array
35+
*/
36+
public function fields()
37+
{
38+
return [
39+
//
40+
];
41+
}
3042
}

src/PressFileParser.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace vicgonvt\LaraPress;
44

55
use Illuminate\Support\Facades\File;
6+
use vicgonvt\LaraPress\Facades\LaraPress;
67

78
class PressFileParser
89
{
@@ -50,7 +51,9 @@ public function getData()
5051
protected function processFields()
5152
{
5253
foreach ($this->parsedData as $fieldType => $fieldData) {
53-
$class = 'vicgonvt\LaraPress\Field\\' . ucfirst(camel_case($fieldType));
54+
55+
$class = $this->class($fieldType);
56+
$class = array_pop($class);
5457

5558
if ( ! class_exists($class) && ! method_exists($class, 'process')) {
5659
$class = 'vicgonvt\LaraPress\Field\Extra';
@@ -92,4 +95,20 @@ protected function splitFile()
9295
$this->splitFile
9396
);
9497
}
98+
99+
/**
100+
* @param $fieldType
101+
*
102+
* @return string
103+
*/
104+
private function class($fieldType)
105+
{
106+
$baseClass = ucfirst(camel_case($fieldType));
107+
108+
return array_filter(LaraPress::availableFields(), function ($class) use ($baseClass) {
109+
if (preg_match('/\\\\' . $baseClass . '$/', $class)) {
110+
return $class;
111+
}
112+
});
113+
}
95114
}

tests/Unit/PressFileParserTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Carbon\Carbon;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use vicgonvt\LaraPress\Facades\LaraPress;
8+
use vicgonvt\LaraPress\Field\FieldContract;
79
use vicgonvt\LaraPress\PressFileParser;
810
use vicgonvt\LaraPress\Series;
911

@@ -138,8 +140,42 @@ public function the_body_gets_markdown_parsed()
138140
$this->assertEquals('<h1>Title Here</h1>', $data['body']);
139141
}
140142

143+
/** @test */
144+
public function it_can_use_a_users_class()
145+
{
146+
LaraPress::fields(['\vicgonvt\LaraPress\Tests\Other']);
147+
148+
$data = (new PressFileParser("---\nOther: A Cool Title---\n#Title Here"))->getData();
149+
$this->assertEquals('A Cool Title', $data['other']);
150+
}
151+
152+
/** @test */
153+
public function it_fulfills_the_full_class_name()
154+
{
155+
LaraPress::fields(['\vicgonvt\LaraPress\Tests\TitleTitle']);
156+
157+
$data = (new PressFileParser("---\nTitle: A Cool Title---\n#Title Here"))->getData();
158+
$this->assertEquals('A Cool Title', $data['title']);
159+
}
160+
141161
private function getSampleMarkdownParser()
142162
{
143163
return (new PressFileParser(__DIR__ . '/../stubs/MarkFile1.md'));
144164
}
165+
}
166+
167+
class Other extends FieldContract
168+
{
169+
public static function process($fieldType, $fieldValue, $fields)
170+
{
171+
return ['other' => $fieldValue];
172+
}
173+
}
174+
175+
class TitleTitle extends FieldContract
176+
{
177+
public static function process($fieldType, $fieldValue, $fields)
178+
{
179+
return ['titletitle' => $fieldValue];
180+
}
145181
}

0 commit comments

Comments
 (0)