Created
August 15, 2017 07:41
-
-
Save podhy/5e610bd91215555a8a633be4e4ba28cc to your computer and use it in GitHub Desktop.
Doctrine CZK money type
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
<?php | |
use Doctrine\DBAL\Platforms\AbstractPlatform; | |
use Doctrine\DBAL\Types\Type; | |
use Money\Currencies\ISOCurrencies; | |
use Money\Formatter\DecimalMoneyFormatter; | |
use Money\Money; | |
use Money\Parser\DecimalMoneyParser; | |
/** | |
* Class CZKCurrencyType | |
* | |
* @package CustomIS\AppBundle\Doctrine\Types | |
*/ | |
class CZKCurrencyType extends Type | |
{ | |
const CZKCURRENCY = 'czkcurrency'; | |
/** | |
* @param array $fieldDeclaration | |
* @param AbstractPlatform $platform | |
* | |
* @return string | |
*/ | |
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) | |
{ | |
return 'numeric(10,2)'; | |
} | |
/** | |
* @param mixed $value | |
* @param AbstractPlatform $platform | |
* | |
* @return Money | |
*/ | |
public function convertToPHPValue($value, AbstractPlatform $platform) | |
{ | |
if ($value !== null) { | |
$moneyParser = new DecimalMoneyParser(new ISOCurrencies()); | |
return $moneyParser->parse($value, 'CZK'); | |
} | |
return null; | |
} | |
/** | |
* @param Money $value | |
* @param AbstractPlatform $platform | |
* | |
* @return mixed | |
*/ | |
public function convertToDatabaseValue($value, AbstractPlatform $platform) | |
{ | |
if ($value === null) { | |
return null; | |
} | |
$moneyFormatter = new DecimalMoneyFormatter(new ISOCurrencies()); | |
return $moneyFormatter->format($value); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return self::CZKCURRENCY; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment