beautified
This commit is contained in:
parent
ddf593e42d
commit
24ef880439
33
README.md
33
README.md
|
@ -1,2 +1,33 @@
|
||||||
# csv
|
# Comma-Separated Values by RFC 4180
|
||||||
|
A lightweight database in pure PHP<br>
|
||||||
|
It will perfectly replace complex databases in simple projects
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
- PHP 8.4
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
1. `composer require mirzaev/csv`
|
||||||
|
2. Create a class that inherits `mirzaev/csv/database` and redefine the `database::FILE` constant
|
||||||
|
3. Enjoy!
|
||||||
|
|
||||||
|
## Example
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Library for CSV
|
||||||
|
use mirzaev\csv\{database, record};
|
||||||
|
|
||||||
|
// Initializing the database
|
||||||
|
$database = new database('name', 'age', 'created');
|
||||||
|
|
||||||
|
// Initializing the record
|
||||||
|
$record = new record(['Arsen', '23', time());
|
||||||
|
|
||||||
|
// Writing to the database
|
||||||
|
$database->write($record);
|
||||||
|
?>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Used by
|
||||||
|
- My site-article about how i was kidnapped by PMC Wagner operatives [mirzaev/repression](https://git.mirzaev.sexy/mirzaev/repression)
|
||||||
|
- My decentralized P2P blockchain chats project [mirzaev/notchat](https://git.mirzaev.sexy/mirzaev/notchat)
|
||||||
|
|
|
@ -69,14 +69,14 @@ class database
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param array|null $columns Columns
|
* @param string ...$columns Columns
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(?array $columns = null)
|
public function __construct(string ...$columns)
|
||||||
{
|
{
|
||||||
// Initializing columns
|
// Initializing columns
|
||||||
if (isset($columns)) $this->columns = $columns;
|
if (!empty($columns)) $this->columns = $columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -43,14 +43,14 @@ class record
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param string|null $row Row for converting to record instance parameters
|
* @param mixed $parameters Parameter of the record
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(?string $row = null)
|
public function __construct(mixed ...$parameters)
|
||||||
{
|
{
|
||||||
// Initializing parameters
|
// Initializing parameters
|
||||||
if (isset($row)) $this->parameters = static::deserialize($row);
|
if (!empty($parameters)) $this->parameters = $parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -105,7 +105,7 @@ class record
|
||||||
*
|
*
|
||||||
* @return array Deserialized record
|
* @return array Deserialized record
|
||||||
*/
|
*/
|
||||||
public function deserialize(string $row): array
|
public static function deserialize(string $row): array
|
||||||
{
|
{
|
||||||
// Separating row by commas
|
// Separating row by commas
|
||||||
preg_match_all('/(.*)(?>(?<!\\\),|$)/Uu', $row, $matches);
|
preg_match_all('/(.*)(?>(?<!\\\),|$)/Uu', $row, $matches);
|
||||||
|
|
|
@ -10,7 +10,7 @@ require(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRE
|
||||||
$action = 0;
|
$action = 0;
|
||||||
|
|
||||||
// Initializing the test database
|
// Initializing the test database
|
||||||
$database = new database([
|
$database = new database(
|
||||||
'empty_value_at_the_beginning',
|
'empty_value_at_the_beginning',
|
||||||
'name',
|
'name',
|
||||||
'second_name',
|
'second_name',
|
||||||
|
@ -30,12 +30,12 @@ $database = new database([
|
||||||
'string_with_space_at_the_beginning',
|
'string_with_space_at_the_beginning',
|
||||||
'string_with_escaped_comma',
|
'string_with_escaped_comma',
|
||||||
'string_with_unicode_symbols'
|
'string_with_unicode_symbols'
|
||||||
]);
|
);
|
||||||
|
|
||||||
echo '[' . ++$action . "] Created the database instance with columns\n";
|
echo '[' . ++$action . "] Created the database instance with columns\n";
|
||||||
|
|
||||||
// Initializing the test record with the test row
|
// Initializing the test record with the test row
|
||||||
$record = new record(',"Arsen","Mirzaev","23",true,,"",100,null,"null","102.1",300.34,1001.23145,5000.400.400,"test ""value""","another"" test "" value with ""two double quotes pairs"" yeah"," starts with space","has\, an escaped comma inside","unicode символы"');
|
$record = new record(...record::deserialize(',"Arsen","Mirzaev","23",true,,"",100,null,"null","102.1",300.34,1001.23145,5000.400.400,"test ""value""","another"" test "" value with ""two double quotes pairs"" yeah"," starts with space","has\, an escaped comma inside","unicode символы"'));
|
||||||
|
|
||||||
echo '[' . ++$action . "] Created the record with the test row\n";
|
echo '[' . ++$action . "] Created the record with the test row\n";
|
||||||
|
|
Loading…
Reference in New Issue