Skip to content

Commit

Permalink
Rename Record to Row
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed May 20, 2024
1 parent 3ff35cb commit 8461092
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
24 changes: 12 additions & 12 deletions docs/9.0/reader/statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ used independently to help create your own where expression as shown in the foll

```php
use League\Csv\Query;
use League\Csv\Query\Constraint;

$data = [
['volume' => 67, 'edition' => 2],
Expand All @@ -185,9 +184,9 @@ $data = [
['volume' => 67, 'edition' => 7],
];

$criteria = Constraint\Criteria::xany(
Constraint\Column::filterOn('volume', 'gt', 80),
fn (mixed $record, int|string $key) => Query\Record::from($record)->field('edition') < 6
$criteria = Query\Constraint\Criteria::xany(
Query\Constraint\Column::filterOn('volume', 'gt', 80),
fn (mixed $record, int|string $key) => Query\Row::from($record)->field('edition') < 6
);

$filteredData = array_filter($data, $criteria, ARRAY_FILTER_USE_BOTH));
Expand All @@ -205,13 +204,13 @@ also applied. The callable accepted is similar to the one used by the `usort` fu
As an example let's order the records according to the lastname found on the records.

```php
use League\Csv\Query;
use League\Csv\Reader;
use League\Csv\Query\Record;
use League\Csv\Statement;

$reader = Reader::createFromPath('/path/to/file.csv');
$records = Statement::create()
->orderBy(fn (mixed $rA, mixed $rB): int => strcmp(Record::from($rB)->field(1) ?? '', Record::from($rA)->field(1) ?? '')))
->orderBy(fn (mixed $rA, mixed $rB): int => strcmp(Query\Row::from($rB)->field(1) ?? '', Query\Row::from($rA)->field(1) ?? '')))
->process($reader);
// $records is a League\Csv\ResultSet instance
```
Expand Down Expand Up @@ -246,13 +245,13 @@ use the `orderBy` method with the classes defined under the `League\Csv\Query` n

```php

use League\Csv\Query\Ordering;
use League\Csv\Query;
use League\Csv\Reader;
use League\Csv\Statement;

$sort = Ordering\MultiSort::all(
Ordering\Column::sortBy(1, 'desc'),
Ordering\Column::sortBy('foo', 'asc', strcmp(...)),
$sort = Query\Ordering\MultiSort::all(
Query\Ordering\Column::sortBy(1, 'desc'),
Query\Ordering\Column::sortBy('foo', 'asc', strcmp(...)),
);

$reader = Reader::createFromPath('/path/to/file.csv');
Expand Down Expand Up @@ -312,8 +311,8 @@ use League\Csv\Statement;

$constraints = Statement::create()
->select('Integer', 'Text', 'Date and Time')
->where(fn (array $record): bool => (float) $record['Float'] < 1.3)
->orderBy(fn (array $r1, array $r2): int => (int) $r2['Integer'] <=> (int) $r1['Integer'])
->andWhere('Float', '<', 1.3)
->orderByDesc('Integer')
->offset(2)
->limit(5);

Expand All @@ -329,6 +328,7 @@ CSV;

$csv = Reader::createFromString($document);
$csv->setHeaderOffset(0);
$csv->addFormatter(fn (array $record) => [...$record, ...['Float' => (float) $record['Float'], 'Integer' => (int) $record['Integer']]])
$records = $constraints->process($csv);
//returns a ResultSet containing records which validate all the constraints.
```
Expand Down
4 changes: 2 additions & 2 deletions src/Query/Constraint/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Iterator;
use IteratorIterator;
use League\Csv\Query\Predicate;
use League\Csv\Query\Record;
use League\Csv\Query\Row;
use League\Csv\InvalidArgument;
use League\Csv\StatementError;
use ReflectionException;
Expand Down Expand Up @@ -68,7 +68,7 @@ public static function filterOn(
*/
public function __invoke(mixed $value, int|string $key): bool
{
return $this->operator->compare(Record::from($value)->value($this->column), $this->value);
return $this->operator->compare(Row::from($value)->value($this->column), $this->value);
}

public function filter(iterable $value): Iterator
Expand Down
10 changes: 5 additions & 5 deletions src/Query/Constraint/CriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use ArrayIterator;
use CallbackFilterIterator;
use League\Csv\Query\Record;
use League\Csv\Query\Row;
use League\Csv\Query\QueryTestCase;

use PHPUnit\Framework\Attributes\Test;
Expand All @@ -38,8 +38,8 @@ public function it_returns_a_value_when_no_predicate_is_given(): void
#[Test]
public function it_returns_a_value_when_some_predicates_are_given(): void
{
$predicate1 = fn (mixed $record, int $key) => Record::from($record)->value('volume') > 80;
$predicate2 = fn (mixed $record, int $key) => Record::from($record)->value('edition') < 6;
$predicate1 = fn (mixed $record, int $key) => Row::from($record)->value('volume') > 80;
$predicate2 = fn (mixed $record, int $key) => Row::from($record)->value('edition') < 6;

self::assertSame([
1 => ['volume' => 86, 'edition' => 1],
Expand Down Expand Up @@ -70,8 +70,8 @@ public function it_returns_the_inverse_when_using_an_empty_not(): void
{
$data = new ArrayIterator($this->iterable);

$predicate1 = fn (mixed $record, int $key) => Record::from($record)->value('volume') > 80;
$predicate2 = fn (mixed $record, int $key) => Record::from($record)->value('edition') < 6;
$predicate1 = fn (mixed $record, int $key) => Row::from($record)->value('volume') > 80;
$predicate2 = fn (mixed $record, int $key) => Row::from($record)->value('edition') < 6;

self::assertSame([
0 => ['volume' => 67, 'edition' => 2],
Expand Down
6 changes: 3 additions & 3 deletions src/Query/Constraint/TwoColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Iterator;
use IteratorIterator;
use League\Csv\Query\Predicate;
use League\Csv\Query\Record;
use League\Csv\Query\Row;
use League\Csv\InvalidArgument;
use League\Csv\StatementError;
use ReflectionException;
Expand Down Expand Up @@ -78,8 +78,8 @@ public static function filterOn(
public function __invoke(mixed $value, int|string $key): bool
{
$val = match (true) {
is_array($this->second) => array_values(Record::from($value)->select(...$this->second)),
default => Record::from($value)->value($this->second),
is_array($this->second) => array_values(Row::from($value)->select(...$this->second)),
default => Row::from($value)->value($this->second),
};

return Column::filterOn($this->first, $this->operator, $val)($value, $key);
Expand Down
6 changes: 3 additions & 3 deletions src/Query/Ordering/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Closure;
use Iterator;
use IteratorIterator;
use League\Csv\Query\Record;
use League\Csv\Query\Row;
use League\Csv\InvalidArgument;
use League\Csv\Query\Sort;
use League\Csv\StatementError;
Expand Down Expand Up @@ -79,8 +79,8 @@ public static function sortBy(
*/
public function __invoke(mixed $valueA, mixed $valueB): int
{
$first = Record::from($valueA)->value($this->column);
$second = Record::from($valueB)->value($this->column);
$first = Row::from($valueA)->value($this->column);
$second = Row::from($valueB)->value($this->column);

return match ($this->direction) {
self::ASCENDING => ($this->callback)($first, $second),
Expand Down
6 changes: 3 additions & 3 deletions src/Query/Record.php → src/Query/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
use function lcfirst;
use function str_replace;

final class Record
final class Row
{
public static function from(mixed $value): Record
public static function from(mixed $value): Row
{
return new self(match (true) {
is_object($value),
Expand All @@ -52,7 +52,7 @@ private function __construct(private readonly array|object $record)
*
* @throws ReflectionException
* @throws StatementError If the value can not be retrieved
*@see Record::select()
*@see Row::select()
*
*/
public function value(string|int $key): mixed
Expand Down

0 comments on commit 8461092

Please sign in to comment.