Skip to content
2 changes: 1 addition & 1 deletion src/CastType/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setModel(CastType $model): CastTypeInterface
public function cast($originalValue)
{
if (!$this->model->getFormat()) {
throw new \InvalidArgumentException(__('The format must be provided!'));
throw new \InvalidArgumentException('The format must be provided!');
}
$date = new \DateTime($originalValue);
return $date->format($this->model->getFormat());
Expand Down
40 changes: 40 additions & 0 deletions src/Mutator/Add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Data Mapper (https://github.com/mozahran/data-mapper)
* Copyright 2021, Mohamed Zahran. (https://www.linkedin.com/in/mo-zahran/)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Mohamed Zahran (https://www.linkedin.com/in/mo-zahran/)
* @link https://github.com/mozahran/data-mapper
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Zahran\Mapper\Mutator;

use Zahran\Mapper\Contract\Mutator as MutatorInterface;
use Zahran\Mapper\Model\Mutator;

class Add implements MutatorInterface
{
/**
* @var Mutator
*/
protected $model;

public function setModel(Mutator $model): MutatorInterface
{
$this->model = $model;
return $this;
}

public function apply($originalValue, array $arguments = [])
{
$newValue = $originalValue;
for($i=0; $i < count($arguments); $i++) {
$newValue += $arguments[$i];
}
return $newValue;
}
}
40 changes: 40 additions & 0 deletions src/Mutator/BaseConvert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Data Mapper (https://github.com/mozahran/data-mapper)
* Copyright 2021, Mohamed Zahran. (https://www.linkedin.com/in/mo-zahran/)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Mohamed Zahran (https://www.linkedin.com/in/mo-zahran/)
* @link https://github.com/mozahran/data-mapper
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Zahran\Mapper\Mutator;

use Zahran\Mapper\Contract\Mutator as MutatorInterface;
use Zahran\Mapper\Model\Mutator;

class BaseConvert implements MutatorInterface
{
/**
* @var Mutator
*/
protected $model;

protected $defaultToBase = 10;

public function setModel(Mutator $model): MutatorInterface
{
$this->model = $model;
return $this;
}

public function apply($originalValue, array $arguments = [])
{
$fromBase = (int) $arguments[0];
$toBase = ( isset($arguments[1]) && (int) $arguments[1] > 0 ) ? (int) $arguments[1]:$this->defaultToBase;
return base_convert($originalValue,$fromBase,$toBase);
}
}
40 changes: 40 additions & 0 deletions src/Mutator/Divide.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Data Mapper (https://github.com/mozahran/data-mapper)
* Copyright 2021, Mohamed Zahran. (https://www.linkedin.com/in/mo-zahran/)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Mohamed Zahran (https://www.linkedin.com/in/mo-zahran/)
* @link https://github.com/mozahran/data-mapper
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Zahran\Mapper\Mutator;

use Zahran\Mapper\Contract\Mutator as MutatorInterface;
use Zahran\Mapper\Model\Mutator;

class Divide implements MutatorInterface
{
/**
* @var Mutator
*/
protected $model;

public function setModel(Mutator $model): MutatorInterface
{
$this->model = $model;
return $this;
}

public function apply($originalValue, array $arguments = [])
{
$newValue = $originalValue;
for($i=0; $i < count($arguments); $i++) {
$newValue /= $arguments[$i];
}
return $newValue;
}
}
40 changes: 40 additions & 0 deletions src/Mutator/Exponential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Data Mapper (https://github.com/mozahran/data-mapper)
* Copyright 2021, Mohamed Zahran. (https://www.linkedin.com/in/mo-zahran/)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Mohamed Zahran (https://www.linkedin.com/in/mo-zahran/)
* @link https://github.com/mozahran/data-mapper
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Zahran\Mapper\Mutator;

use Zahran\Mapper\Contract\Mutator as MutatorInterface;
use Zahran\Mapper\Model\Mutator;

class Exponential implements MutatorInterface
{
/**
* @var Mutator
*/
protected $model;

public function setModel(Mutator $model): MutatorInterface
{
$this->model = $model;
return $this;
}

public function apply($originalValue, array $arguments = [])
{
$newValue = $originalValue;
for($i=0; $i < count($arguments); $i++) {
$newValue **= $arguments[$i];
}
return $newValue;
}
}
36 changes: 36 additions & 0 deletions src/Mutator/Modulo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Data Mapper (https://github.com/mozahran/data-mapper)
* Copyright 2021, Mohamed Zahran. (https://www.linkedin.com/in/mo-zahran/)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Mohamed Zahran (https://www.linkedin.com/in/mo-zahran/)
* @link https://github.com/mozahran/data-mapper
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Zahran\Mapper\Mutator;

use Zahran\Mapper\Contract\Mutator as MutatorInterface;
use Zahran\Mapper\Model\Mutator;

class Modulo implements MutatorInterface
{
/**
* @var Mutator
*/
protected $model;

public function setModel(Mutator $model): MutatorInterface
{
$this->model = $model;
return $this;
}

public function apply($originalValue, array $arguments = [])
{
return fmod(floatval($originalValue),floatval($arguments[0]));
}
}
40 changes: 40 additions & 0 deletions src/Mutator/Subtract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Data Mapper (https://github.com/mozahran/data-mapper)
* Copyright 2021, Mohamed Zahran. (https://www.linkedin.com/in/mo-zahran/)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Mohamed Zahran (https://www.linkedin.com/in/mo-zahran/)
* @link https://github.com/mozahran/data-mapper
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Zahran\Mapper\Mutator;

use Zahran\Mapper\Contract\Mutator as MutatorInterface;
use Zahran\Mapper\Model\Mutator;

class Subtract implements MutatorInterface
{
/**
* @var Mutator
*/
protected $model;

public function setModel(Mutator $model): MutatorInterface
{
$this->model = $model;
return $this;
}

public function apply($originalValue, array $arguments = [])
{
$newValue = $originalValue;
for($i=0; $i < count($arguments); $i++) {
$newValue -= $arguments[$i];
}
return $newValue;
}
}
52 changes: 52 additions & 0 deletions tests/CastTypes/BooleanCastTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php


namespace CastTypes;


use PHPUnit\Framework\TestCase;
use Zahran\Mapper\CastType\Boolean;
use Zahran\Mapper\Model\CastType;

class BooleanCastTypeTest extends TestCase
{
protected $castType;
function setUp(): void
{
parent::setUp();
$this->castType = new Boolean;
$this->castType->setModel(new CastType([
CastType::ATTRIBUTE_TYPE => "boolean"
]));
}

/** @test */
function it_can_cast_numerals_as_a_boolean()
{
$originalNumeral = 1;
$castTo = $this->castType->cast($originalNumeral);
$this->assertSame(boolval($originalNumeral),$castTo,"Converts numerals to booleans");
$this->assertNotSame($originalNumeral,$castTo,"Original value casting should have changed");
$this->assertTrue( $castTo ,"Original value should now equate to true");
} //EOF it_can_cast_numerals_as_a_boolean

/** @test */
function it_can_cast_strings_as_a_boolean()
{
$originalNumeral = "1";
$castTo = $this->castType->cast($originalNumeral);
$this->assertSame(boolval($originalNumeral),$castTo,"Converts strings to booleans");
$this->assertNotSame($originalNumeral,$castTo,"Original value casting should have changed");
$this->assertTrue( $castTo ,"Original value should now equate to true");
} //EOF it_can_cast_strings_as_a_boolean

/** @test */
function it_keeps_booleans_as_booleans()
{
$originalNumeral = true;
$castTo = $this->castType->cast($originalNumeral);
$this->assertSame($originalNumeral,$castTo,"Original value casting should match the output");
$this->assertTrue( $castTo ,"Original value should still equate to true");
} //EOF it_keeps_booleans_as_booleans

}
73 changes: 73 additions & 0 deletions tests/CastTypes/DateCastTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php


namespace CastTypes;


use PHPUnit\Framework\TestCase;
use Zahran\Mapper\CastType\Date;
use Zahran\Mapper\Model\CastType;

class DateCastTypeTest extends TestCase
{
const ATTRIBUTE_TYPE = "date";
protected $castType;

function setUp(): void
{
parent::setUp();
$this->castType = new Date;

}

/** @test */
function it_can_format_date_to_string_supplied()
{
$originalValue = "2021-03-21 09:34:15";
$format = "d/m/Y H:i";

$this->castType->setModel(new CastType([
CastType::ATTRIBUTE_TYPE => self::ATTRIBUTE_TYPE,
CastType::ATTRIBUTE_FORMAT => $format,
]));

$reformattedDate = $this->castType->cast($originalValue);

$this->assertSame( ( new \DateTime($originalValue))->format($format), $reformattedDate, "Original value should now be in new date format");
$this->assertNotSame($originalValue,$reformattedDate,"Original value should have changed");

} //EOF it_can_format_date_to_string_supplied

/** @test */
function it_can_format_date_to_constant_supplied()
{
$originalValue = "2021-03-21 09:34:15";
$format = \DateTimeInterface::ISO8601;

$this->castType->setModel(new CastType([
CastType::ATTRIBUTE_TYPE => self::ATTRIBUTE_TYPE,
CastType::ATTRIBUTE_FORMAT => $format,
]));

$reformattedDate = $this->castType->cast($originalValue);

$this->assertSame( ( new \DateTime($originalValue))->format($format), $reformattedDate, "Original value should now be in new date format");
$this->assertNotSame($originalValue,$reformattedDate,"Original value should have changed");

} //EOF it_can_format_date_to_constant_supplied

/** @test */
function it_throws_exception_if_format_missing()
{
$originalValue = "2021-03-21 09:34:15";

$this->expectExceptionObject(new \InvalidArgumentException('The format must be provided!'));

$this->castType->setModel(new CastType([
CastType::ATTRIBUTE_TYPE => self::ATTRIBUTE_TYPE,
]));

$this->castType->cast($originalValue);

} //EOF it_throws_exception_if_format_missing
}
Loading