mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-27 08:28:40 +00:00
Fix types
This commit is contained in:
parent
a0b51cef62
commit
72d2e3ad77
|
|
@ -1,90 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
* Orange Management
|
|
||||||
*
|
|
||||||
* PHP Version 7.2
|
|
||||||
*
|
|
||||||
* @package TBD
|
|
||||||
* @copyright Dennis Eichhorn
|
|
||||||
* @license OMS License 1.0
|
|
||||||
* @version 1.0.0
|
|
||||||
* @link http://website.orange-management.de
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace phpOMS\Utils\IO\Csv;
|
|
||||||
|
|
||||||
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
|
|
||||||
use phpOMS\DataStorage\Database\Query\Builder;
|
|
||||||
use phpOMS\Utils\IO\IODatabaseMapper;
|
|
||||||
|
|
||||||
class CsvDatabaseMapper implements IODatabaseMapper
|
|
||||||
{
|
|
||||||
private $db = null;
|
|
||||||
|
|
||||||
private $sources = [];
|
|
||||||
|
|
||||||
private $delimiter = ';';
|
|
||||||
|
|
||||||
private $enclosure = '"';
|
|
||||||
|
|
||||||
private $lineBuffer = 500;
|
|
||||||
|
|
||||||
private $autoIdentifyCsvSettings = false;
|
|
||||||
|
|
||||||
public function __construct(ConnectionAbstract $db)
|
|
||||||
{
|
|
||||||
$this->db = $db;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addSource(string $source)
|
|
||||||
{
|
|
||||||
$this->sources[] = $source;
|
|
||||||
|
|
||||||
$this->sources = array_unique($this->sources);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setSources(array $sources) : void
|
|
||||||
{
|
|
||||||
$this->sources = $sources;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function autoIdentifyCsvSettings(bool $identify)
|
|
||||||
{
|
|
||||||
$this->autoIdentifyCsvSettings = $identify;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setLineBuffer(int $buffer) : void
|
|
||||||
{
|
|
||||||
$this->lineBuffer = $buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function insert()
|
|
||||||
{
|
|
||||||
foreach ($this->sources as $source) {
|
|
||||||
$file = fopen($source, 'r');
|
|
||||||
$header = [];
|
|
||||||
$delimiter = $this->autoIdentifyCsvSettings ? $this->getFileDelimiter($file, 100) : $this->delimiter;
|
|
||||||
|
|
||||||
if (feof($file) && ($line = fgetcsv($file, 0, $delimiter)) !== false) {
|
|
||||||
$header = $line;
|
|
||||||
}
|
|
||||||
|
|
||||||
$query = new Builder($this->db);
|
|
||||||
$query->insert(...$header)->into(\str_replace(' ', '', explode($source, '.')));
|
|
||||||
|
|
||||||
while (feof($file)) {
|
|
||||||
$c = 0;
|
|
||||||
|
|
||||||
while (($line = fgetcsv($file)) !== false && $c < $this->lineBuffer && feof($file)) {
|
|
||||||
$query->values($line);
|
|
||||||
$c++;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->db->con->prepare($query->toSql())->execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose($file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -25,9 +25,9 @@ class CsvSettings
|
||||||
/**
|
/**
|
||||||
* Get csv file delimiter.
|
* Get csv file delimiter.
|
||||||
*
|
*
|
||||||
* @param mixed $file File resource
|
* @param mixed $file File resource
|
||||||
* @param int $checkLines Lines to check for evaluation
|
* @param int $checkLines Lines to check for evaluation
|
||||||
* @param array $delimiters Potential delimiters
|
* @param string[] $delimiters Potential delimiters
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*
|
*
|
||||||
|
|
@ -36,14 +36,23 @@ class CsvSettings
|
||||||
public static function getFileDelimiter($file, int $checkLines = 2, array $delimiters = [',', '\t', ';', '|', ':']) : string
|
public static function getFileDelimiter($file, int $checkLines = 2, array $delimiters = [',', '\t', ';', '|', ':']) : string
|
||||||
{
|
{
|
||||||
$results = [];
|
$results = [];
|
||||||
|
$i = 0;
|
||||||
|
$line = \fgets($file);
|
||||||
|
|
||||||
$i = 0;
|
if ($line === false) {
|
||||||
while (($line = fgets($file)) !== false && $i < $checkLines) {
|
return ';';
|
||||||
|
}
|
||||||
|
|
||||||
|
while ($line !== false && $i < $checkLines) {
|
||||||
$i++;
|
$i++;
|
||||||
|
|
||||||
foreach ($delimiters as $delimiter) {
|
foreach ($delimiters as $delimiter) {
|
||||||
$regExp = '/[' . $delimiter . ']/';
|
$regExp = '/[' . $delimiter . ']/';
|
||||||
$fields = preg_split($regExp, $line);
|
$fields = \preg_split($regExp, $line);
|
||||||
|
|
||||||
|
if ($fields === false) {
|
||||||
|
return ';';
|
||||||
|
}
|
||||||
|
|
||||||
if (count($fields) > 1) {
|
if (count($fields) > 1) {
|
||||||
if (!empty($results[$delimiter])) {
|
if (!empty($results[$delimiter])) {
|
||||||
|
|
@ -55,7 +64,7 @@ class CsvSettings
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = array_keys($results, max($results));
|
$results = \array_keys($results, max($results));
|
||||||
|
|
||||||
return $results[0];
|
return $results[0];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace phpOMS\Utils\IO\Zip;
|
namespace phpOMS\Utils\IO\Zip;
|
||||||
|
|
||||||
|
use phpOMS\System\File\FileUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zip class for handling zip files.
|
* Zip class for handling zip files.
|
||||||
*
|
*
|
||||||
|
|
@ -31,48 +33,57 @@ class Tar implements ArchiveInterface
|
||||||
*/
|
*/
|
||||||
public static function pack($sources, string $destination, bool $overwrite = true) : bool
|
public static function pack($sources, string $destination, bool $overwrite = true) : bool
|
||||||
{
|
{
|
||||||
$destination = \str_replace('\\', '/', realpath($destination));
|
$destination = FileUtils::absolute(\str_replace('\\', '/', $destination));
|
||||||
|
|
||||||
if (!$overwrite && \file_exists($destination)) {
|
if (!$overwrite && \file_exists($destination)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$tar = new \PharData($destination);
|
||||||
|
|
||||||
/** @var array $sources */
|
/** @var array $sources */
|
||||||
foreach ($sources as $source) {
|
foreach ($sources as $source => $relative) {
|
||||||
$source = \str_replace('\\', '/', realpath($source));
|
$source = \realpath($source);
|
||||||
|
|
||||||
|
if ($source === false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$source = \str_replace('\\', '/', $source);
|
||||||
|
|
||||||
if (!\file_exists($source)) {
|
if (!\file_exists($source)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_dir($source)) {
|
if (\is_dir($source)) {
|
||||||
$files = new \RecursiveIteratorIterator(
|
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST);
|
||||||
new \RecursiveDirectoryIterator($source),
|
|
||||||
\RecursiveIteratorIterator::SELF_FIRST
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
$file = \str_replace('\\', '/', $file);
|
$file = \str_replace('\\', '/', $file);
|
||||||
|
|
||||||
/* Ignore . and .. */
|
/* Ignore . and .. */
|
||||||
if (\in_array(mb_substr($file, mb_strrpos($file, '/') + 1), ['.', '..'])) {
|
if (($pos = \mb_strrpos($file, '/')) === false
|
||||||
|
|| \in_array(\mb_substr($file, $pos + 1), ['.', '..'])
|
||||||
|
) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$file = realpath($file);
|
$absolute = \realpath($file);
|
||||||
|
$absolute = \str_replace('\\', '/', (string) $absolute);
|
||||||
|
$dir = \str_replace($source . '/', '', $relative . '/' . $absolute);
|
||||||
|
|
||||||
if (is_dir($file)) {
|
if (\is_dir($absolute)) {
|
||||||
// todo: do work here
|
$tar->addEmptyDir($dir . '/');
|
||||||
} elseif (is_file($file)) {
|
} elseif (\is_file($absolute)) {
|
||||||
// todo: do work here
|
$tar->addFile($absolute, $dir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif (is_file($source)) {
|
} elseif (\is_file($source)) {
|
||||||
// todo: do work here
|
$tar->addFile($source, $relative);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fwrite($tar, pack('a1024', ''));
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -80,6 +91,16 @@ class Tar implements ArchiveInterface
|
||||||
*/
|
*/
|
||||||
public static function unpack(string $source, string $destination) : bool
|
public static function unpack(string $source, string $destination) : bool
|
||||||
{
|
{
|
||||||
|
if (!\file_exists($source)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$destination = \str_replace('\\', '/', $destination);
|
||||||
|
$destination = \rtrim($destination, '/');
|
||||||
|
$tar = new \PharData($destination);
|
||||||
|
|
||||||
|
$tar->extractTo($destination . '/');
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ class Zip implements ArchiveInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
$destination = \str_replace('\\', '/', $destination);
|
$destination = \str_replace('\\', '/', $destination);
|
||||||
$destination = rtrim($destination, '/');
|
$destination = \rtrim($destination, '/');
|
||||||
|
|
||||||
$zip = new \ZipArchive();
|
$zip = new \ZipArchive();
|
||||||
if (!$zip->open($source)) {
|
if (!$zip->open($source)) {
|
||||||
|
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
* Orange Management
|
|
||||||
*
|
|
||||||
* PHP Version 7.2
|
|
||||||
*
|
|
||||||
* @package TBD
|
|
||||||
* @copyright Dennis Eichhorn
|
|
||||||
* @license OMS License 1.0
|
|
||||||
* @version 1.0.0
|
|
||||||
* @link http://website.orange-management.de
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace phpOMS\tests\Utils\IO\Csv;
|
|
||||||
|
|
||||||
use phpOMS\Utils\IO\Csv\CsvDatabaseMapper;
|
|
||||||
|
|
||||||
class CsvDatabaseMapperTest extends \PHPUnit\Framework\TestCase
|
|
||||||
{
|
|
||||||
public function testPlaceholder()
|
|
||||||
{
|
|
||||||
self::markTestIncomplete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user