-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
668 additions
and
0 deletions.
There are no files selected for viewing
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,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)); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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)); | ||
} | ||
} |
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,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)); | ||
} | ||
} |
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,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; | ||
}; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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', | ||
], | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.