Skip to content

Commit

Permalink
\StdClass serializer. Merging on master
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Feb 14, 2018
1 parent a24c6b5 commit 503d8c4
Show file tree
Hide file tree
Showing 19 changed files with 219 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ This limit is:

## Requirements

* PHP 7.1+
* PHP 5.6+
* MySQL 5.7+
* PostgreSQL 9.5+

Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
"require": {
"php": "^5.6 || ^7.0",
"doctrine/dbal": "^2.6",
"jstayton/miner": "^0.10.0",
"symfony/serializer": "^4.0",
"symfony/property-access": "^4.0"
"symfony/property-access": "^4.0",
"symfony/serializer": "^4.0"
},
"require-dev": {
"doctrine/collections": "^1.5",
Expand Down
62 changes: 1 addition & 61 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/Exceptions/NotAllowedDriverException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\Exceptions;

Expand Down
8 changes: 8 additions & 0 deletions src/Exceptions/NotAllowedModeException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\Exceptions;

Expand Down
8 changes: 8 additions & 0 deletions src/Exceptions/NotIterableDataException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\Exceptions;

Expand Down
29 changes: 19 additions & 10 deletions src/Importer.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter;

use DbImporter\Exceptions\NotAllowedDriverException;
use DbImporter\Exceptions\NotAllowedModeException;
use DbImporter\Exceptions\NotIterableDataException;
use DbImporter\Normalizer\StdClassNormalizer;
use DbImporter\QueryBuilder\Contracts\QueryBuilderInterface;
use DbImporter\QueryBuilder\MySqlQueryBuilder;
use DbImporter\QueryBuilder\PgSqlQueryBuilder;
use DbImporter\QueryBuilder\SqliteQueryBuilder;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Statement;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Yaml\Yaml;

class Importer
{
Expand Down Expand Up @@ -97,7 +101,7 @@ private function __construct(
$this->driver = $driver;
$this->table = $table;
$this->mapping = $mapping;
$this->data = $this->serialize($data);
$this->data = $this->normalize($data);
$this->ignoreErrors = $skipDuplicates;
$this->mode = $mode;
}
Expand Down Expand Up @@ -139,13 +143,18 @@ private function checkMode($mode)
* @return array
* @throws NotIterableDataException
*/
private function serialize($data)
private function normalize($data)
{
if (false === is_iterable($data)) {
throw new NotIterableDataException('Data is not iterable');
}

return (new Serializer([new ObjectNormalizer()]))->normalize($data, null);
$serializer = new Serializer([
new StdClassNormalizer(),
new ObjectNormalizer()
]);

return $serializer->normalize($data);
}

/**
Expand Down Expand Up @@ -216,7 +225,7 @@ public function execute()
return $this->executeSingleInsertQueries();
}

return $this->executeMultipleInsertQuery();
return $this->executeMultipleInsertQueries();
}

/**
Expand All @@ -243,7 +252,7 @@ private function executeSingleInsertQueries()
/**
* @return bool
*/
private function executeMultipleInsertQuery()
private function executeMultipleInsertQueries()
{
$queries = $this->getQueries();

Expand Down
37 changes: 37 additions & 0 deletions src/Normalizer/StdClassNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\Normalizer;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class StdClassNormalizer implements NormalizerInterface
{
/**
* @param object $object
* @param null $format
* @param array $context
* @return array
*/
public function normalize($object, $format = null, array $context = array())
{
return get_object_vars($object);
}

/**
* @param mixed $data
* @param null $format
* @return bool
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof \StdClass;
}
}
9 changes: 8 additions & 1 deletion src/QueryBuilder/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\QueryBuilder;

use DbImporter\Collections\DataCollection;
use DbImporter\QueryBuilder\Contracts\QueryBuilderInterface;
use Doctrine\Common\Collections\ArrayCollection;

Expand Down
8 changes: 8 additions & 0 deletions src/QueryBuilder/Contracts/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\QueryBuilder\Contracts;

Expand Down
8 changes: 8 additions & 0 deletions src/QueryBuilder/MySqlQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\QueryBuilder;

Expand Down
8 changes: 8 additions & 0 deletions src/QueryBuilder/PgSqlQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\QueryBuilder;

Expand Down
8 changes: 8 additions & 0 deletions src/QueryBuilder/SqliteQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\QueryBuilder;

Expand Down
19 changes: 19 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\Tests;

Expand Down Expand Up @@ -124,6 +132,17 @@ protected function createPhotosCollection($limit)
return new ArrayCollection($array);
}

/**
* @param $limit
* @return array
*/
public function createPhotosStdClassArray($limit)
{
$url = 'https://jsonplaceholder.typicode.com/photos';

return array_slice(json_decode(file_get_contents($url)), 0, $limit);
}

/**
* @param $url
* @return \Doctrine\DBAL\Connection
Expand Down
8 changes: 8 additions & 0 deletions tests/Entity/Photo.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the DbImporter package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DbImporter\Tests\Entity;

Expand Down
Loading

0 comments on commit 503d8c4

Please sign in to comment.