mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +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.
|
||||
*
|
||||
* @param mixed $file File resource
|
||||
* @param int $checkLines Lines to check for evaluation
|
||||
* @param array $delimiters Potential delimiters
|
||||
* @param mixed $file File resource
|
||||
* @param int $checkLines Lines to check for evaluation
|
||||
* @param string[] $delimiters Potential delimiters
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
|
|
@ -36,14 +36,23 @@ class CsvSettings
|
|||
public static function getFileDelimiter($file, int $checkLines = 2, array $delimiters = [',', '\t', ';', '|', ':']) : string
|
||||
{
|
||||
$results = [];
|
||||
$i = 0;
|
||||
$line = \fgets($file);
|
||||
|
||||
$i = 0;
|
||||
while (($line = fgets($file)) !== false && $i < $checkLines) {
|
||||
if ($line === false) {
|
||||
return ';';
|
||||
}
|
||||
|
||||
while ($line !== false && $i < $checkLines) {
|
||||
$i++;
|
||||
|
||||
foreach ($delimiters as $delimiter) {
|
||||
$regExp = '/[' . $delimiter . ']/';
|
||||
$fields = preg_split($regExp, $line);
|
||||
$fields = \preg_split($regExp, $line);
|
||||
|
||||
if ($fields === false) {
|
||||
return ';';
|
||||
}
|
||||
|
||||
if (count($fields) > 1) {
|
||||
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];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\Utils\IO\Zip;
|
||||
|
||||
use phpOMS\System\File\FileUtils;
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
$destination = \str_replace('\\', '/', realpath($destination));
|
||||
$destination = FileUtils::absolute(\str_replace('\\', '/', $destination));
|
||||
|
||||
if (!$overwrite && \file_exists($destination)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$tar = new \PharData($destination);
|
||||
|
||||
/** @var array $sources */
|
||||
foreach ($sources as $source) {
|
||||
$source = \str_replace('\\', '/', realpath($source));
|
||||
foreach ($sources as $source => $relative) {
|
||||
$source = \realpath($source);
|
||||
|
||||
if ($source === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$source = \str_replace('\\', '/', $source);
|
||||
|
||||
if (!\file_exists($source)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_dir($source)) {
|
||||
$files = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($source),
|
||||
\RecursiveIteratorIterator::SELF_FIRST
|
||||
);
|
||||
if (\is_dir($source)) {
|
||||
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$file = \str_replace('\\', '/', $file);
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
$file = realpath($file);
|
||||
$absolute = \realpath($file);
|
||||
$absolute = \str_replace('\\', '/', (string) $absolute);
|
||||
$dir = \str_replace($source . '/', '', $relative . '/' . $absolute);
|
||||
|
||||
if (is_dir($file)) {
|
||||
// todo: do work here
|
||||
} elseif (is_file($file)) {
|
||||
// todo: do work here
|
||||
if (\is_dir($absolute)) {
|
||||
$tar->addEmptyDir($dir . '/');
|
||||
} elseif (\is_file($absolute)) {
|
||||
$tar->addFile($absolute, $dir);
|
||||
}
|
||||
}
|
||||
} elseif (is_file($source)) {
|
||||
// todo: do work here
|
||||
} elseif (\is_file($source)) {
|
||||
$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
|
||||
{
|
||||
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 = rtrim($destination, '/');
|
||||
$destination = \rtrim($destination, '/');
|
||||
|
||||
$zip = new \ZipArchive();
|
||||
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