Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Converters/CsvConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function item(FeedItem $item, bool $isLast): string
{
$data = $this->performItem($item->toArray());

return $this->toCsv($data);
return $this->encode($data);
}

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

return $this->toCsv($data);
return $this->encode($data);
}

protected function performItem(array $data): array
Expand All @@ -66,7 +66,7 @@ protected function performItem(array $data): array
return $data;
}

protected function toCsv(array $data): string
protected function encode(array $data): string
{
return implode($this->delimiter, $data);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Converters/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public function item(FeedItem $item, bool $isLast): string

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

return $this->toJson($data) . $suffix;
return $this->encode($data) . $suffix;
}

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

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

if (! $afterRoot) {
$json = mb_substr($json, 1, -1);
Expand All @@ -78,7 +78,7 @@ protected function performItem(array $data): array
return $data;
}

protected function toJson(array $data): string
protected function encode(array $data): string
{
return json_encode($data, $this->jsonOptions());
}
Expand Down
6 changes: 3 additions & 3 deletions src/Converters/JsonLinesConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function item(FeedItem $item, bool $isLast): string
{
$data = $this->performItem($item->toArray());

return $this->toJson($data);
return $this->encode($data);
}

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

return $this->toJson($data);
return $this->encode($data);
}

protected function performItem(array $data): array
Expand All @@ -68,7 +68,7 @@ protected function performItem(array $data): array
return $data;
}

protected function toJson(array $data): string
protected function encode(array $data): string
{
return json_encode($data, $this->options);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Converters/XmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function item(FeedItem $item, bool $isLast): string

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

return $this->toXml($box);
return $this->encode($box);
}

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

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

return $this->toXml($box);
return $this->encode($box);
}

protected function performBox(FeedItem $item): DOMNode
Expand Down Expand Up @@ -198,7 +198,7 @@ protected function rootAttributes(ElementData $item): string
->implode(' ');
}

protected function toXml(DOMNode $item): string
protected function encode(DOMNode $item): string
{
return $this->document->saveXML($item, LIBXML_COMPACT);
}
Expand Down
74 changes: 74 additions & 0 deletions stubs/converter.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace DummyNamespace;

use DragonCode\LaravelFeed\Converters\Converter;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use DragonCode\LaravelFeed\Services\TransformerService;
use Illuminate\Container\Attributes\Config;

use function is_array;
use function json_encode;

class DummyClass extends Converter
{
public function __construct(
#[Config('feeds.converters.jsonl.options')]
protected int $options,
TransformerService $transformer
) {
parent::__construct(false, $transformer);
}

public function header(Feed $feed): string
{
return '';
}

public function footer(Feed $feed): string
{
return '';
}

public function root(Feed $feed): string
{
return '';
}

public function item(FeedItem $item, bool $isLast): string
{
$data = $this->performItem($item->toArray());

return $this->encode($data);
}

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

return $this->encode($data);
}

protected function performItem(array $data): array
{
foreach ($data as &$value) {
if (is_array($value)) {
$value = $this->performItem($value);

continue;
}

$value = $this->transformValue($value);
}

return $data;
}

protected function encode(array $data): string
{
return json_encode($data, $this->options);
}
}