Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add base func for csv import #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Empty file.
5 changes: 5 additions & 0 deletions Modules/CsvImport/Config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'name' => 'CsvImport'
];
66 changes: 66 additions & 0 deletions Modules/CsvImport/CsvConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php declare(strict_types=1);

namespace Modules\CsvImport;

use Illuminate\Support\Collection;
use Modules\CsvImport\Entities\CsvConverterInterface;
use Modules\CsvImport\Entities\CsvHasHeaderInterface;
use Modules\CsvImport\Handler\ColumnPosition;
use Modules\CsvImport\Helpers\TmpFileService;
use Modules\CsvImport\Parser\ObjectParser;

class CsvConverter
{
/** @var CsvNavigator */
private $csvNavigator;

/**
* CsvConverter constructor.
*
* @param CsvNavigator $csvNavigator
*/
public function __construct(CsvNavigator $csvNavigator)
{
$this->csvNavigator = $csvNavigator;
}

/**
* @param Collection $collection
*
* @return \SplFileObject
*/
public function convertCollection(Collection $collection): \SplFileObject
{
$result = (new TmpFileService())->createFile()->openFile('w');
foreach ($collection as $row) {
$result->fputcsv($this->toArray($row));
}

return $result;
}

/**
* @param CsvConverterInterface $data
*
* @return array
*/
public function toArray(CsvConverterInterface $data): array
{
$result = [];
if ($data instanceof CsvHasHeaderInterface) {
$result[] = $data->getHeaderMapping();
}

foreach (ColumnPosition::$letterByPosition as $position => $letter) {
$objectParser = new ObjectParser($letter, $data);
if (!$objectParser->hasColumn()) {
$result[$position] = '';
continue;
}

$result[$position] = $this->csvNavigator->accept($objectParser->getValue());
}

return $result;
}
}
73 changes: 73 additions & 0 deletions Modules/CsvImport/CsvNavigator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types=1);

namespace Modules\CsvImport;

use Modules\CsvImport\Handler\HandlerRegistryInterface;

class CsvNavigator
{
/** @var CsvVisitor */
private $visitor;
/** @var HandlerRegistryInterface */
private $handlerRegistry;

/**
* CsvNavigator constructor.
*
* @param CsvVisitor $visitor
* @param HandlerRegistryInterface $handlerRegistry
*/
public function __construct(CsvVisitor $visitor, HandlerRegistryInterface $handlerRegistry)
{
$this->visitor = $visitor;
$this->handlerRegistry = $handlerRegistry;
}

/**
* @param $data
* @param array|null $type
*
* @return mixed
*/
public function accept($data, array $type = null)
{
if (null === $type) {
$typeName = \gettype($data);
$type = ['name' => $typeName, 'params' => []];
}

switch ($type['name']) {
case 'NULL':
return $this->visitor->visitNull();

case 'string':
return $this->visitor->visitString($data);

case 'int':
case 'integer':
return $this->visitor->visitInteger($data);

case 'bool':
case 'boolean':
return $this->visitor->visitBoolean($data);

case 'double':
case 'float':
return $this->visitor->visitDouble($data);

case 'array':
case 'resource':
case 'object':
default:
if ($type['name'] == 'object') {
$type['name'] = get_class($data);
}

if (null !== $handler = $this->handlerRegistry->getHandler($type['name'])) {
return \call_user_func($handler, $data);
}

throw new \RuntimeException(sprintf('Type %s is not implement for convert', $type['name']));
}
}
}
32 changes: 32 additions & 0 deletions Modules/CsvImport/CsvVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace Modules\CsvImport;

class CsvVisitor
{

public function visitNull()
{
return null;
}

public function visitString($data): string
{
return (string)$data;
}

public function visitBoolean($data): bool
{
return (bool)$data;
}

public function visitInteger($data): int
{
return (int)$data;
}

public function visitDouble($data): float
{
return (float)$data;
}
}
Empty file.
8 changes: 8 additions & 0 deletions Modules/CsvImport/Entities/CsvConverterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Modules\CsvImport\Entities;

interface CsvConverterInterface
{
public function getColumnsMapping(): array;
}
8 changes: 8 additions & 0 deletions Modules/CsvImport/Entities/CsvHasHeaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Modules\CsvImport\Entities;

interface CsvHasHeaderInterface
{
public function getHeaderMapping(): array;
}
145 changes: 145 additions & 0 deletions Modules/CsvImport/Entities/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php declare(strict_types=1);

namespace Modules\CsvImport\Entities;

use Illuminate\Support\Collection;
use Modules\CsvImport\Helpers\DateTimeTrait;

class Product implements CsvConverterInterface
{
use DateTimeTrait {
DateTimeTrait::__construct as private __dtConstruct;
}

/** @var int */
private $id;
/** @var string */
private $name;
/** @var string */
private $code;
/** @var array */
private $images;
/** @var Collection|Property[] */
private $properties;

/**
* Product constructor.
*/
public function __construct()
{
$this->__dtConstruct();
$this->properties = new Collection();
}

/**
* @return int
*/
public function getId(): int
{
return $this->id;
}

/**
* @param int $id
*
* @return $this;
*/
public function setId(int $id): self
{
$this->id = $id;

return $this;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @param string $name
*
* @return $this;
*/
public function setName(string $name): self
{
$this->name = $name;

return $this;
}

/**
* @return string
*/
public function getCode(): string
{
return $this->code;
}

/**
* @param string $code
*
* @return $this;
*/
public function setCode(string $code): self
{
$this->code = $code;

return $this;
}

/**
* @return array
*/
public function getImages(): array
{
return $this->images;
}

/**
* @param array $images
*
* @return $this;
*/
public function setImages(array $images): self
{
$this->images = $images;

return $this;
}

/**
* @return Collection
*/
public function getProperties(): Collection
{
return $this->properties;
}

/**
* @param Collection $properties
*
* @return $this;
*/
public function setProperties(Collection $properties): self
{
$this->properties = $properties;

return $this;
}

/**
* @return array
*/
public function getColumnsMapping(): array
{
return [
'B' => 'name',
'D' => 'code',
'E' => 'dateCreated',
];
}
}
Loading