-
Notifications
You must be signed in to change notification settings - Fork 1
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
23 changed files
with
679 additions
and
18 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
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,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; | ||
} | ||
} |
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,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()); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -65,4 +65,4 @@ protected function getCellValue($row, $column) | |
{ | ||
return $row[$this->colPositions[$column]]; | ||
} | ||
} | ||
} |
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,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() | ||
); | ||
} | ||
} |
Oops, something went wrong.