Skip to content

Commit

Permalink
Merge pull request #20 from prolic/mongodb
Browse files Browse the repository at this point in the history
Doctrine ODM MongoDB Mapping - rebased and cleaned up
  • Loading branch information
danizord committed May 11, 2015
2 parents 54c30e7 + ff3442e commit a48d84d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 22 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ language: php
php:
- 5.5
- 5.6
- 7

install:
- composer install --prefer-source
before_script:
- echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

script:
- composer install --prefer-source
- vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml
- vendor/bin/php-cs-fixer fix -v --dry-run ./test

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require-dev": {
"phpunit/phpunit": "~4.4",
"fabpot/php-cs-fixer": "dev-master",
"doctrine/dbal": "~2.5"
"doctrine/dbal": "~2.5",
"doctrine/mongodb-odm": "1.0.*@dev"
},
"require": {
"php": ">=5.5",
Expand All @@ -34,7 +35,8 @@
"mathiasverraes/money": "1.2.*"
},
"suggest": {
"doctrine/dbal": "If you want to use Currency DBAL type."
"doctrine/dbal": "2.5.*",
"doctrine/mongodb-odm": "1.0.*@dev"
},
"autoload": {
"psr-4": {
Expand Down
35 changes: 25 additions & 10 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Doctrine\Common\Persistence\Mapping\Driver\PHPDriver;
use ZFBrasil\DoctrineMoneyModule\DBAL\Types\CurrencyType;
use ZFBrasil\DoctrineMoneyModule\Form\Factory\MoneyFieldsetFactory;
use ZFBrasil\DoctrineMoneyModule\Form\MoneyFieldset;
use ZFBrasil\DoctrineMoneyModule\View\Helper\MoneyFormat;
Expand Down Expand Up @@ -29,16 +28,39 @@
],
'doctrine' => [
'driver' => [
'money_driver' => [
'money_driver_orm' => [
'class' => PHPDriver::class,
'paths' => [
__DIR__ . '/../mapping/PHPDriver/orm'
]
],
'money_driver_odm_mongodb' => [
'class' => PHPDriver::class,
'paths' => [
__DIR__ . '/../mapping/PHPDriver/odm-mongodb'
]
],
'odm_default' => [
'drivers' => [
'Money\Money' => 'money_driver_odm_mongodb'
],
],
'orm_default' => [
'drivers' => [
'Money\Money' => 'money_driver'
'Money\Money' => 'money_driver_orm'
],
]
],
'configuration' => [
'odm_default' => [
'types' => [
'currency' => \ZFBrasil\DoctrineMoneyModule\ODM\MongoDB\Types\CurrencyType::class
]
],
'orm_default' => [
'types' => [
'currency' => \ZFBrasil\DoctrineMoneyModule\DBAL\Types\CurrencyType::class
]
],
],
'connection' => [
Expand All @@ -48,13 +70,6 @@
],
],
],
'configuration' => [
'orm_default' => [
'types' => [
'currency' => CurrencyType::class
]
]
]
],
'money' => [
'currencies' => require __DIR__ . '/currencies.config.php'
Expand Down
15 changes: 15 additions & 0 deletions mapping/PHPDriver/odm-mongodb/Money.Money.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$metadata->isEmbeddedDocument = true;

$metadata->mapField(array(
'fieldName' => 'amount',
'type' => 'integer',
'nullable' => true
));

$metadata->mapField(array(
'fieldName' => 'currency',
'type' => 'currency',
'nullable' => true
));
8 changes: 1 addition & 7 deletions src/DBAL/Types/CurrencyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
return $value;
}

$val = new Currency($value);

if (!$val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'string');
}

return $val;
return new Currency($value);
}
}
25 changes: 25 additions & 0 deletions src/ODM/MongoDB/Types/CurrencyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ZFBrasil\DoctrineMoneyModule\ODM\MongoDB\Types;

use Doctrine\ODM\MongoDB\Types\StringType;
use Money\Currency;

class CurrencyType extends StringType
{
const NAME = 'currency';

public function getName()
{
return self::NAME;
}

public function convertToPHPValue($value)
{
if ($value === null || $value instanceof Currency) {
return $value;
}

return new Currency($value);
}
}

0 comments on commit a48d84d

Please sign in to comment.