Skip to content

Commit

Permalink
Merge pull request #11 from vitorccs/feature/add_importing_feature
Browse files Browse the repository at this point in the history
Adds CSV Importing feature
  • Loading branch information
vitorccs authored Jul 19, 2024
2 parents 31f0cff + e324835 commit d1a6a4a
Show file tree
Hide file tree
Showing 97 changed files with 2,831 additions and 555 deletions.
269 changes: 219 additions & 50 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns;
namespace Vitorccs\LaravelCsv\Concerns\Exportables;

use Illuminate\Foundation\Bus\PendingDispatch;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Vitorccs\LaravelCsv\Entities\CsvConfig;
use Vitorccs\LaravelCsv\Facades\CsvExporter;
use Vitorccs\LaravelCsv\Helpers\CsvHelper;

Expand All @@ -17,6 +18,22 @@ public function limit(): ?int
return null;
}

/**
* @return CsvConfig
*/
public function getConfig(): CsvConfig
{
return CsvExporter::getConfig();
}

/**
* @param CsvConfig $config
*/
public function setConfig(CsvConfig $config): void
{
CsvExporter::setConfig($config);
}

/**
* @return int
*/
Expand Down Expand Up @@ -68,6 +85,14 @@ public function queue(?string $filename = null,
return CsvExporter::queue($this, $this->getFilename($filename), $disk, $diskOptions);
}

/**
* @return resource
*/
public function stream()
{
return CsvExporter::stream($this);
}

/**
* @param string|null $filename
* @return string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns;
namespace Vitorccs\LaravelCsv\Concerns\Exportables;

interface FromArray
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns;
namespace Vitorccs\LaravelCsv\Concerns\Exportables;

use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns;
namespace Vitorccs\LaravelCsv\Concerns\Exportables;

use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;

interface FromQuery
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns;
namespace Vitorccs\LaravelCsv\Concerns\Exportables;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
Expand Down
11 changes: 11 additions & 0 deletions src/Concerns/Importables/FromContents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns\Importables;

interface FromContents
{
/**
* @return string
*/
public function contents(): string;
}
16 changes: 16 additions & 0 deletions src/Concerns/Importables/FromDisk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns\Importables;

interface FromDisk
{
/**
* @return string|null
*/
public function disk(): ?string;

/**
* @return string
*/
public function filename(): string;
}
11 changes: 11 additions & 0 deletions src/Concerns/Importables/FromFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns\Importables;

interface FromFile
{
/**
* @return string
*/
public function filename(): string;
}
11 changes: 11 additions & 0 deletions src/Concerns/Importables/FromResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns\Importables;

interface FromResource
{
/**
* @return resource
*/
public function resource();
}
60 changes: 60 additions & 0 deletions src/Concerns/Importables/Importable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Vitorccs\LaravelCsv\Concerns\Importables;

use Vitorccs\LaravelCsv\Entities\CsvConfig;
use Vitorccs\LaravelCsv\Facades\CsvImporter;

trait Importable
{
/**
* @return int|null
*/
public function limit(): ?int
{
return null;
}

/**
* @return CsvConfig
*/
public function getConfig(): CsvConfig
{
return CsvImporter::getConfig();
}

/**
* @param CsvConfig $config
*/
public function setConfig(CsvConfig $config): void
{
CsvImporter::setConfig($config);
}

/**
* @return int
*/
public function count(): int
{
return CsvImporter::count($this);
}

/**
* @param callable(array,int):void $callable
* @param int|null $size
* @return void
*/
public function chunkArray(callable $callable,
?int $size = null): void
{
CsvImporter::chunkArray($this, $callable, $size);
}

/**
* @return array
*/
public function getArray(): array
{
return CsvImporter::getArray($this);
}
}
10 changes: 10 additions & 0 deletions src/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,14 @@ public function queue(object $exportable,
{
return $this->service->queue($exportable, $filename, $disk, $diskOptions);
}

/**
* @param object $exportable
* @return resource
* @throws InvalidCellValueException
*/
public function stream(object $exportable)
{
return $this->service->getStream($exportable);
}
}
31 changes: 26 additions & 5 deletions src/CsvImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(ImportableService $service)
*/
public function getConfig(): CsvConfig
{
return $this->service->getConfig();
return $this->service->getConfig();
}

/**
Expand All @@ -37,12 +37,33 @@ public function setConfig(CsvConfig $config): void
}

/**
* @param string $filename
* @param string|null $disk
* @param object $importable
* @return int
*/
public function count(object $importable): int
{
return $this->service->count($importable);
}

/**
* @param object $importable
* @return array
*/
public function fromDisk(string $filename, ?string $disk = null): array
public function getArray(object $importable): array
{
return $this->service->getArray($importable);
}

/**
* @param object $importable
* @param callable(array,int):void $callable
* @param int|null $size
* @return void
*/
public function chunkArray(object $importable,
callable $callable,
?int $size): void
{
return $this->service->fromDisk($filename, $disk);
$this->service->chunkArray($importable, $callable, $size);
}
}
3 changes: 2 additions & 1 deletion src/Facades/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

/**
* @method static CsvConfig getConfig()
* @method static void setConfig(CsvConfig $config)
* @method static int count(object $exportable)
* @method static array toArray(object $exportable)
* @method static string store(object $exportable, string $filename = null, ?string $disk = null, array $diskOptions = [])
* @method static StreamedResponse download(object $exportable, string $filename)
* @method static stream(object $exportable)
* @method static PendingDispatch queue(object $exportable, string $filename, ?string $disk = null, array $diskOptions = [])
* @method static void setConfig(CsvConfig $config)
*/
class CsvExporter extends Facade
{
Expand Down
4 changes: 3 additions & 1 deletion src/Facades/CsvImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

/**
* @method static CsvConfig getConfig()
* @method static array fromDisk(string $filename, ?string $disk = null)
* @method static void setConfig(CsvConfig $config)
* @method static int count(object $importable)
* @method static void chunkArray(object $importable, callable $callable, ?int $size = null)
* @method static array getArray(object $importable)
*/
class CsvImporter extends Facade
{
Expand Down
10 changes: 0 additions & 10 deletions src/Handlers/Handler.php

This file was deleted.

31 changes: 31 additions & 0 deletions src/Handlers/Readers/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Vitorccs\LaravelCsv\Handlers\Readers;

interface Handler
{
/**
* @return int
*/
public function count(): int;

/**
* @return array
*/
public function getAll(): array;

/**
* @param callable(array,int):void $callable
* @param int $size
* @param int|null $maxRecords
* @return void
*/
public function getChunk(callable $callable,
int $size,
?int $maxRecords = null): void;

/**
* @return resource
*/
public function getResource();
}
Loading

0 comments on commit d1a6a4a

Please sign in to comment.