-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* List select function * Added missing classes
- Loading branch information
1 parent
b4fdb76
commit a09e99f
Showing
11 changed files
with
239 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Function; | ||
|
||
use Flow\ETL\Row\Reference; | ||
|
||
final class ListFunctions | ||
{ | ||
public function __construct(private readonly Reference $ref) | ||
{ | ||
} | ||
|
||
public function select(Reference|string ...$refs) : ListSelect | ||
{ | ||
return new ListSelect($this->ref, ...$refs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Function; | ||
|
||
use Flow\ETL\Row; | ||
use Flow\ETL\Row\Entry\ListEntry; | ||
use Flow\ETL\Row\EntryReference; | ||
use Flow\ETL\Row\Reference; | ||
use Flow\ETL\Row\References; | ||
|
||
final class ListSelect implements ScalarFunction | ||
{ | ||
private readonly Reference $ref; | ||
|
||
private readonly References $refs; | ||
|
||
public function __construct( | ||
Reference|string $ref, | ||
Reference|string ...$refs, | ||
) { | ||
$this->ref = EntryReference::init($ref); | ||
$this->refs = References::init(...$refs); | ||
} | ||
|
||
public function eval(Row $row) : array|null | ||
{ | ||
if (!$row->has($this->ref)) { | ||
return null; | ||
} | ||
|
||
$list = $row->get($this->ref); | ||
|
||
if (!$list instanceof ListEntry) { | ||
return null; | ||
} | ||
|
||
$output = []; | ||
|
||
foreach ($list->value() as $index => $element) { | ||
$output[$index] = []; | ||
|
||
foreach ($this->refs as $ref) { | ||
if (\is_array($element) && \array_key_exists($ref->to(), $element)) { | ||
$output[$index][$ref->name()] = $element[$ref->to()]; | ||
} else { | ||
$output[$index][$ref->name()] = null; | ||
} | ||
} | ||
} | ||
|
||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/core/etl/tests/Flow/ETL/Tests/Integration/Function/ListSelectTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Integration\Function; | ||
|
||
use function Flow\ETL\DSL\df; | ||
use function Flow\ETL\DSL\from_array; | ||
use function Flow\ETL\DSL\list_ref; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ListSelectTest extends TestCase | ||
{ | ||
public function test_selecting_properties_from_list() : void | ||
{ | ||
$rows = df() | ||
->read( | ||
from_array([ | ||
['list' => [['id' => 1, 'name' => 'test'], ['id' => 2, 'name' => 'test2'], ['id' => 3, 'name' => 'test3']]], | ||
['list' => [['id' => 4, 'name' => 'test4'], ['id' => 5, 'name' => 'test5'], ['id' => 6, 'name' => 'test6']]], | ||
]) | ||
) | ||
->withEntry('list', list_ref('list')->select('id')) | ||
->fetch(); | ||
|
||
$this->assertEquals( | ||
[ | ||
['list' => [['id' => 1], ['id' => 2], ['id' => 3]]], | ||
['list' => [['id' => 4], ['id' => 5], ['id' => 6]]], | ||
], | ||
$rows->toArray() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
121 changes: 121 additions & 0 deletions
121
src/core/etl/tests/Flow/ETL/Tests/Unit/Function/ListSelectTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Unit\Function; | ||
|
||
use function Flow\ETL\DSL\list_entry; | ||
use function Flow\ETL\DSL\ref; | ||
use function Flow\ETL\DSL\row; | ||
use function Flow\ETL\DSL\struct_element; | ||
use function Flow\ETL\DSL\struct_type; | ||
use function Flow\ETL\DSL\type_int; | ||
use function Flow\ETL\DSL\type_list; | ||
use function Flow\ETL\DSL\type_string; | ||
use Flow\ETL\Function\ListSelect; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ListSelectTest extends TestCase | ||
{ | ||
public function test_selecting_non_existing_value_from_list_using_alias() : void | ||
{ | ||
$list = row( | ||
list_entry( | ||
'list', | ||
[ | ||
['id' => 1, 'name' => 'test'], | ||
['id' => 2, 'name' => 'test2'], | ||
['id' => 3, 'name' => 'test3'], | ||
], | ||
type_list(struct_type( | ||
struct_element('id', type_int()), | ||
struct_element('name', type_string()), | ||
)) | ||
) | ||
); | ||
|
||
$this->assertEquals( | ||
[ | ||
['id' => 1, 'mail' => null], | ||
['id' => 2, 'mail' => null], | ||
['id' => 3, 'mail' => null], | ||
], | ||
(new ListSelect(ref('list'), ref('id'), ref('mail')))->eval($list) | ||
); | ||
} | ||
|
||
public function test_selecting_value_from_list() : void | ||
{ | ||
$list = row( | ||
list_entry( | ||
'list', | ||
[ | ||
['id' => 1, 'name' => 'test'], | ||
['id' => 2, 'name' => 'test2'], | ||
['id' => 3, 'name' => 'test3'], | ||
], | ||
type_list(struct_type( | ||
struct_element('id', type_int()), | ||
struct_element('name', type_string()), | ||
)) | ||
) | ||
); | ||
|
||
$this->assertEquals( | ||
[ | ||
['id' => 1], | ||
['id' => 2], | ||
['id' => 3], | ||
], | ||
(new ListSelect(ref('list'), 'id'))->eval($list) | ||
); | ||
} | ||
|
||
public function test_selecting_value_from_list_using_alias() : void | ||
{ | ||
$list = row( | ||
list_entry( | ||
'list', | ||
[ | ||
['id' => 1, 'name' => 'test'], | ||
['id' => 2, 'name' => 'test2'], | ||
['id' => 3, 'name' => 'test3'], | ||
], | ||
type_list(struct_type( | ||
struct_element('id', type_int()), | ||
struct_element('name', type_string()), | ||
)) | ||
) | ||
); | ||
|
||
$this->assertEquals( | ||
[ | ||
['new_id' => 1], | ||
['new_id' => 2], | ||
['new_id' => 3], | ||
], | ||
(new ListSelect(ref('list'), ref('id')->as('new_id')))->eval($list) | ||
); | ||
} | ||
|
||
public function test_selecting_value_from_simple_list() : void | ||
{ | ||
$list = row( | ||
list_entry( | ||
'list', | ||
[ | ||
'a', 'b', 'c', 'd', | ||
], | ||
type_list(type_string()) | ||
) | ||
); | ||
|
||
$this->assertEquals( | ||
[ | ||
['id' => null], | ||
['id' => null], | ||
['id' => null], | ||
['id' => null], | ||
], | ||
(new ListSelect(ref('list'), ref('id')))->eval($list) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters