modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-14 05:35:42 +04:00
parent b71cd8beb8
commit 907aba4590
12 changed files with 130 additions and 31 deletions

13
.gitignore vendored
View File

@ -1,16 +1,3 @@
/.idea/**/workspace.xml
/.idea/**/tasks.xml
/.idea/**/usage.statistics.xml
/.idea/**/dictionaries
/.idea/**/shelf
/.idea/**/contentModel.xml
/.idea/**/dataSources/
/.idea/**/dataSources.ids
/.idea/**/dataSources.local.xml
/.idea/**/sqlDataSources.xml
/.idea/**/dynamic.xml
/.idea/**/uiDesigner.xml
/.idea/**/dbnavigator.xml
/.phpunit.result.cache /.phpunit.result.cache
.~lock*# .~lock*#

View File

@ -56,6 +56,7 @@
<path value="$PROJECT_DIR$/vendor/psr/http-message" /> <path value="$PROJECT_DIR$/vendor/psr/http-message" />
<path value="$PROJECT_DIR$/vendor/maennchen/zipstream-php" /> <path value="$PROJECT_DIR$/vendor/maennchen/zipstream-php" />
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-mbstring" /> <path value="$PROJECT_DIR$/vendor/symfony/polyfill-mbstring" />
<path value="$PROJECT_DIR$/vendor/lib/spout" />
</include_path> </include_path>
</component> </component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.4" /> <component name="PhpProjectSharedConfiguration" php_language_level="7.4" />

View File

@ -11,6 +11,12 @@
<PhpSpecSuiteConfiguration> <PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" /> <option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration> </PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
</suites> </suites>
</component> </component>
</project> </project>

View File

@ -3,6 +3,10 @@
"type": "library", "type": "library",
"description": "espace de maturation pour les librairies", "description": "espace de maturation pour les librairies",
"repositories": [ "repositories": [
{
"type": "path",
"url": "../lib-spout"
},
{ {
"type": "composer", "type": "composer",
"url": "https://repos.univ-reunion.fr/composer" "url": "https://repos.univ-reunion.fr/composer"
@ -14,6 +18,7 @@
}, },
"require-dev": { "require-dev": {
"nulib/tests": "7.4", "nulib/tests": "7.4",
"lib/spout": "dev-master",
"phpoffice/phpspreadsheet": "^1.0", "phpoffice/phpspreadsheet": "^1.0",
"ext-posix": "*", "ext-posix": "*",
"ext-pcntl": "*", "ext-pcntl": "*",

83
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "c951b0cf9bd2f2bab34a46242331eb88", "content-hash": "35dddc6d357654919ddf2728176ac22f",
"packages": [ "packages": [
{ {
"name": "symfony/deprecation-contracts", "name": "symfony/deprecation-contracts",
@ -360,6 +360,83 @@
}, },
"time": "2023-11-17T15:01:25+00:00" "time": "2023-11-17T15:01:25+00:00"
}, },
{
"name": "lib/spout",
"version": "dev-master",
"dist": {
"type": "path",
"url": "../lib-spout",
"reference": "0fd23b5cd9312c9236e435687f2dbdf2d63c8d83"
},
"require": {
"ext-dom": "*",
"ext-filter": "*",
"ext-libxml": "*",
"ext-xmlreader": "*",
"ext-zip": "*",
"php": "~7.3.0 || ~7.4.0 || ~8.0.0 || ~8.1.0"
},
"replace": {
"openspout/openspout": "v3.7.4"
},
"require-dev": {
"ext-zlib": "*",
"friendsofphp/php-cs-fixer": "^3.4",
"phpstan/phpstan": "^1.4",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5"
},
"suggest": {
"ext-iconv": "To handle non UTF-8 CSV files (if \"php-intl\" is not already installed or is too limited)",
"ext-intl": "To handle non UTF-8 CSV files (if \"iconv\" is not already installed)"
},
"type": "library",
"autoload": {
"psr-4": {
"OpenSpout\\": "src/"
}
},
"autoload-dev": {
"classmap": [
"tests/"
]
},
"license": [
"MIT"
],
"authors": [
{
"name": "Jephte Clain",
"email": "Jephte.Clain@univ-reunion.fr"
},
{
"name": "Adrien Loison",
"email": "adrien@box.com"
}
],
"description": "PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way",
"homepage": "https://github.com/openspout/openspout",
"keywords": [
"csv",
"excel",
"memory",
"odf",
"ods",
"office",
"ooxml",
"open",
"php",
"read",
"scale",
"spreadsheet",
"stream",
"write",
"xlsx"
],
"transport-options": {
"relative": true
}
},
{ {
"name": "maennchen/zipstream-php", "name": "maennchen/zipstream-php",
"version": "2.2.6", "version": "2.2.6",
@ -2710,7 +2787,9 @@
], ],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": [], "stability-flags": {
"lib/spout": 20
},
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {

View File

@ -10,7 +10,6 @@ use PhpOffice\PhpSpreadsheet\Cell\StringValueBinder;
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\Ods; use PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Writer\Xls;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/** /**
@ -92,9 +91,6 @@ class SsBuilder extends AbstractBuilder {
case ".ods": case ".ods":
$contentType = "application/vnd.oasis.opendocument.spreadsheet"; $contentType = "application/vnd.oasis.opendocument.spreadsheet";
break; break;
case ".xls":
$contentType = "application/vnd.ms-excel";
break;
case ".xlsx": case ".xlsx":
default: default:
$contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; $contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
@ -108,9 +104,6 @@ class SsBuilder extends AbstractBuilder {
case ".ods": case ".ods":
$writer = new Ods($this->ss); $writer = new Ods($this->ss);
break; break;
case ".xls":
$writer = new Xls($this->ss);
break;
case ".xlsx": case ".xlsx":
default: default:
$writer = new Xlsx($this->ss); $writer = new Xlsx($this->ss);

View File

@ -165,7 +165,7 @@ class Stream extends AbstractIterator implements IReader, IWriter {
/** @var array paramètres pour la lecture et l'écriture de flux au format CSV */ /** @var array paramètres pour la lecture et l'écriture de flux au format CSV */
protected $csvFlavour; protected $csvFlavour;
function setCsvFlavour(string $flavour): void { function setCsvFlavour(?string $flavour): void {
$this->csvFlavour = csv_flavours::verifix($flavour); $this->csvFlavour = csv_flavours::verifix($flavour);
} }

View File

@ -9,7 +9,7 @@ use nur\sery\php\func;
use nur\sery\php\time\DateTime; use nur\sery\php\time\DateTime;
use nur\sery\web\http; use nur\sery\web\http;
abstract class AbstractBuilder extends TempStream { abstract class AbstractBuilder extends TempStream implements IBuilder {
/** @var ?array schéma des données à écrire */ /** @var ?array schéma des données à écrire */
const SCHEMA = null; const SCHEMA = null;

View File

@ -6,8 +6,16 @@ use nur\sery\file\FileReader;
class CsvReader extends AbstractReader { class CsvReader extends AbstractReader {
use TAbstractReader; use TAbstractReader;
function __construct($input, ?array $params=null) {
parent::__construct($input, $params);
$this->csvFlavour = $params["csv_flavour"] ?? null;
}
protected ?string $csvFlavour;
function getIterator() { function getIterator() {
$reader = new FileReader($this->input); $reader = new FileReader($this->input);
$reader->setCsvFlavour($this->csvFlavour);
while (($row = $reader->fgetcsv()) !== null) { while (($row = $reader->fgetcsv()) !== null) {
foreach ($row as &$col) { foreach ($row as &$col) {
$this->verifixCol($col); $this->verifixCol($col);

14
src/file/csv/IBuilder.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace nur\sery\file\csv;
interface IBuilder {
function writeHeaders(?array $headers=null): void;
function write(?array $row): void;
function writeAll(?iterable $rows=null): void;
function sendHeaders(): void;
function sendFile(?iterable $rows=null): int;
}

View File

@ -7,7 +7,7 @@ use nur\sery\ValueException;
trait TAbstractBuilder { trait TAbstractBuilder {
/** @param Upload|string|array $builder */ /** @param Upload|string|array $builder */
static function with($builder): self { static function with($builder): IBuilder {
if ($builder instanceof self) return $builder; if ($builder instanceof self) return $builder;
$class = null; $class = null;
if ($builder instanceof Upload) { if ($builder instanceof Upload) {

View File

@ -1,29 +1,35 @@
<?php <?php
namespace nur\sery\file\csv; namespace nur\sery\file\csv;
use nur\sery\cl;
use nur\sery\file\web\Upload; use nur\sery\file\web\Upload;
use nur\sery\os\path; use nur\sery\os\path;
use nur\sery\ValueException; use nur\sery\ValueException;
trait TAbstractReader { trait TAbstractReader {
/** @param Upload|string|array $reader */ /** @param Upload|string|array $reader */
static function with($reader): self { static function with($reader, ?array $params=null): IReader {
if ($reader instanceof self) return $reader; if ($reader instanceof self) return $reader;
$class = null; $class = null;
if ($reader instanceof Upload) { if ($reader instanceof Upload) {
if ($reader->isExt(".csv")) $class = CsvReader::class; if ($reader->isExt(".csv")) $class = CsvReader::class;
else $class = static::class; else $class = static::class;
return new $class($reader->tmpName); return new $class($reader->tmpName, $params);
} }
if (is_string($reader)) $reader = ["input" => $reader];
if (!is_array($reader)) { if (is_string($reader)) {
$params["input"] = $reader;
} elseif (is_array($reader)) {
$params = cl::merge($reader, $params);
} elseif ($reader !== null) {
throw ValueException::invalid_type($reader, self::class); throw ValueException::invalid_type($reader, self::class);
} }
$input = $reader["input"] ?? null;
$input = $params["input"] ?? null;
if (is_string($input) && path::ext($input) === ".csv") { if (is_string($input) && path::ext($input) === ".csv") {
$class = CsvReader::class; $class = CsvReader::class;
} }
if ($class === null) $class = static::class; if ($class === null) $class = static::class;
return new $class(null, $reader); return new $class(null, $params);
} }
} }