Skip to content

Commit e35e14b

Browse files
Merge pull request #120
Unified encoding methods in converter classes
2 parents cbfd773 + 59f4ddb commit e35e14b

File tree

5 files changed

+86
-12
lines changed

5 files changed

+86
-12
lines changed

src/Converters/CsvConverter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public function item(FeedItem $item, bool $isLast): string
4141
{
4242
$data = $this->performItem($item->toArray());
4343

44-
return $this->toCsv($data);
44+
return $this->encode($data);
4545
}
4646

4747
public function info(array $info, bool $afterRoot): string
4848
{
4949
$data = $this->performItem($info);
5050

51-
return $this->toCsv($data);
51+
return $this->encode($data);
5252
}
5353

5454
protected function performItem(array $data): array
@@ -66,7 +66,7 @@ protected function performItem(array $data): array
6666
return $data;
6767
}
6868

69-
protected function toCsv(array $data): string
69+
protected function encode(array $data): string
7070
{
7171
return implode($this->delimiter, $data);
7272
}

src/Converters/JsonConverter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public function item(FeedItem $item, bool $isLast): string
4747

4848
$suffix = $isLast ? '' : ',';
4949

50-
return $this->toJson($data) . $suffix;
50+
return $this->encode($data) . $suffix;
5151
}
5252

5353
public function info(array $info, bool $afterRoot): string
5454
{
5555
$data = $this->performItem($info);
5656

57-
$json = $this->toJson($data);
57+
$json = $this->encode($data);
5858

5959
if (! $afterRoot) {
6060
$json = mb_substr($json, 1, -1);
@@ -78,7 +78,7 @@ protected function performItem(array $data): array
7878
return $data;
7979
}
8080

81-
protected function toJson(array $data): string
81+
protected function encode(array $data): string
8282
{
8383
return json_encode($data, $this->jsonOptions());
8484
}

src/Converters/JsonLinesConverter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public function item(FeedItem $item, bool $isLast): string
4343
{
4444
$data = $this->performItem($item->toArray());
4545

46-
return $this->toJson($data);
46+
return $this->encode($data);
4747
}
4848

4949
public function info(array $info, bool $afterRoot): string
5050
{
5151
$data = $this->performItem($info);
5252

53-
return $this->toJson($data);
53+
return $this->encode($data);
5454
}
5555

5656
protected function performItem(array $data): array
@@ -68,7 +68,7 @@ protected function performItem(array $data): array
6868
return $data;
6969
}
7070

71-
protected function toJson(array $data): string
71+
protected function encode(array $data): string
7272
{
7373
return json_encode($data, $this->options);
7474
}

src/Converters/XmlConverter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function item(FeedItem $item, bool $isLast): string
7575

7676
$this->performItem($box, $item->toArray());
7777

78-
return $this->toXml($box);
78+
return $this->encode($box);
7979
}
8080

8181
public function info(array $info, bool $afterRoot): string
@@ -84,7 +84,7 @@ public function info(array $info, bool $afterRoot): string
8484

8585
$this->performItem($box, $info);
8686

87-
return $this->toXml($box);
87+
return $this->encode($box);
8888
}
8989

9090
protected function performBox(FeedItem $item): DOMNode
@@ -198,7 +198,7 @@ protected function rootAttributes(ElementData $item): string
198198
->implode(' ');
199199
}
200200

201-
protected function toXml(DOMNode $item): string
201+
protected function encode(DOMNode $item): string
202202
{
203203
return $this->document->saveXML($item, LIBXML_COMPACT);
204204
}

stubs/converter.stub

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DummyNamespace;
6+
7+
use DragonCode\LaravelFeed\Converters\Converter;
8+
use DragonCode\LaravelFeed\Feeds\Feed;
9+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
10+
use DragonCode\LaravelFeed\Services\TransformerService;
11+
use Illuminate\Container\Attributes\Config;
12+
13+
use function is_array;
14+
use function json_encode;
15+
16+
class DummyClass extends Converter
17+
{
18+
public function __construct(
19+
#[Config('feeds.converters.jsonl.options')]
20+
protected int $options,
21+
TransformerService $transformer
22+
) {
23+
parent::__construct(false, $transformer);
24+
}
25+
26+
public function header(Feed $feed): string
27+
{
28+
return '';
29+
}
30+
31+
public function footer(Feed $feed): string
32+
{
33+
return '';
34+
}
35+
36+
public function root(Feed $feed): string
37+
{
38+
return '';
39+
}
40+
41+
public function item(FeedItem $item, bool $isLast): string
42+
{
43+
$data = $this->performItem($item->toArray());
44+
45+
return $this->encode($data);
46+
}
47+
48+
public function info(array $info, bool $afterRoot): string
49+
{
50+
$data = $this->performItem($info);
51+
52+
return $this->encode($data);
53+
}
54+
55+
protected function performItem(array $data): array
56+
{
57+
foreach ($data as &$value) {
58+
if (is_array($value)) {
59+
$value = $this->performItem($value);
60+
61+
continue;
62+
}
63+
64+
$value = $this->transformValue($value);
65+
}
66+
67+
return $data;
68+
}
69+
70+
protected function encode(array $data): string
71+
{
72+
return json_encode($data, $this->options);
73+
}
74+
}

0 commit comments

Comments
 (0)