Skip to content

Commit

Permalink
Improve codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 19, 2025
1 parent 8b13132 commit f477260
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions docs/9.0/interoperability/tabular-data-importer.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ $names = RdbmsResult::columnNames($result);
//will return ['firstname', 'lastname', ...]
```

- convert the result into an `Iterator` using the `records` public static method.
- convert the result into an `Iterator` using the `rows` public static method.

```php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = new mysqli("localhost", "my_user", "my_password", "world");
$result = $connection->query("SELECT * FROM authors");
$result instanceOf mysqli_result || throw new RuntimeException('MySQL results not available');
foreach (RdbmsResult::records($stmt) as $record) {
foreach (RdbmsResult::rows($stmt) as $record) {
// returns each found record which match the processed query.
}
```
Expand Down Expand Up @@ -116,13 +116,13 @@ JSON;

$tabularData = new class ($payload) implements TabularData {
private readonly array $header;
private readonly ArrayIterator $records;
private readonly ArrayIterator $rows;
public function __construct(string $payload)
{
try {
$data = json_decode($payload, true);
$this->header = array_keys($data[0] ?? []);
$this->records = new ArrayIterator($data);
$this->rows = new ArrayIterator($data);
} catch (Throwable $exception) {
throw new ValueError('The provided JSON payload could not be converted into a Tabular Data instance.', previous: $exception);
}
Expand All @@ -135,7 +135,7 @@ $tabularData = new class ($payload) implements TabularData {

public function getIterator() : Iterator
{
return $this->records;
return $this->rows;
}
};

Expand Down
16 changes: 8 additions & 8 deletions src/RdbmsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
final class RdbmsResult implements TabularData
{
/**
* @param Iterator<array-key, array<array-key, mixed>> $records
* @param array<string>|array{} $headers
* @param Iterator<array-key, array<array-key, mixed>> $rows
* @param array<string>|array{} $header
*/
private function __construct(
private readonly Iterator $records,
private readonly array $headers
private readonly Iterator $rows,
private readonly array $header
) {
}

Expand All @@ -53,15 +53,15 @@ private function __construct(
*/
public function getHeader(): array
{
return $this->headers;
return $this->header;
}

/**
* @return Iterator<array-key, array<array-key, mixed>>
*/
public function getIterator(): Iterator
{
return $this->records;
return $this->rows;
}

public static function tryFrom(object $result): ?self
Expand All @@ -78,7 +78,7 @@ public static function tryFrom(object $result): ?self
*/
public static function from(object $result): self
{
return new self(self::records($result), self::columnNames($result));
return new self(self::rows($result), self::columnNames($result));
}

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ function (int $index) use ($result): string {
/**
* @return Iterator<array-key, array<array-key, mixed>>
*/
public static function records(object $result): Iterator
public static function rows(object $result): Iterator
{
return match (true) {
$result instanceof SQLite3Result => new class ($result) implements Iterator {
Expand Down

0 comments on commit f477260

Please sign in to comment.