Skip to content

Commit

Permalink
Added more php native functions to DSL::Transform (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Apr 14, 2022
1 parent 03ee16e commit 6a71356
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 51 deletions.
134 changes: 85 additions & 49 deletions src/Flow/ETL/DSL/Transform.php
Expand Up @@ -40,9 +40,9 @@

class Transform
{
final public static function add(string $leftEntry, string $rightEntry, string $newEntryName = 'add') : Transformer
final public static function add(string $leftEntry, string $rightEntry, string $new_entry_name = 'add') : Transformer
{
return MathOperationTransformer::add($leftEntry, $rightEntry, $newEntryName);
return MathOperationTransformer::add($leftEntry, $rightEntry, $new_entry_name);
}

/**
Expand Down Expand Up @@ -114,30 +114,30 @@ final public static function add_string(string $name, string $value) : Transform
return new Transformer\StaticEntryTransformer(DSLEntry::string($name, $value));
}

final public static function add_value(string $leftEntry, int|float $value, string $newEntryName = 'add') : Transformer
final public static function add_value(string $leftEntry, int|float $value, string $new_entry_name = 'add') : Transformer
{
return MathValueOperationTransformer::add($leftEntry, $value, $newEntryName);
return MathValueOperationTransformer::add($leftEntry, $value, $new_entry_name);
}

/**
* @param array<string> $keys
*/
final public static function array_collection_get(array $keys, string $arrayEntryName, string $newEntryName = 'element') : Transformer
final public static function array_collection_get(array $keys, string $arrayEntryName, string $new_entry_name = 'element') : Transformer
{
return new Transformer\ArrayCollectionGetTransformer($keys, $arrayEntryName, $newEntryName);
return new Transformer\ArrayCollectionGetTransformer($keys, $arrayEntryName, $new_entry_name);
}

/**
* @param array<string> $keys
*/
final public static function array_collection_get_first(array $keys, string $arrayEntryName, string $newEntryName = 'element') : Transformer
final public static function array_collection_get_first(array $keys, string $arrayEntryName, string $new_entry_name = 'element') : Transformer
{
return Transformer\ArrayCollectionGetTransformer::fromFirst($keys, $arrayEntryName, $newEntryName);
return Transformer\ArrayCollectionGetTransformer::fromFirst($keys, $arrayEntryName, $new_entry_name);
}

final public static function array_collection_merge(string $arrayEntryName, string $newEntryName = 'element') : Transformer
final public static function array_collection_merge(string $arrayEntryName, string $new_entry_name = 'element') : Transformer
{
return new Transformer\ArrayCollectionMergeTransformer($arrayEntryName, $newEntryName);
return new Transformer\ArrayCollectionMergeTransformer($arrayEntryName, $new_entry_name);
}

final public static function array_convert_keys(string $array_column, string $style) : Transformer
Expand Down Expand Up @@ -210,6 +210,11 @@ final public static function callback_row(callable $callable) : Transformer
return new Transformer\CallbackRowTransformer($callable);
}

final public static function ceil(string $entry) : Transformer
{
return self::user_function([$entry], 'ceil');
}

final public static function chain(Transformer ...$transformers) : Transformer
{
return new Transformer\ChainTransformer(...$transformers);
Expand All @@ -229,14 +234,14 @@ final public static function convert_name(string $style) : Transformer
return new Transformer\EntryNameStyleConverterTransformer($style);
}

final public static function divide(string $leftEntry, string $rightEntry, string $newEntryName = 'divide') : Transformer
final public static function divide(string $leftEntry, string $rightEntry, string $new_entry_name = 'divide') : Transformer
{
return MathOperationTransformer::divide($leftEntry, $rightEntry, $newEntryName);
return MathOperationTransformer::divide($leftEntry, $rightEntry, $new_entry_name);
}

final public static function divide_by(string $leftEntry, int|float $value, string $newEntryName = 'divide') : Transformer
final public static function divide_by(string $leftEntry, int|float $value, string $new_entry_name = 'divide') : Transformer
{
return MathValueOperationTransformer::divide($leftEntry, $value, $newEntryName);
return MathValueOperationTransformer::divide($leftEntry, $value, $new_entry_name);
}

/**
Expand Down Expand Up @@ -304,9 +309,14 @@ final public static function filter_valid(string $entry, Constraint ...$constrai
return new FilterRowsTransformer(new Opposite(new ValidValue($entry, new ValidValue\SymfonyValidator($constraints))));
}

final public static function group_to_array(string $groupByEntry, string $newEntryName) : Transformer
final public static function floor(string $entry) : Transformer
{
return self::user_function([$entry], 'floor');
}

final public static function group_to_array(string $groupByEntry, string $new_entry_name) : Transformer
{
return new Transformer\GroupToArrayTransformer($groupByEntry, $newEntryName);
return new Transformer\GroupToArrayTransformer($groupByEntry, $new_entry_name);
}

/**
Expand All @@ -315,12 +325,12 @@ final public static function group_to_array(string $groupByEntry, string $newEnt
*
* @throws \Flow\ETL\Exception\InvalidArgumentException
*/
final public static function hash(array $entries, string $algorithm = null, string $newEntryName = 'hash') : Transformer
final public static function hash(array $entries, string $algorithm = null, string $new_entry_name = 'hash') : Transformer
{
return new Transformer\HashTransformer(
$entries,
$algorithm ?? (PHP_VERSION_ID >= 80100 ? 'murmur3f' : 'sha256'),
$newEntryName
$new_entry_name
);
}

Expand All @@ -329,37 +339,42 @@ final public static function keep(string ...$entries) : Transformer
return new KeepEntriesTransformer(...$entries);
}

final public static function modulo(string $leftEntry, string $rightEntry, string $newEntryName = 'modulo') : Transformer
final public static function ltrim(string $entry, string $characters = " \n\r\t\v\x00") : Transformer
{
return MathOperationTransformer::modulo($leftEntry, $rightEntry, $newEntryName);
return self::user_function([$entry], 'ltrim', [$characters]);
}

final public static function modulo_by(string $leftEntry, int|float $value, string $newEntryName = 'modulo') : Transformer
final public static function modulo(string $leftEntry, string $rightEntry, string $new_entry_name = 'modulo') : Transformer
{
return MathValueOperationTransformer::modulo($leftEntry, $value, $newEntryName);
return MathOperationTransformer::modulo($leftEntry, $rightEntry, $new_entry_name);
}

final public static function multiply(string $leftEntry, string $rightEntry, string $newEntryName = 'multiply') : Transformer
final public static function modulo_by(string $leftEntry, int|float $value, string $new_entry_name = 'modulo') : Transformer
{
return MathOperationTransformer::multiply($leftEntry, $rightEntry, $newEntryName);
return MathValueOperationTransformer::modulo($leftEntry, $value, $new_entry_name);
}

final public static function multiply_by(string $leftEntry, int|float $value, string $newEntryName = 'multiply') : Transformer
final public static function multiply(string $leftEntry, string $rightEntry, string $new_entry_name = 'multiply') : Transformer
{
return MathValueOperationTransformer::multiply($leftEntry, $value, $newEntryName);
return MathOperationTransformer::multiply($leftEntry, $rightEntry, $new_entry_name);
}

final public static function multiply_by(string $leftEntry, int|float $value, string $new_entry_name = 'multiply') : Transformer
{
return MathValueOperationTransformer::multiply($leftEntry, $value, $new_entry_name);
}

/**
* @param array<string> $entries
*
* @throws \Flow\ETL\Exception\InvalidArgumentException
*/
final public static function murmur3(array $entries, string $newEntryName = 'hash') : Transformer
final public static function murmur3(array $entries, string $new_entry_name = 'hash') : Transformer
{
return new Transformer\HashTransformer(
$entries,
'murmur3f',
$newEntryName
$new_entry_name
);
}

Expand All @@ -371,14 +386,14 @@ final public static function object_method(string $object_name, string $method,
return new Transformer\ObjectMethodTransformer($object_name, $method, $entry_name, $parameters);
}

final public static function power(string $leftEntry, string $rightEntry, string $newEntryName = 'power') : Transformer
final public static function power(string $leftEntry, string $rightEntry, string $new_entry_name = 'power') : Transformer
{
return MathOperationTransformer::power($leftEntry, $rightEntry, $newEntryName);
return MathOperationTransformer::power($leftEntry, $rightEntry, $new_entry_name);
}

final public static function power_of(string $leftEntry, int|float $value, string $newEntryName = 'power') : Transformer
final public static function power_of(string $leftEntry, int|float $value, string $new_entry_name = 'power') : Transformer
{
return MathValueOperationTransformer::power($leftEntry, $value, $newEntryName);
return MathValueOperationTransformer::power($leftEntry, $value, $new_entry_name);
}

final public static function remove(string ...$entries) : Transformer
Expand All @@ -391,20 +406,35 @@ final public static function rename(string $from, string $to) : Transformer
return new RenameEntriesTransformer(new EntryRename($from, $to));
}

final public static function round(string $entry, int $precision = 0, int $mode = \PHP_ROUND_HALF_UP) : Transformer
{
return self::user_function([$entry], 'round', [$precision, $mode]);
}

final public static function rtrim(string $entry, string $characters = " \n\r\t\v\x00") : Transformer
{
return self::user_function([$entry], 'rtrim', [$characters]);
}

/**
* @param array<string> $entries
*
* @throws \Flow\ETL\Exception\InvalidArgumentException
*/
final public static function sha256(array $entries, string $newEntryName = 'hash') : Transformer
final public static function sha256(array $entries, string $new_entry_name = 'hash') : Transformer
{
return new Transformer\HashTransformer(
$entries,
'sha256',
$newEntryName
$new_entry_name
);
}

final public static function str_pad(string $entry, int $length, string $pad_string = ' ', int $type = STR_PAD_RIGHT) : Transformer
{
return self::user_function([$entry], 'str_pad', [$length, $pad_string, $type]);
}

/**
* @param string[] $string_columns
*/
Expand All @@ -413,29 +443,29 @@ final public static function string_concat(array $string_columns, string $glue =
return new Transformer\StringConcatTransformer($string_columns, $glue, $entry_name);
}

final public static function string_format(string $entryName, string $format) : Transformer
final public static function string_format(string $entry_name, string $format) : Transformer
{
return new Transformer\StringFormatTransformer($entryName, $format);
return new Transformer\StringFormatTransformer($entry_name, $format);
}

final public static function string_lower(string ...$entryNames) : Transformer
final public static function string_lower(string ...$entry_names) : Transformer
{
return StringEntryValueCaseConverterTransformer::lower(...$entryNames);
return StringEntryValueCaseConverterTransformer::lower(...$entry_names);
}

final public static function string_upper(string ...$entryNames) : Transformer
final public static function string_upper(string ...$entry_names) : Transformer
{
return StringEntryValueCaseConverterTransformer::upper(...$entryNames);
return StringEntryValueCaseConverterTransformer::upper(...$entry_names);
}

final public static function subtract(string $leftEntry, string $rightEntry, string $newEntryName = 'subtract') : Transformer
final public static function subtract(string $leftEntry, string $rightEntry, string $new_entry_name = 'subtract') : Transformer
{
return MathOperationTransformer::subtract($leftEntry, $rightEntry, $newEntryName);
return MathOperationTransformer::subtract($leftEntry, $rightEntry, $new_entry_name);
}

final public static function subtract_value(string $leftEntry, int|float $value, string $newEntryName = 'subtract') : Transformer
final public static function subtract_value(string $leftEntry, int|float $value, string $new_entry_name = 'subtract') : Transformer
{
return MathValueOperationTransformer::subtract($leftEntry, $value, $newEntryName);
return MathValueOperationTransformer::subtract($leftEntry, $value, $new_entry_name);
}

final public static function to_array(string ...$entries) : Transformer
Expand Down Expand Up @@ -468,9 +498,9 @@ final public static function to_datetime(array $entries, ?string $timezone = nul
/**
* @param array<string> $entries
*/
final public static function to_datetime_from_string(array $entries, ?string $tz = null, ?string $toTz = null) : Transformer
final public static function to_datetime_from_string(array $entries, ?string $tz = null, ?string $to_tz = null) : Transformer
{
return new CastTransformer(new Transformer\Cast\CastEntries($entries, new StringToDateTimeEntryCaster($tz, $toTz), true));
return new CastTransformer(new Transformer\Cast\CastEntries($entries, new StringToDateTimeEntryCaster($tz, $to_tz), true));
}

final public static function to_integer(string ...$entries) : Transformer
Expand Down Expand Up @@ -506,12 +536,18 @@ final public static function transform_if(Transformer\Condition\RowCondition $co
return new Transformer\ConditionalTransformer($condition, $transformer);
}

final public static function trim(string $entry, string $characters = " \n\r\t\v\x00") : Transformer
{
return self::user_function([$entry], 'trim', [$characters]);
}

/**
* @param array<string> $entries
* @param EntryFactory $entryFactory
* @param array<mixed> $extra_arguments
* @param EntryFactory $entry_factory
*/
final public static function user_function(array $entries, callable $callback, EntryFactory $entryFactory = new NativeEntryFactory()) : Transformer
final public static function user_function(array $entries, callable $callback, array $extra_arguments = [], EntryFactory $entry_factory = new NativeEntryFactory()) : Transformer
{
return new Transformer\CallUserFunctionTransformer($entries, $callback, $entryFactory);
return new Transformer\CallUserFunctionTransformer($entries, $callback, $extra_arguments, $entry_factory);
}
}
8 changes: 6 additions & 2 deletions src/Flow/ETL/Transformer/CallUserFunctionTransformer.php
Expand Up @@ -14,7 +14,7 @@
use Laravel\SerializableClosure\SerializableClosure;

/**
* @implements Transformer<array{entries: array<string>, callback: callable, entry_factory: EntryFactory}>
* @implements Transformer<array{entries: array<string>, callback: callable, extra_arguments: array<mixed>, entry_factory: EntryFactory}>
* @psalm-immutable
*/
final class CallUserFunctionTransformer implements Transformer
Expand All @@ -27,10 +27,12 @@ final class CallUserFunctionTransformer implements Transformer

/**
* @param array<string> $entries
* @param array<mixed> $extraArguments
*/
public function __construct(
private readonly array $entries,
callable $callback,
private readonly array $extraArguments = [],
private readonly EntryFactory $entryFactory = new NativeEntryFactory()
) {
$this->callback = $callback;
Expand All @@ -45,6 +47,7 @@ public function __serialize() : array
return [
'entries' => $this->entries,
'callback' => $this->callback instanceof \Closure ? new SerializableClosure(\Closure::fromCallable($this->callback)) : $this->callback,
'extra_arguments' => $this->extraArguments,
'entry_factory' => $this->entryFactory,
];
}
Expand All @@ -58,6 +61,7 @@ public function __unserialize(array $data) : void
$this->entries = $data['entries'];
/** @psalm-suppress ImpureMethodCall */
$this->callback = $data['callback'] instanceof SerializableClosure ? $data['callback']->getClosure() : $data['callback'];
$this->extraArguments = $data['extra_arguments'];
$this->entryFactory = $data['entry_factory'];
}

Expand All @@ -73,7 +77,7 @@ public function transform(Rows $rows) : Rows
if (\in_array($entry->name(), $this->entries, true)) {
$entry = $this->entryFactory->create(
$entry->name(),
\call_user_func($this->callback, $entry->value())
\call_user_func($this->callback, ...\array_merge([$entry->value()], $this->extraArguments))
);
}

Expand Down

0 comments on commit 6a71356

Please sign in to comment.