Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,55 +1,4 @@
/*.pnproj
/.idea
.htaccess
atlassian-ide-plugin.xml

### Symfony template
# Cache and logs (Symfony2)
/app/cache/*
/app/logs/*
!app/cache/.gitkeep
!app/logs/.gitkeep

# Cache and logs (Symfony3)
/var/*
!/var/cache
/var/cache/*
!var/cache/.gitkeep
!/var/logs
/var/logs/*
!var/logs/.gitkeep
!/var/sessions
/var/sessions/*
!var/sessions/.gitkeep
var/SymfonyRequirements.php

# Parameters
/app/config/parameters.yml
/app/config/parameters.local.yml
/app/config/parameters.ini

# Managed by Composer
/app/bootstrap.php.cache
/var/bootstrap.php.cache
/bin/*
!bin/console
bin/symfony_requirements
/vendor/

# Assets and user uploads
/web/bundles/
/web/uploads/

# PHPUnit
/app/phpunit.xml

# Build data
/build/
/target/

# Any PHARs
*.phar

Thumbs.db

composer.lock
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ build:
tests:
override:
-
command: phpunit --coverage-clover=build/clover.xml
command: JMS_BUNDLE=1 DOCTRINE_BUNDLE=1 phpunit --coverage-clover=build/clover.xml
coverage:
file: build/clover.xml
format: php-clover
45 changes: 31 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
language: php

php:
- 5.5
- 5.6
- 7
- nightly
- hhvm
- 7.1
- 7
- 5.6
- 5.5

## Run on container environment
sudo: false

## Cache composer bits
cache:
directories:
- $HOME/.composer/cache

env:
- PACKAGES='symfony/symfony=2.7.*'
- PACKAGES='symfony/symfony=2.8.*'
- PACKAGES='symfony/symfony=3.1.*'
- PACKAGES='symfony/symfony=~3.4' STABILITY=dev
- PACKAGES='symfony/symfony=3.3.*'
- PACKAGES='symfony/symfony=3.2.*'
- PACKAGES='symfony/symfony=~3.3@dev'
- PACKAGES='symfony/symfony=2.8.*'
- PACKAGES='symfony/symfony=2.7.*'

matrix:
fast_finish: true
include:
- php: 5.5
env: PACKAGES='symfony/symfony=2.7.*' deps='low'
- php: 7.1
env: PACKAGES='symfony/symfony=~4.0' STABILITY=dev
- php: nightly
env: PACKAGES='symfony/symfony=~4.0' STABILITY=dev

allow_failures:
- php: hhvm
- php: nightly
- env: PACKAGES='symfony/symfony=~4.0' STABILITY=dev

before_install:
- travis_retry composer self-update

install:
- composer require --no-update ${PACKAGES}
- if [ "$deps" = "no"] || [ -z "$deps" ]; then composer --prefer-source install; fi;
- if [ "$deps" = "low" ]; then composer --prefer-source --prefer-lowest --prefer-stable update; composer --prefer-source install; fi;
- if [ "$STABILITY" = "dev" ]; then composer config minimum-stability dev; fi;
- composer --prefer-source install

script:
- rm -rf build
- mkdir -p build
- vendor/bin/phpunit --colors -c phpunit.xml
- vendor/bin/phpunit --colors -c phpunit.xml --testsuite unit
- vendor/bin/phpunit --colors -c phpunit.xml --testsuite integration
- JMS_BUNDLE=1 vendor/bin/phpunit --colors -c phpunit.xml --testsuite integration
- SYMFONY_SERIALIZER=1 vendor/bin/phpunit --colors -c phpunit.xml --testsuite integration
- JMS_BUNDLE=1 DOCTRINE_BUNDLE=1 vendor/bin/phpunit --colors -c phpunit.xml --testsuite integration
- SYMFONY_SERIALIZER=1 DOCTRINE_BUNDLE=1 vendor/bin/phpunit --colors -c phpunit.xml --testsuite integration
18 changes: 18 additions & 0 deletions Adapters/Builtin/BuiltinNormalizerAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Bankiru\Api\JsonRpc\Adapters\Builtin;

use Bankiru\Api\JsonRpc\NormalizerInterface;

final class BuiltinNormalizerAdapter implements NormalizerInterface
{
/** {@inheritdoc} */
public function normalize($entity, array $context = [])
{
if (null === $entity) {
return null;
}

return json_decode(json_encode($entity), true);
}
}
69 changes: 69 additions & 0 deletions Adapters/JMS/Compiler/RelationHandlerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Bankiru\Api\JsonRpc\Adapters\JMS\Compiler;

use Bankiru\Api\JsonRpc\Adapters\JMS\RelationsHandler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class RelationHandlerPass implements CompilerPassInterface
{
/** {@inheritdoc} */
public function process(ContainerBuilder $container)
{
if (!$container->hasParameter('jsonrpc_server.jms.handlers')) {
return;
}

foreach ($container->getParameter('jsonrpc_server.jms.handlers') as $handler => $emid) {
$this->configureRelationHandler($container, $handler, $emid);
}
}

private function configureRelationHandler(ContainerBuilder $builder, $handler, $emid)
{
if (!$builder->has($emid)) {
return;
}

$builder->register('jms_serializer.handler.relation.' . $handler, RelationsHandler::class)
->setArguments([new Reference($emid)])
->addTag(
'jms_serializer.handler',
[
'type' => $handler,
'direction' => 'serialization',
'format' => 'json',
'method' => 'serializeRelation',
]
)
->addTag(
'jms_serializer.handler',
[
'type' => $handler,
'direction' => 'deserialization',
'format' => 'json',
'method' => 'deserializeRelation',
]
)
->addTag(
'jms_serializer.handler',
[
'type' => $handler . '<?>',
'direction' => 'serialization',
'format' => 'json',
'method' => 'serializeRelation',
]
)
->addTag(
'jms_serializer.handler',
[
'type' => $handler . '<?>',
'direction' => 'deserialization',
'format' => 'json',
'method' => 'deserializeRelation',
]
);
}
}
42 changes: 42 additions & 0 deletions Adapters/JMS/JmsNormalizerAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Bankiru\Api\JsonRpc\Adapters\JMS;

use Bankiru\Api\JsonRpc\NormalizerInterface;
use JMS\Serializer\ArrayTransformerInterface;
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;

final class JmsNormalizerAdapter implements NormalizerInterface
{
/** @var ArrayTransformerInterface */
private $normalizer;
/** @var SerializationContextFactoryInterface */
private $contextFactory;

/**
* JmsNormalizerAdapter constructor.
*
* @param ArrayTransformerInterface $normalizer
* @param SerializationContextFactoryInterface $contextFactory
*/
public function __construct(
ArrayTransformerInterface $normalizer,
SerializationContextFactoryInterface $contextFactory
) {
$this->normalizer = $normalizer;
$this->contextFactory = $contextFactory;
}

/** {@inheritdoc} */
public function normalize($entity, array $context = [])
{
if (null === $entity) {
return null;
}

$jmsContext = $this->contextFactory->createSerializationContext();
$jmsContext->setGroups($context);

return $this->normalizer->toArray($entity, $jmsContext);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
<?php

namespace Bankiru\Api\JsonRpc\Serializer;
namespace Bankiru\Api\JsonRpc\Adapters\JMS;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Mapping\ClassMetadata;
use JMS\Serializer\Context;
use JMS\Serializer\JsonDeserializationVisitor;
use JMS\Serializer\JsonSerializationVisitor;

final class RelationsHandler
{
/**
* @var EntityManagerInterface
* @var ObjectManager
*/
private $manager;

/**
* RelationsHandler constructor.
*
* @param EntityManagerInterface $manager
* @param ObjectManager $manager
*/
public function __construct(EntityManagerInterface $manager) { $this->manager = $manager; }

public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}

public function serializeRelation(JsonSerializationVisitor $visitor, $relation, array $type, Context $context)
public function serializeRelation(JsonSerializationVisitor $visitor, $relation)
{
if ($relation instanceof \Traversable) {
$relation = iterator_to_array($relation);
Expand All @@ -36,24 +37,7 @@ public function serializeRelation(JsonSerializationVisitor $visitor, $relation,
return $this->getSingleEntityRelation($relation);
}

/**
* @param $relation
*
* @return array|mixed
*/
protected function getSingleEntityRelation($relation)
{
$metadata = $this->manager->getClassMetadata(get_class($relation));

$ids = $metadata->getIdentifierValues($relation);
if (!$metadata->isIdentifierComposite) {
$ids = array_shift($ids);
}

return $ids;
}

public function deserializeRelation(JsonDeserializationVisitor $visitor, $relation, array $type, Context $context)
public function deserializeRelation(JsonDeserializationVisitor $visitor, $relation, array $type)
{
$className = isset($type['params'][0]['name']) ? $type['params'][0]['name'] : null;

Expand All @@ -65,7 +49,7 @@ public function deserializeRelation(JsonDeserializationVisitor $visitor, $relati
$metadata = $this->manager->getClassMetadata($className);

if (!is_array($relation)) {
return $this->manager->getReference($className, $relation);
return $this->deserializeIdentifier($className, $relation);
}

$single = false;
Expand All @@ -77,14 +61,46 @@ public function deserializeRelation(JsonDeserializationVisitor $visitor, $relati
}

if ($single) {
return $this->manager->getReference($className, $relation);
return $this->deserializeIdentifier($className, $relation);
}

$objects = [];
foreach ($relation as $idSet) {
$objects[] = $this->manager->getReference($className, $idSet);
$objects[] = $this->deserializeIdentifier($className, $idSet);
}

return $objects;
}

/**
* @param $relation
*
* @return array|mixed
*/
private function getSingleEntityRelation($relation)
{
$metadata = $this->manager->getClassMetadata(get_class($relation));

$ids = $metadata->getIdentifierValues($relation);
if (1 === count($metadata->getIdentifierFieldNames())) {
$ids = array_shift($ids);
}

return $ids;
}

/**
* @param string $className
* @param mixed $identifier
*
* @return object
*/
private function deserializeIdentifier($className, $identifier)
{
if (method_exists($this->manager, 'getReference')) {
return $this->manager->getReference($className, $identifier);
}

return $this->manager->find($className, $identifier);
}
}
Loading