Skip to content

Commit

Permalink
Improve force enclosure implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Aug 1, 2023
1 parent a7b8b5b commit 65105be
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 136 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
}
],
"require": {
"php" : "^8.1.2",
"ext-json" : "*",
"ext-mbstring" : "*"
"php": "^8.1.2",
"ext-json": "*",
"ext-mbstring": "*"
},
"require-dev": {
"ext-dom": "*",
"ext-xdebug": "*",
"doctrine/collections": "^2.1.2",
"friendsofphp/php-cs-fixer": "^v3.14.3",
"phpbench/phpbench": "^1.2.10",
Expand All @@ -40,7 +41,7 @@
"phpstan/phpstan-phpunit": "^1.3.11",
"phpstan/phpstan-strict-rules": "^1.5.1",
"phpunit/phpunit": "^10.0.19",
"ext-xdebug": "*"
"symfony/var-dumper": "^6.3"
},
"autoload": {
"psr-4": {
Expand Down
32 changes: 20 additions & 12 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public static function createFromPath(string $path, string $open_mode = 'r', $co

protected function resetProperties(): void
{
parent::resetProperties();

$this->nb_records = -1;
$this->header = [];
}
Expand Down Expand Up @@ -180,28 +182,22 @@ public function fetchColumnByOffset(int $offset = 0): Iterator
return ResultSet::createFromTabularDataReader($this)->fetchColumnByOffset($offset);
}

/** @codeCoverageIgnore */
public function fetchColumn($index = 0): Iterator
{
return ResultSet::createFromTabularDataReader($this)->fetchColumn($index);
}

/**
* @throws Exception
*/
public function first(): array
{
return ResultSet::createFromTabularDataReader($this)->first();
}

/**
* @throws Exception
*/
public function nth(int $nth_record): array
{
return ResultSet::createFromTabularDataReader($this)->nth($nth_record);
}

/** @codeCoverageIgnore */
public function fetchOne(int $nth_record = 0): array
{
return ResultSet::createFromTabularDataReader($this)->nth($nth_record);
}

public function fetchPairs($offset_index = 0, $value_index = 1): Iterator
{
return ResultSet::createFromTabularDataReader($this)->fetchPairs($offset_index, $value_index);
Expand Down Expand Up @@ -406,4 +402,16 @@ public function isEmptyRecordsIncluded(): bool
{
return $this->is_empty_records_included;
}

/** @codeCoverageIgnore */
public function fetchColumn($index = 0): Iterator
{
return ResultSet::createFromTabularDataReader($this)->fetchColumn($index);
}

/** @codeCoverageIgnore */
public function fetchOne(int $nth_record = 0): array
{
return $this->nth($nth_record);
}
}
68 changes: 33 additions & 35 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
class ResultSet implements TabularDataReader, JsonSerializable
{
/**
* @param Iterator<array-key, array<array-key, string|null>> $records
* @param array<string> $header
*
* @throws SyntaxError
*/
public function __construct(
/** @var Iterator<array-key, array<array-key, string|null>> */
protected Iterator $records,
/** @var array<string> */
protected array $header = []
) {
public function __construct(protected Iterator $records, protected array $header = [])
{
$this->validateHeader($this->header);
}

Expand Down Expand Up @@ -140,18 +139,6 @@ public function jsonSerialize(): array
return array_values([...$this->records]);
}

/**
* @deprecated since version 9.9.0
*
* @see ::nth
*
* @codeCoverageIgnore
*/
public function fetchOne(int $nth_record = 0): array
{
return $this->nth($nth_record);
}

public function first(): array
{
return $this->nth(0);
Expand Down Expand Up @@ -192,23 +179,6 @@ public function fetchColumnByOffset(int $offset): Iterator
);
}

/**
* @deprecated since version 9.8.0
*
* @see ::fetchColumnByName
* @see ::fetchColumnByOffset
*
* @codeCoverageIgnore
* @throws Exception
*/
public function fetchColumn($index = 0): Iterator
{
return $this->yieldColumn(
$this->getColumnIndex($index, 'offset', __METHOD__)
);
}


protected function yieldColumn(string|int $offset): Generator
{
yield from new MapIterator(
Expand Down Expand Up @@ -283,4 +253,32 @@ public function fetchPairs($offset_index = 0, $value_index = 1): Iterator
yield $pair[0] => $pair[1];
}
}

/**
* @deprecated since version 9.9.0
*
* @see ::nth
*
* @codeCoverageIgnore
*/
public function fetchOne(int $nth_record = 0): array
{
return $this->nth($nth_record);
}

/**
* @deprecated since version 9.8.0
*
* @see ::fetchColumnByName
* @see ::fetchColumnByOffset
*
* @codeCoverageIgnore
* @throws Exception
*/
public function fetchColumn($index = 0): Iterator
{
return $this->yieldColumn(
$this->getColumnIndex($index, 'offset', __METHOD__)
);
}
}
30 changes: 15 additions & 15 deletions src/TabularDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ public function getHeader(): array;
*/
public function getRecords(array $header = []): Iterator;

/**
* Returns the next key-value pairs from the tabular data (first
* column is the key, second column is the value).
*
* By default, if no column index is provided:
* - the first column is used to provide the keys
* - the second column is used to provide the value
*
* @param string|int $offset_index The column index to serve as offset
* @param string|int $value_index The column index to serve as value
*
* @throws UnableToProcessCsv if the column index is invalid or not found
*/
public function fetchPairs($offset_index = 0, $value_index = 1): Iterator;

/**
* DEPRECATION WARNING! This class will be removed in the next major point release.
*
Expand Down Expand Up @@ -109,19 +124,4 @@ public function fetchOne(int $nth_record = 0): array;
* @throws UnableToProcessCsv if the column index is invalid or not found
*/
public function fetchColumn($index = 0): Iterator;

/**
* Returns the next key-value pairs from the tabular data (first
* column is the key, second column is the value).
*
* By default, if no column index is provided:
* - the first column is used to provide the keys
* - the second column is used to provide the value
*
* @param string|int $offset_index The column index to serve as offset
* @param string|int $value_index The column index to serve as value
*
* @throws UnableToProcessCsv if the column index is invalid or not found
*/
public function fetchPairs($offset_index = 0, $value_index = 1): Iterator;
}
Loading

0 comments on commit 65105be

Please sign in to comment.