Skip to content

Commit

Permalink
Merge 3731931 into bc7a1e7
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakubko committed May 28, 2020
2 parents bc7a1e7 + 3731931 commit 899facb
Show file tree
Hide file tree
Showing 19 changed files with 586 additions and 95 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/

composer.phar
composer.lock
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: php
php:
- 7.3

install:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --no-interaction

# Testing the app (see phpunit.xml) for configs, generating Code Coverage report
script:
- mkdir -p build/logs
- php vendor/bin/phpunit -c phpunit.xml.dist

# Submit coverage report
after_success:
- travis_retry php vendor/bin/php-coveralls

# Monitor only these branches
branches:
only:
- master
- dev
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Yakub

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

> This class help read and create new csv/xlsx files. Optimized for simple but big spreadsheet.
- [Install](#install)
- [Settings](#settings)
- [CSV](#csv)
* [Read](#read-csv-file)
* [Create](#create-csv-file)
- [XLSX](#xlsx)
* [Read](#read-xlsx-file)
* [Create](#create-xlsx-file)
- [Batch usage](#batch-usage)


**Build status**

master: [![Build Status](https://travis-ci.com/Yakubko/yxel.svg?branch=master)](https://travis-ci.com/Yakubko/yxel)
[![Coverage Status](https://coveralls.io/repos/github/Yakubko/yxel/badge.svg?branch=master)](https://coveralls.io/github/Yakubko/yxel?branch=master)

dev: [![Build Status](https://travis-ci.com/Yakubko/yxel.svg?branch=dev)](https://travis-ci.com/Yakubko/yxel)
[![Coverage Status](https://coveralls.io/repos/github/Yakubko/yxel/badge.svg?branch=dev)](https://coveralls.io/github/Yakubko/yxel?branch=dev)

- [Install](#install)
- [Settings](#settings)
- [CSV](#csv)
- [Read](#read-csv-file)
- [Create](#create-csv-file)
- [XLSX](#xlsx)
- [Read](#read-xlsx-file)
- [Create](#create-xlsx-file)
- [Batch usage](#batch-usage)

## Install

The recommended way to install is via Composer:
Expand Down Expand Up @@ -103,7 +110,7 @@ $write->save();
In other process just open existing file

```php
// Name of file must be same
// Name of file must be same
$write = \Yakub\Yxel\Main::write('yxel_test', \Yakub\Yxel\Main::CSV);

// Add new row to previous in this file
Expand All @@ -115,7 +122,7 @@ $write->close();

To cominication between processes can use storage for custom data

```php
```php
$write = \Yakub\Yxel\Main::write('yxel_test', \Yakub\Yxel\Main::CSV);

$write->addRow(['A1', 'B1', '']);
Expand Down
27 changes: 17 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
{
"name" : "yakub/yxel",
"type" : "library",
"description" : "This class help read and create new csv/xlsx files. Optimized for simple but big spreadsheet.",
"license" : "Apache-2.0",
"require" : {
"php" : ">=5.3.0"
},
"autoload" : {
"psr-4" : {
"Yakub\\Yxel\\" : "src"
"name": "yakub/yxel",
"type": "library",
"description": "This class help read and create new csv/xlsx files. Optimized for simple but big spreadsheet.",
"license": "Apache-2.0",
"autoload": {
"psr-4": {
"Yakub\\Yxel\\": "src"
}
},
"scripts": {
"test": "phpunit"
},
"require": {
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^9.1",
"php-coveralls/php-coveralls": "^2.2"
}
}
33 changes: 33 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap = "vendor/autoload.php"
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false">

<testsuites>
<testsuite name="Test Suite for simple-templating">
<directory suffix=".php">tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

<php>
<env name="APP_ENV" value="testing"/>
</php>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

</phpunit>
6 changes: 3 additions & 3 deletions src/Csv/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public function getRows($callback) {
// Start loop for reading rows and call $callback function
$rowPosition = 0;
while (! feof($handle) && $row = fgetcsv($handle, 0, $delimiter)) {
$formatedRow = [];
$formattedRow = [];
$currentPosition = 'a';
foreach ($row as $column) {
$formatedRow[$currentPosition++] = $column;
$formattedRow[$currentPosition++] = $column;
}

if ($callback($formatedRow, $rowPosition++) === false) {
if ($callback($formattedRow, $rowPosition++) === false) {
break;
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/Csv/Write.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ class Write extends \Yakub\Yxel\Main implements \Yakub\Yxel\iWrite {
private $settings = [];

protected function __construct($name = null) {
$this->file = $name?: uniqid('file_write_');
if (is_null(static::$creatingDir)) {
static::$creatingDir = sys_get_temp_dir().'/';
}
$this->dir = static::$creatingDir.$this->file;

if (is_null(static::$creatingDir)) { static::$creatingDir = sys_get_temp_dir(); }
$this->file = $name ?: uniqid('file_write_');
$this->dir = static::$creatingDir.'/'.$this->file;

// Create/Load settings
if (! file_exists($this->dir)) {
Expand Down
49 changes: 31 additions & 18 deletions src/Main.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Main {

protected static $creatingDir;

protected function __construct() {}

/**
* Init writing
*
Expand All @@ -23,11 +25,11 @@ class Main {
*
* @return NULL|iWrite
*/
public static function write($name = null, $type = self::CSV) {
public static function write($fileName = null, $type = self::CSV) {
$ret = null;
$name = trim($name);
$name = is_null($fileName) ? null : static::sanitizeFileName($fileName);

if (! empty($name)) {
if (is_null($name) || ! empty($name)) {
switch ($type) {
case self::CSV:
$ret = new \Yakub\Yxel\Csv\Write($name);
Expand All @@ -45,24 +47,24 @@ public static function write($name = null, $type = self::CSV) {
/**
* Init reading
*
* @param string $name - Path to file
* @param string $pathToFile - Path to file
*
* @return NULL|iRead
*/
public static function read($name) {
public static function read($pathToFile) {
$ret = null;

if (file_exists($name)) {
$ext = pathinfo($name, PATHINFO_EXTENSION);
// $mime = mime_content_type($name);
if (file_exists($pathToFile)) {
$ext = pathinfo($pathToFile, PATHINFO_EXTENSION);
// $mime = mime_content_type($pathToFile);

switch (true) {
case $ext == 'csv': // ($mime == 'text/plain' &&
$ret = new \Yakub\Yxel\Csv\Read($name);
$ret = new \Yakub\Yxel\Csv\Read($pathToFile);
break;

case $ext == 'xlsx': // (($mime == 'application/octet-stream' || $mime == 'application/zip') &&
$ret = new \Yakub\Yxel\Xlsx\Read($name);
$ret = new \Yakub\Yxel\Xlsx\Read($pathToFile);
break;
}
}
Expand All @@ -72,10 +74,23 @@ public static function read($name) {

/**
*
* @param string $parh - Path where script have permission to write
* @param string $path - Path where script have permission to write
*/
public static function setCreatingDir($path) {
if (! is_dir($path) || ! is_writable($path)) { return false; }

static::$creatingDir = $path;
return true;
}

/**
* Clean file name before using him
*/
public static function setCreatingDir($parh) {
static::$creatingDir = $parh;
protected static function sanitizeFileName($fileName) {
$fileName = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', trim($fileName));
$fileName = mb_ereg_replace("([\.]{2,})", '', $fileName);

return $fileName;
}

/**
Expand All @@ -84,7 +99,7 @@ public static function setCreatingDir($parh) {
* @param string $dir - Path to dir to delete
*/
protected function rrmdir($dir) {
if (is_dir($dir)) {
if (static::$creatingDir && (substr($dir, 0, strlen(static::$creatingDir)) === static::$creatingDir) && is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
Expand All @@ -95,12 +110,10 @@ protected function rrmdir($dir) {
rmdir($dir);
}
}

protected function __construct() {}
}

interface iRead {

/**
*
* @param callable $callback - Function where are two arguments. First is row exploded to array and second is number of row
Expand All @@ -109,7 +122,7 @@ public function getRows($callback);
}

interface iWrite {

public function addRow($row = []);
public function getFilePath();
public function settings($name = null, $value = null);
Expand Down
Loading

0 comments on commit 899facb

Please sign in to comment.