Skip to content

Commit

Permalink
Merge da949c7 into f4dc94c
Browse files Browse the repository at this point in the history
  • Loading branch information
courtney-miles committed Aug 15, 2018
2 parents f4dc94c + da949c7 commit 1d400f2
Show file tree
Hide file tree
Showing 23 changed files with 679 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/Transform/AbstractTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

abstract class AbstractTransformer implements TransformerInterface
{
public final function __construct()
final public function __construct()
{
}

public function isString($value)
{
return is_scalar($value) || (\is_object($value) && method_exists($value, '__toString'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace MilesAsylum\Slurp\Transform;


abstract class Transformation
abstract class Change
{
/**
* @return string
Expand Down
46 changes: 46 additions & 0 deletions src/Transform/DateTimeFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Author: Courtney Miles
* Date: 15/08/18
* Time: 11:22 AM
*/

namespace MilesAsylum\Slurp\Transform;


class DateTimeFormat extends Change
{
private $formatFrom;

private $formatTo;

public function __construct($formatFrom, $formatTo)
{
$this->formatFrom = $formatFrom;
$this->formatTo = $formatTo;
}

/**
* @return string
*/
public function getFormatFrom()
{
return $this->formatFrom;
}

/**
* @return string
*/
public function getFormatTo()
{
return $this->formatTo;
}

/**
* @return string
*/
public function transformedBy()
{
return DateTimeFormatTransformer::class;
}
}
38 changes: 38 additions & 0 deletions src/Transform/DateTimeFormatTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Author: Courtney Miles
* Date: 15/08/18
* Time: 11:24 AM
*/

namespace MilesAsylum\Slurp\Transform;

use MilesAsylum\Slurp\Transform\Exception\UnexpectedTypeException;

class DateTimeFormatTransformer extends AbstractTransformer
{
public function transform($value, Change $transformation)
{
if (!$transformation instanceof DateTimeFormat) {
throw new UnexpectedTypeException($transformation, DateTimeFormat::class);
}

if (!$this->isString($value)) {
throw new UnexpectedTypeException($value, 'string');
}

$value = \DateTime::createFromFormat($transformation->getFormatFrom(), $value);

if ($value === false) {
throw new \InvalidArgumentException(
sprintf(
'The date value %s was not able to be converted from the format %s',
$value,
$transformation->getFormatFrom()
)
);
}

return $value->format($transformation->getFormatTo());
}
}
1 change: 0 additions & 1 deletion src/Transform/Exception/UnexpectedTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace MilesAsylum\Slurp\Transform\Exception;


class UnexpectedTypeException extends \InvalidArgumentException implements ExceptionInterface
{
public function __construct($value, string $expectedType)
Expand Down
31 changes: 31 additions & 0 deletions src/Transform/StrCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Author: Courtney Miles
* Date: 13/08/18
* Time: 10:52 PM
*/

namespace MilesAsylum\Slurp\Transform;

class StrCase extends Change
{
private $caseChange;

const CASE_UPPER = 'upper';
const CASE_LOWER = 'lower';

public function __construct($caseChange)
{
$this->caseChange = $caseChange;
}

public function transformedBy()
{
return StrCaseTransformer::class;
}

public function getCaseChange()
{
return $this->caseChange;
}
}
35 changes: 35 additions & 0 deletions src/Transform/StrCaseTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Author: Courtney Miles
* Date: 13/08/18
* Time: 10:53 PM
*/

namespace MilesAsylum\Slurp\Transform;

use MilesAsylum\Slurp\Transform\Exception\UnexpectedTypeException;

class StrCaseTransformer extends AbstractTransformer
{
public function transform($value, Change $transformation)
{
if (!$transformation instanceof StrCase) {
throw new UnexpectedTypeException($transformation, StrCase::class);
}

if (!$this->isString($value)) {
throw new UnexpectedTypeException($value, 'string');
}

switch ($transformation->getCaseChange()) {
case StrCase::CASE_UPPER:
$value = strtoupper($value);
break;
case StrCase::CASE_LOWER:
$value = strtolower($value);
break;
}

return $value;
}
}
4 changes: 2 additions & 2 deletions src/Transform/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Transformer
{
/**
* @var Transformation[][]
* @var Change[][]
*/
protected $columnTranslators = [];

Expand All @@ -33,7 +33,7 @@ public function __construct(TransformerLoader $loader, array $columns)

/**
* @param $columnName
* @param Transformation|Transformation[] $transformations
* @param Change|Change[] $transformations
*/
public function addColumnTransformations($columnName, $transformations)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Transform/TransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface TransformerInterface
{
public function transform($value, Transformation $transformation);
public function transform($value, Change $transformation);
}
4 changes: 2 additions & 2 deletions src/Transform/TransformerLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class TransformerLoader
private $loadedTransformers = [];

/**
* @param Transformation $transformation
* @param Change $transformation
* @return TransformerInterface
*/
public function loadTransformer(Transformation $transformation)
public function loadTransformer(Change $transformation)
{
if (!isset($this->loadedTransformers[$transformation->transformedBy()])) {
$transformedBy = $transformation->transformedBy();
Expand Down
72 changes: 72 additions & 0 deletions src/Transform/Trim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Author: Courtney Miles
* Date: 15/08/18
* Time: 10:45 AM
*/

namespace MilesAsylum\Slurp\Transform;


class Trim extends Change
{
/**
* @var string
*/
private $chars;

/**
* @var bool
*/
private $fromLeft;

/**
* @var bool
*/
private $fromRight;

/**
* Trim constructor.
* @param bool $fromLeft
* @param bool $fromRight
* @param string $chars
*/
public function __construct($fromLeft = true, $fromRight = true, $chars = " \t\n\r\0\x0B")
{
$this->chars = $chars;
$this->fromLeft = $fromLeft;
$this->fromRight = $fromRight;
}

/**
* @return string
*/
public function getChars(): string
{
return $this->chars;
}

/**
* @return bool
*/
public function fromLeft(): bool
{
return $this->fromLeft;
}

/**
* @return bool
*/
public function fromRight(): bool
{
return $this->fromRight;
}

/**
* @return string
*/
public function transformedBy()
{
return TrimTransformer::class;
}
}
34 changes: 34 additions & 0 deletions src/Transform/TrimTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Author: Courtney Miles
* Date: 15/08/18
* Time: 10:45 AM
*/

namespace MilesAsylum\Slurp\Transform;

use MilesAsylum\Slurp\Transform\Exception\UnexpectedTypeException;

class TrimTransformer extends AbstractTransformer
{
public function transform($value, Change $transformation)
{
if (!$transformation instanceof Trim) {
throw new UnexpectedTypeException($transformation, Trim::class);
}

if (!$this->isString($value)) {
throw new UnexpectedTypeException($value, 'string');
}

if ($transformation->fromLeft() && $transformation->fromRight()) {
$value = trim($value, $transformation->getChars());
} elseif ($transformation->fromLeft()) {
$value = ltrim($value, $transformation->getChars());
} elseif ($transformation->fromRight()) {
$value = rtrim($value, $transformation->getChars());
}

return $value;
}
}
2 changes: 1 addition & 1 deletion src/Validate/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ protected function getCellValue($row, $column)
{
return $row[$this->colPositions[$column]];
}
}
}
39 changes: 39 additions & 0 deletions tests/Slurp/Transform/DateTimeFormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Author: Courtney Miles
* Date: 15/08/18
* Time: 11:27 AM
*/

namespace MilesAsylum\Slurp\Tests\Slurp\Transform;

use MilesAsylum\Slurp\Transform\DateTimeFormat;
use MilesAsylum\Slurp\Transform\DateTimeFormatTransformer;
use PHPUnit\Framework\TestCase;

class DateTimeFormatTest extends TestCase
{
public function testGetFormatFrom()
{
$this->assertSame(
'Y-m-d',
(new DateTimeFormat('Y-m-d', 'Y'))->getFormatFrom()
);
}

public function testGetFormatTo()
{
$this->assertSame(
'Y-m-d',
(new DateTimeFormat('Y', 'Y-m-d'))->getFormatTo()
);
}

public function testTransformedBy()
{
$this->assertSame(
DateTimeFormatTransformer::class,
(new DateTimeFormat('Y', 'Y'))->transformedBy()
);
}
}

0 comments on commit 1d400f2

Please sign in to comment.