Skip to content

Commit

Permalink
Updated repository structure and added strict types. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
tannguyen04 authored Dec 19, 2023
1 parent f427724 commit e5c42ee
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 107 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/auto-assign-pr-author.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ name: 'Auto Author Assign'

on:
pull_request_target:
types: [opened, reopened]
types:
- opened
- reopened

permissions:
pull-requests: write
Expand All @@ -11,4 +13,4 @@ jobs:
assign-author:
runs-on: ubuntu-latest
steps:
- uses: toshimaru/auto-author-assign@v1.4.0
- uses: toshimaru/auto-author-assign@v2.0.1
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: Release Drafter
name: Release

on:
push:
tags:
- '*'
branches:
- main
pull_request:
types: [opened, reopened, synchronize]

permissions:
contents: read
contents: write

jobs:
update_release_draft:
release-drafter:
permissions:
contents: write
pull-requests: write
Expand Down
31 changes: 20 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
name: Test

on:
push:
pull_request:
branches:
- main
- 'feature/**'

push:
branches:
- main
jobs:
test:
test-php:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
- uses: php-actions/composer@v6
- run: composer lint
- run: composer test
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
- uses: shivammathur/setup-php@v2
- run: composer install
- run: composer lint
- run: composer test:coverage
- uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
files: ./cobertura.xml
fail_ci_if_error: true
94 changes: 49 additions & 45 deletions CsvTable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

namespace AlexSkrypnyk\CsvTable;

/**
Expand All @@ -14,49 +16,49 @@ class CsvTable {
*
* @var string
*/
protected $csvString;
protected string $csvString;

/**
* The character used to separate values in the CSV data.
*
* @var string
*/
protected $csvSeparator;
protected string $csvSeparator;

/**
* The character used to enclose values in the CSV data.
*
* @var string
*/
protected $csvEnclosure;
protected string $csvEnclosure;

/**
* The character used to escape special characters in the CSV data.
*
* @var string
*/
protected $csvEscape;
protected string $csvEscape;

/**
* Array containing the header row from the CSV data.
*
* @var array
* @var array<string>
*/
protected $header = [];
protected array $header = [];

/**
* Array containing all non-header rows from the CSV data.
*
* @var array
* @var array<array<string>>
*/
protected $rows = [];
protected array $rows = [];

/**
* Boolean flag indicating whether the header should be parsed.
*
* @var bool
*/
protected $useHeader = TRUE;
protected bool $useHeader = TRUE;

/**
* Constructs a CsvTable object.
Expand All @@ -71,7 +73,7 @@ class CsvTable {
* The character used to escape special characters in the CSV data.
* Defaults to '\\'.
*/
public function __construct($csvString = NULL, $separator = ',', $enclosure = '"', $escape = '\\') {
public function __construct(string $csvString = NULL, string $separator = ',', string $enclosure = '"', string $escape = '\\') {
$this->csvSeparator = $separator;
$this->csvEnclosure = $enclosure;
$this->csvEscape = $escape;
Expand All @@ -86,7 +88,7 @@ public function __construct($csvString = NULL, $separator = ',', $enclosure = '"
/**
* Get header columns.
*
* @return array
* @return array<string>
* Array of header columns.
*/
public function getHeader(): array {
Expand All @@ -96,7 +98,7 @@ public function getHeader(): array {
/**
* Get rows without the header.
*
* @return array
* @return array<array<string>>
* Array of rows without the header.
*/
public function getRows(): array {
Expand All @@ -108,7 +110,7 @@ public function getRows(): array {
*
* @return $this
*/
public function hasHeader() {
public function hasHeader(): static {
$this->useHeader = TRUE;
$this->parse();
return $this;
Expand All @@ -119,7 +121,7 @@ public function hasHeader() {
*
* @return $this
*/
public function noHeader() {
public function noHeader(): static {
$this->useHeader = FALSE;
$this->parse();
return $this;
Expand All @@ -141,24 +143,24 @@ public function noHeader() {
* @return CsvTable
* A new CsvTable object containing the contents of the file.
*
* @throws Exception
* @throws \Exception
* When the file is not readable.
*/
public static function fromFile($filepath, $separator = ',', $enclosure = '"', $escape = '\\') {
public static function fromFile($filepath, $separator = ',', $enclosure = '"', $escape = '\\'): CsvTable {
if (!is_readable($filepath)) {
throw new \Exception('File not readable');
}

/* @phpstan-ignore-next-line */
return new static(file_get_contents($filepath), $separator, $enclosure, $escape);
}

/**
* Render the CSV data.
*
* @param callable|null $renderer
* @param callable|string|null $renderer
* A callable to renderer the output. Defaults to NULL, which uses the
* default renderer.
* @param array $options
* @param array<mixed> $options
* An array of options to pass to the renderer. Defaults to an empty array.
*
* @return string
Expand Down Expand Up @@ -188,19 +190,19 @@ public function render(callable|string $renderer = NULL, array $options = []): s
/**
* Parse the CSV string into header and rows.
*/
protected function parse() {
protected function parse(): void {
$rows = [];

if (!empty($this->csvString)) {
$stream = fopen('php://memory', 'r+');
fwrite($stream, $this->csvString);
rewind($stream);

while (($data = fgetcsv($stream, 0, $this->csvSeparator, $this->csvEnclosure, $this->csvEscape)) !== FALSE) {
$rows[] = $data;
if ($stream) {
fwrite($stream, $this->csvString);
rewind($stream);
while (($data = fgetcsv($stream, 0, $this->csvSeparator, $this->csvEnclosure, $this->csvEscape)) !== FALSE) {
$rows[] = $data;
}
fclose($stream);
}

fclose($stream);
}

$this->header = $this->useHeader && count($rows) > 0 ? array_slice($rows, 0, 1)[0] : [];
Expand All @@ -210,47 +212,49 @@ protected function parse() {
/**
* Render as CSV.
*
* @param array $header
* @param array<string> $header
* An array containing the header row.
* @param array $rows
* @param array<array<string>> $rows
* An array containing all non-header rows.
* @param array $options
* @param array<mixed> $options
* An array of options for the renderer.
*
* @return string
* The formatted output.
*/
public static function renderCsv($header, $rows, $options): string {
public static function renderCsv(array $header, array $rows, array $options): string {
$output = '';
$out = fopen('php://temp/maxmemory:' . (5 * 1024 * 1024), 'r+');
if ($out) {
if (count($header) > 0) {
/* @phpstan-ignore-next-line */
fputcsv($out, $header, $options['separator'], $options['enclosure'], $options['escape']);
}
foreach ($rows as $row) {
/* @phpstan-ignore-next-line */
fputcsv($out, $row, $options['separator'], $options['enclosure'], $options['escape']);
}

if (count($header) > 0) {
fputcsv($out, $header, $options['separator'], $options['enclosure'], $options['escape']);
}
foreach ($rows as $row) {
fputcsv($out, $row, $options['separator'], $options['enclosure'], $options['escape']);
rewind($out);
$output = (string) stream_get_contents($out);
fclose($out);
}

rewind($out);
$output = stream_get_contents($out);
fclose($out);

return $output;
}

/**
* Render as a table.
*
* @param array $header
* @param array<string> $header
* An array containing the header row.
* @param array $rows
* @param array<array<string>> $rows
* An array containing all non-header rows.
* @param array $options
* An array of options for the renderer.
*
* @return string
* The formatted output.
*/
public static function renderTable($header, $rows, $options): string {
public static function renderTable(array $header, array $rows): string {
if (count($header) > 0) {
$header = implode('|', $header);
$header = $header . "\n" . str_repeat('-', strlen($header)) . "\n";
Expand Down
Loading

0 comments on commit e5c42ee

Please sign in to comment.