Skip to content

Commit

Permalink
more transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
fab2s committed Nov 9, 2022
1 parent ac2579c commit edba216
Show file tree
Hide file tree
Showing 13 changed files with 668 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Transformers/Arrays/ArrayKeyTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\YaEtl\Transformers\Arrays;

use fab2s\NodalFlow\NodalFlowException;
use fab2s\YaEtl\Transformers\TransformerAbstract;

class ArrayKeyTransformer extends TransformerAbstract
{
/**
* Any callable with one argument which returns something
*
* @var callable
*/
protected $mapper;

/**
* @param callable $mapper
*
* @throws NodalFlowException
*/
public function __construct(callable $mapper)
{
$this->mapper = $mapper;
parent::__construct();
}

/**
* Execute the array_map call
*
* @param array $param
*
* @return array
*/
public function exec($param = null)
{
return array_combine(array_map($this->mapper, array_keys($param)), array_values($param));
}
}
59 changes: 59 additions & 0 deletions src/Transformers/Arrays/ArrayMapRecursiveTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\YaEtl\Transformers\Arrays;

use fab2s\NodalFlow\NodalFlowException;
use fab2s\YaEtl\Transformers\TransformerAbstract;

/**
* Class ArrayMapRecursiveTransformer
*/
class ArrayMapRecursiveTransformer extends TransformerAbstract
{
/**
* Any callable with one argument which returns something
*
* @var callable
*/
protected $mapper;

/**
* @param callable $mapper
*
* @throws NodalFlowException
*/
public function __construct(callable $mapper)
{
$this->mapper = $mapper;
parent::__construct();
}

/**
* Execute the arrayMapRecursive call
*
* @param array $record
*
* @return array
*/
public function exec($record = null)
{
return $this->arrayMapRecursive($record);
}

protected function arrayMapRecursive(array $record): array
{
$out = [];
foreach ($record as $key => $value) {
$out[$key] = is_array($value) ? $this->arrayMapRecursive($value) : call_user_func($this->mapper, $value);
}

return $out;
}
}
22 changes: 22 additions & 0 deletions src/Transformers/Arrays/CharsetRecursiveTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\YaEtl\Transformers\Arrays;

use fab2s\Strings\Strings;

class CharsetRecursiveTransformer extends ArrayMapRecursiveTransformer
{
use CharsetTransformerTrait;

public function __construct(?string $from = null, string $to = Strings::ENCODING)
{
parent::__construct($this->getConvertClosure($from, $to));
}
}
22 changes: 22 additions & 0 deletions src/Transformers/Arrays/CharsetTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\YaEtl\Transformers\Arrays;

use fab2s\Strings\Strings;

class CharsetTransformer extends ArrayMapTransformer
{
use CharsetTransformerTrait;

public function __construct(?string $from = null, string $to = Strings::ENCODING)
{
parent::__construct($this->getConvertClosure($from, $to));
}
}
22 changes: 22 additions & 0 deletions src/Transformers/Arrays/CharsetTransformerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\YaEtl\Transformers\Arrays;

use fab2s\Strings\Strings;

trait CharsetTransformerTrait
{
public function getConvertClosure(?string $from = null, string $to = Strings::ENCODING): \Closure
{
return function ($value) use ($from, $to) {
return is_string($value) ? Strings::convert($value, $from, $to) : $value;
};
}
}
97 changes: 97 additions & 0 deletions src/Transformers/Arrays/DateFormatTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\YaEtl\Transformers\Arrays;

use DateTimeImmutable;
use DateTimeZone;
use fab2s\NodalFlow\NodalFlowException;
use fab2s\YaEtl\Transformers\TransformerAbstract;

class DateFormatTransformer extends TransformerAbstract
{
const DEFAULT_TIMEZONE = 'UTC';

/**
* @var array<string,array<string, string|null>>
* 'key_name' => 'date_format' // Y-m-d => DateTimeImmutable instance
* or:
* 'key_name' => ['from' => 'date_format', 'to' => 'date_format|null'] // to defaults to from
*/
protected $setUp = [];

/**
* @var DateTimeZone
*/
protected $dateTimeZoneFrom;

/**
* @var DateTimeZone
*/
protected $dateTimeZoneTo;

/**
* @var array<string,string|array<string,string>>
* 'key_name' => 'date_format' // Y-m-d => DateTimeImmutable instance
* or:
* 'key_name' => ['from' => 'date_format', 'to' => 'date_format'] // to defaults to from
*
* @param DateTimeZone|null $dateTimeZoneFrom
* @param DateTimeZone|null $dateTimeZoneTo
*
* @throws NodalFlowException
*/
public function __construct(array $setup, DateTimeZone $dateTimeZoneFrom = null, DateTimeZone $dateTimeZoneTo = null)
{
parent::__construct();

$this->initSetup($setup);
$this->dateTimeZoneFrom = $dateTimeZoneFrom ?: new DateTimeZone(static::DEFAULT_TIMEZONE);
$this->dateTimeZoneTo = $dateTimeZoneTo ?: new DateTimeZone(static::DEFAULT_TIMEZONE);
}

public function exec($param = null)
{
foreach ($this->setUp as $key => $dateFormat) {
$param[$key] = DateTimeImmutable::createFromFormat($dateFormat['from'], $param[$key], $this->dateTimeZoneFrom)
->setTimezone($this->dateTimeZoneTo);

if ($dateFormat['to']) {
$param[$key] = $param[$key]->format($dateFormat['to']);
}
}

return $param;
}

/**
* @param array<string,string|array<string,string>> $setup
*
* @return $this
*/
protected function initSetup(array $setup): self
{
foreach ($setup as $key => $dateFormat) {
$formatTo = null;
if (is_array($dateFormat)) {
$formatFrom = $dateFormat['from'];
$formatTo = $dateFormat['to'] ?? $formatFrom;
} else {
$formatFrom = $dateFormat;
}

$this->setUp[$key] = [
'from' => $formatFrom,
'to' => $formatTo,
];
}

return $this;
}
}
31 changes: 31 additions & 0 deletions src/Transformers/Arrays/SetValuesTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\YaEtl\Transformers\Arrays;

use fab2s\YaEtl\Transformers\TransformerAbstract;

class SetValuesTransformer extends TransformerAbstract
{
/**
* @var array<string,mixed>
*/
protected $setUp = [];

public function __construct(array $setup)
{
parent::__construct();
$this->setUp = $setup;
}

public function exec($param = null)
{
return \array_replace($param, $this->setUp);
}
}
52 changes: 52 additions & 0 deletions tests/Lib/Arrays/ArrayKeyTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\Tests\Lib\Arrays;

use fab2s\NodalFlow\NodalFlowException;
use fab2s\Tests\Lib\TestBase;
use fab2s\YaEtl\Transformers\Arrays\ArrayKeyTransformer;

class ArrayKeyTransformerTest extends TestBase
{
/**
* @dataProvider arrayKeyProvider
*
* @param callable $callable
* @param array $data
* @param array $expected
*
* @throws NodalFlowException
*/
public function testArrayKeyTransformer(callable $callable, array $data, array $expected)
{
$transformer = new ArrayKeyTransformer($callable);

$this->assertSame($expected, $transformer->exec($data));
}

public function arrayKeyProvider(): array
{
return [
[
'strtolower',
[
'Key1' => 'Val1',
'Key2' => 'Val2',
'Key3' => 'Val3',
],
[
'key1' => 'Val1',
'key2' => 'Val2',
'key3' => 'Val3',
],
],
];
}
}
Loading

0 comments on commit edba216

Please sign in to comment.