Skip to content

Commit

Permalink
simplified control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
xHeaven committed May 1, 2024
1 parent f168c01 commit a140c71
Showing 1 changed file with 57 additions and 46 deletions.
103 changes: 57 additions & 46 deletions src/JSONL.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,34 @@
use Illuminate\Support\Facades\File;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use RuntimeException;
use Luminarix\JSONL\Exceptions\FailedToEncodeException;
use Luminarix\JSONL\Exceptions\FailedToOpenFileException;
use Luminarix\JSONL\Exceptions\FailedToWriteFileException;
use Luminarix\JSONL\Exceptions\InvalidDtoClassException;
use Luminarix\JSONL\Exceptions\InvalidRowException;

class JSONL
{
/**
* @throws FailedToEncodeException If encoding fails.
*/
public function encode(array|Collection|LazyCollection $objects): string
{
return $this->encodeLines($objects);
}

/**
* @throws FailedToEncodeException If encoding fails.
*/
public function encodeFromDto(array|Collection|LazyCollection $dtos): string
{
return $this->encodeLinesFromDto($dtos);
return $this->encodeLines($dtos, true);
}

/**
* @throws FailedToOpenFileException If <code>$filePath</code> cannot be opened.
* @throws InvalidRowException If a row is invalid.
*/
public function parse(string $filePath): LazyCollection
{
return $this->parseLines($this->readLines($filePath));
Expand All @@ -32,88 +46,85 @@ public function parse(string $filePath): LazyCollection
*
* @param class-string<T> $dtoClass
* @return LazyCollection<int, T>
*
* @throws FailedToOpenFileException If <code>$filePath</code> cannot be opened.
* @throws InvalidRowException If a row is invalid.
* @throws InvalidDtoClassException If <code>$dtoClass</code> doesn't exist.
*/
public function parseToDto(string $filePath, string $dtoClass): LazyCollection
{
return $this->parseLinesToDto($this->readLines($filePath), $dtoClass);
return $this->parseLines($this->readLines($filePath), $dtoClass);
}

public function writeFromDto(string $filePath, array|Collection|LazyCollection $dtos): void
/**
* @throws FailedToEncodeException If encoding fails.
* @throws FailedToWriteFileException If writing fails.
*/
public function writeFromDto(string $filePath, array|Collection|LazyCollection $dtos, bool $lock = false): void
{
File::put($filePath, $this->encodeFromDto($dtos));
$result = File::put($filePath, $this->encodeFromDto($dtos), $lock);

if ($result === false) {
throw new FailedToWriteFileException("Failed to write file: {$filePath}");
}
}

public function write(string $filePath, array|Collection|LazyCollection $objects): void
/**
* @throws FailedToEncodeException If encoding fails.
* @throws FailedToWriteFileException If writing fails.
*/
public function write(string $filePath, array|Collection|LazyCollection $objects, bool $lock = false): void
{
File::put($filePath, $this->encode($objects));
$result = File::put($filePath, $this->encode($objects), $lock);

if ($result === false) {
throw new FailedToWriteFileException("Failed to write file: {$filePath}");
}
}

protected function encodeLines(array|Collection|LazyCollection $objects): string
protected function encodeLines(array|Collection|LazyCollection $objects, bool $dto = false): string
{
if (!$objects instanceof LazyCollection) {
$objects = LazyCollection::make($objects);
}

return $objects
->map(static function ($object) {
$json = json_encode($object, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('JSON encoding failed: ' . json_last_error_msg());
->map(static function ($object) use ($dto) {
if ($dto) {
$object = get_object_vars($object);
}

return $json;
})
->implode("\n");
}

protected function encodeLinesFromDto(array|Collection|LazyCollection $dtos): string
{
if (!$dtos instanceof LazyCollection) {
$dtos = LazyCollection::make($dtos);
}

return $dtos
->map(static function ($dto) {
$data = get_object_vars($dto);
$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$json = json_encode($object, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('JSON encoding failed: ' . json_last_error_msg());
throw new FailedToEncodeException('JSON encoding failed: ' . json_last_error_msg());
}

return $json;
})
->implode("\n");
}

protected function parseLines(LazyCollection $lines): LazyCollection
protected function parseLines(LazyCollection $lines, ?string $dtoClass = null): LazyCollection
{
return $lines
->reject(static fn ($line) => $line->isEmpty())
->map(static function ($line) {
->map(static function ($line) use ($dtoClass) {
$object = json_decode($line, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("Invalid JSON: {$line}");
throw new InvalidRowException("Invalid row: {$line}");
}

return $object;
});
}
if ($dtoClass !== null) {
if (!class_exists($dtoClass)) {
throw new InvalidDtoClassException("Invalid DTO class: {$dtoClass}");
}

protected function parseLinesToDto(LazyCollection $lines, string $dtoClass): LazyCollection
{
return $lines
->reject(static fn ($line) => $line->isEmpty())
->map(static function ($line) use ($dtoClass) {
$data = json_decode($line, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("Invalid JSON: {$line}");
return new $dtoClass(...$object);
}

return new $dtoClass(...$data);
return $object;
});
}

Expand All @@ -123,7 +134,7 @@ protected function readLines(string $filePath): LazyCollection
$handle = fopen($filePath, 'rb');

if ($handle === false) {
throw new RuntimeException("Failed to open file: {$filePath}");
throw new FailedToOpenFileException("Failed to open file: {$filePath}");
}

try {
Expand Down

0 comments on commit a140c71

Please sign in to comment.