Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
/composer.lock
/phpunit.xml
/phpspec.xml
/vendor

View File

@@ -0,0 +1,34 @@
sudo: false
language: php
cache:
directories:
- $HOME/.composer/cache/files
env:
COMPOSER_OPTIONS="--prefer-stable"
matrix:
fast_finish: true
include:
- php: 5.6
env: COMPOSER_OPTIONS="--prefer-lowest --prefer-stable"
- php: 7.0
- php: 7.1
env: xdebug="yes"
- php: 7.2
before_install:
- if [[ "$xdebug" != "yes" ]]; then phpenv config-rm xdebug.ini; fi
- composer self-update
- composer require 'phpunit/phpunit:^4.8.35|^5.4.3|^6.0' --dev --no-update
install:
- composer update $COMPOSER_OPTIONS
script:
- vendor/bin/phpspec run
- if [[ "$xdebug" = "yes" ]]; then vendor/bin/phpunit --coverage-clover=coverage.clover; else vendor/bin/phpunit; fi
- if [[ "$xdebug" = "yes" ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [[ "$xdebug" = "yes" ]]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi

View File

@@ -0,0 +1,34 @@
CHANGELOG
=========
2.1.0
-----
* added missing support for statement versions
* dropped support for PHP < 5.6
* allow `2.x` releases of the `php-xapi/model` package too
* allow `3.x` releases of the `php-xapi/model` package too
2.0.0
-----
Added support for deserializing raw attachment contents. In order to achieve this
an optional `$attachments` argument has been added to the `deserializeStatement()`
and `deserializeStatements()` methods of the `StatementSerializer` class.
When being passed, this argument must be an array mapping SHA-2 hashes to an
array which in turn maps the `type` and `content` keys to the attachment's
content type and raw content data respectively.
1.0.0
-----
The is the first stable release using the Symfony Serializer component to
provide an implementation of the API described by the `php-xapi/serializer`
package.
Apart from dependency versions being bumped to stable versions this release
provides the same features as `0.1.0`.

View File

@@ -0,0 +1,19 @@
Copyright (c) 2016-2019 Christian Flothmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,15 @@
Symfony Based Serializer for the xApi
=====================================
[![Build Status](https://travis-ci.org/php-xapi/symfony-serializer.svg?branch=master)](https://travis-ci.org/php-xapi/symfony-serializer)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/php-xapi/symfony-serializer/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/php-xapi/symfony-serializer/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/php-xapi/symfony-serializer/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/php-xapi/symfony-serializer/?branch=master)
Transform [Experience API](https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md)
model objects into JSON encoded strings conforming to the API specs and vice
versa leveraging the Symfony Serializer component.
This package provides an implementation of the interface defined by the
[php-xapi/serializer package](https://github.com/php-xapi/serializer) and
provides the virtual [php-xapi/serializer-implementation](https://packagist.org/providers/php-xapi/serializer-implementation)
package.

View File

@@ -0,0 +1,14 @@
UPGRADE
=======
Upgrading from 1.x to 2.0
-------------------------
Added support for deserializing raw attachment contents. In order to achieve this
an optional `$attachments` argument has been added to the `deserializeStatement()`
and `deserializeStatements()` methods of the `StatementSerializer` class as well
as to the `StatementResultSerializer::deserializeStatementResult()`.
When being passed, this argument must be an array mapping SHA-2 hashes to an
array which in turn maps the `type` and `content` keys to the attachment's
content type and raw content data respectively.

View File

@@ -0,0 +1,46 @@
{
"name": "php-xapi/symfony-serializer",
"type": "library",
"description": "transform Experience API model objects to JSON encoded strings and vice versa using the Symfony Serializer component",
"keywords": ["xAPI", "Experience API", "Tin Can API", "serializer", "Symfony"],
"homepage": "https://github.com/php-xapi/symfony-serializer",
"license": "MIT",
"authors": [
{
"name": "Christian Flothmann",
"homepage": "https://github.com/xabbuh"
}
],
"require": {
"php": "^5.6 || ^7.0",
"php-xapi/exception": "^0.2.0",
"php-xapi/model": "^1.2 || ^2.1 || ^3.0",
"php-xapi/serializer": "^2.1.1",
"symfony/serializer": "^2.8 || ^3.4 || ^4.0"
},
"require-dev": {
"php-xapi/json-test-fixtures": "^2.0",
"php-xapi/test-fixtures": "^1.0",
"phpspec/phpspec": "~2.3"
},
"minimum-stability": "dev",
"provide": {
"php-xapi/serializer-implementation": "2.0"
},
"autoload": {
"psr-4": {
"Xabbuh\\XApi\\Serializer\\Symfony\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"spec\\Xabbuh\\XApi\\Serializer\\Symfony\\": "spec/",
"Xabbuh\\XApi\\Serializer\\Symfony\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.1.x-dev"
}
}
}

View File

@@ -0,0 +1,4 @@
suites:
default:
namespace: Xabbuh\XApi\Serializer\Symfony
psr4_prefix: Xabbuh\XApi\Serializer\Symfony

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Experience API model serializer leveraging the Symfony Serializer component">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>

View File

@@ -0,0 +1,40 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
use Xabbuh\XApi\DataFixtures\AccountFixtures;
use Xabbuh\XApi\Model\IRL;
use XApi\Fixtures\Json\AccountJsonFixtures;
class AccountNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
function it_supports_normalizing_accounts()
{
$this->supportsNormalization(AccountFixtures::getTypicalAccount())->shouldBe(true);
}
function it_denormalizes_accounts()
{
$account = $this->denormalize(array('homePage' => 'https://tincanapi.com', 'name' => 'test'), 'Xabbuh\XApi\Model\Account');
$account->shouldBeAnInstanceOf('Xabbuh\XApi\Model\Account');
$account->getHomePage()->equals(IRL::fromString('https://tincanapi.com'))->shouldReturn(true);
$account->getName()->shouldReturn('test');
}
function it_supports_denormalizing_accounts()
{
$this->supportsDenormalization(AccountJsonFixtures::getTypicalAccount(), 'Xabbuh\XApi\Model\Account')->shouldBe(true);
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
use Xabbuh\XApi\DataFixtures\ActorFixtures;
use XApi\Fixtures\Json\ActorJsonFixtures;
class ActorNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
function it_requires_an_iri_when_denormalizing_an_agent()
{
$this
->shouldThrow('\Symfony\Component\Serializer\Exception\InvalidArgumentException')
->during('denormalize', array(
array('objectType' => 'Agent'),
'Xabbuh\XApi\Model\Actor',
))
;
}
function it_can_denormalize_agents_with_mbox_sha1_sum()
{
$data = array(
'mbox_sha1sum' => 'db77b9104b531ecbb0b967f6942549d0ba80fda1',
);
$agent = $this->denormalize($data, 'Xabbuh\XApi\Model\Actor');
$agent->shouldBeAnInstanceOf('Xabbuh\XApi\Model\Agent');
$agent->getInverseFunctionalIdentifier()->getMboxSha1Sum()->shouldReturn('db77b9104b531ecbb0b967f6942549d0ba80fda1');
}
function it_supports_normalizing_agents()
{
$this->supportsNormalization(ActorFixtures::getTypicalAgent())->shouldBe(true);
}
function it_supports_normalizing_groups()
{
$this->supportsNormalization(ActorFixtures::getTypicalGroup())->shouldBe(true);
}
function it_supports_denormalizing_agents()
{
$this->supportsDenormalization(ActorJsonFixtures::getTypicalAgent(), 'Xabbuh\XApi\Model\Actor')->shouldBe(true);
}
function it_supports_denormalizing_groups()
{
$this->supportsDenormalization(ActorJsonFixtures::getTypicalGroup(), 'Xabbuh\XApi\Model\Actor')->shouldBe(true);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
use Xabbuh\XApi\Model\Attachment;
use Xabbuh\XApi\Model\IRI;
use Xabbuh\XApi\Model\LanguageMap;
class AttachmentNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_supports_normalizing_attachment_objects()
{
$attachment = new Attachment(
IRI::fromString('http://id.tincanapi.com/attachment/supporting_media'),
'text/plain',
18,
'bd1a58265d96a3d1981710dab8b1e1ed04a8d7557ea53ab0cf7b44c04fd01545',
LanguageMap::create(array('en-US' => 'Text attachment')),
null,
null,
'some text content'
);
$this->supportsNormalization($attachment)->shouldReturn(true);
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
function it_supports_denormalizing_to_attachment_objects()
{
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Attachment')->shouldReturn(true);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
use Xabbuh\XApi\Model\Definition;
class DefinitionNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_supports_normalizing_definition_objects()
{
$this->supportsNormalization(new Definition())->shouldReturn(true);
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
function it_supports_denormalizing_to_definition_objects()
{
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Definition')->shouldReturn(true);
}
function it_supports_denormalizing_to_interaction_definition_objects()
{
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition')->shouldReturn(true);
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition')->shouldReturn(true);
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition')->shouldReturn(true);
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition')->shouldReturn(true);
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition')->shouldReturn(true);
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition')->shouldReturn(true);
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition')->shouldReturn(true);
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition')->shouldReturn(true);
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition')->shouldReturn(true);
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition')->shouldReturn(true);
}
function it_throws_an_exception_when_an_unknown_interaction_type_should_be_denormalized()
{
$this->shouldThrow('Symfony\Component\Serializer\Exception\InvalidArgumentException')->during('denormalize', array(
array('interactionType' => 'foo'),
'Xabbuh\XApi\Model\Definition'
));
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
class DocumentDataNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
use Xabbuh\XApi\Model\Interaction\InteractionComponent;
class InteractionComponentNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_supports_normalizing_interaction_component_objects()
{
$this->supportsNormalization(new InteractionComponent('test'))->shouldReturn(true);
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
function it_supports_denormalizing_to_interaction_component_objects()
{
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Interaction\InteractionComponent')->shouldReturn(true);
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
use Xabbuh\XApi\Model\LanguageMap;
class LanguageMapNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
function it_supports_normalizing_language_map_objects()
{
$this->supportsNormalization(new LanguageMap())->shouldReturn(true);
}
function it_normalizes_language_map_instances_to_arrays()
{
$map = array(
'de-DE' => 'teilgenommen',
'en-GB' => 'attended',
'en-US' => 'attended',
);
$normalizedMap = $this->normalize(LanguageMap::create($map));
$normalizedMap->shouldBeArray();
$normalizedMap->shouldHaveCount(3);
$normalizedMap->shouldHaveKeyWithValue('de-DE', 'teilgenommen');
$normalizedMap->shouldHaveKeyWithValue('en-GB', 'attended');
$normalizedMap->shouldHaveKeyWithValue('en-US', 'attended');
}
function it_supports_denormalizing_to_language_map_objects()
{
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\LanguageMap')->shouldReturn(true);
}
function it_denormalizes_arrays_to_language_map_instances()
{
$map = array(
'de-DE' => 'teilgenommen',
'en-GB' => 'attended',
'en-US' => 'attended',
);
$languageMap = LanguageMap::create($map);
$this->denormalize($map, 'Xabbuh\XApi\Model\LanguageMap')->equals($languageMap)->shouldReturn(true);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
class ObjectNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
class ResultNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
use Xabbuh\XApi\DataFixtures\StatementFixtures;
use XApi\Fixtures\Json\StatementJsonFixtures;
class StatementNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
function it_supports_normalizing_statements()
{
$this->supportsNormalization(StatementFixtures::getMinimalStatement())->shouldBe(true);
}
function it_supports_denormalizing_statements()
{
$this->supportsDenormalization(StatementJsonFixtures::getMinimalStatement(), 'Xabbuh\XApi\Model\Statement')->shouldBe(true);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
class StatementResultNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
class TimestampNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_can_normalize_datetime_objects()
{
$this->supportsNormalization(new \DateTime())->shouldBe(true);
}
function it_cannot_normalize_datetime_like_string()
{
$this->supportsNormalization('2004-02-12T15:19:21+00:00')->shouldBe(false);
}
function it_normalizes_datetime_objects_as_iso_8601_formatted_strings()
{
$date = new \DateTime();
$date->setTimezone(new \DateTimeZone('UTC'));
$date->setDate(2004, 2, 12);
$date->setTime(15, 19, 21);
$this->normalize($date)->shouldReturn('2004-02-12T15:19:21+00:00');
}
function it_throws_an_exception_when_data_other_than_datetime_objects_are_passed()
{
$this->shouldThrow('Symfony\Component\Serializer\Exception\InvalidArgumentException')->during('normalize', array('2004-02-12T15:19:21+00:00'));
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
function it_can_denormalize_to_datetime_objects()
{
$this->supportsDenormalization('2004-02-12T15:19:21+00:00', 'DateTime')->shouldBe(true);
}
function it_denormalizes_iso_8601_formatted_strings_to_datetime_objects()
{
$date = $this->denormalize('2004-02-12T15:19:21+00:00', 'DateTime');
$date->getTimezone()->shouldBeLike(new \DateTimeZone('UTC'));
$date->format('Y-m-d H:i:s')->shouldReturn('2004-02-12 15:19:21');
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony\Normalizer;
use PhpSpec\ObjectBehavior;
use Xabbuh\XApi\Model\IRI;
use Xabbuh\XApi\Model\Verb;
class VerbNormalizerSpec extends ObjectBehavior
{
function it_is_a_normalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
}
function it_supports_normalizing_verb_objects()
{
$this->supportsNormalization(new Verb(IRI::fromString('http://tincanapi.com/conformancetest/verbid')))->shouldReturn(true);
}
function it_is_a_denormalizer()
{
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
}
function it_supports_denormalizing_to_verb_objects()
{
$this->supportsDenormalization(array(), 'Xabbuh\XApi\Model\Verb')->shouldReturn(true);
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony;
use spec\Xabbuh\XApi\Serializer\SerializerFactoryInterfaceSpec;
class SerializerFactorySpec extends SerializerFactoryInterfaceSpec
{
}

View File

@@ -0,0 +1,13 @@
<?php
namespace spec\Xabbuh\XApi\Serializer\Symfony;
use PhpSpec\ObjectBehavior;
class SerializerSpec extends ObjectBehavior
{
function it_creates_a_serializer()
{
self::createSerializer()->shouldBeAnInstanceOf('Symfony\Component\Serializer\SerializerInterface');
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony;
use Symfony\Component\Serializer\SerializerInterface;
use Xabbuh\XApi\Model\Actor;
use Xabbuh\XApi\Serializer\ActorSerializerInterface;
/**
* Serializes and deserializes {@link Actor actors} using the Symfony Serializer component.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class ActorSerializer implements ActorSerializerInterface
{
/**
* @var SerializerInterface
*/
private $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
/**
* {@inheritDoc}
*/
public function serializeActor(Actor $actor)
{
return $this->serializer->serialize($actor, 'json');
}
/**
* {@inheritDoc}
*/
public function deserializeActor($data)
{
return $this->serializer->deserialize($data, 'Xabbuh\XApi\Model\Actor', 'json');
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony;
use Symfony\Component\Serializer\SerializerInterface;
use Xabbuh\XApi\Model\DocumentData;
use Xabbuh\XApi\Serializer\DocumentDataSerializerInterface;
/**
* Serializes and deserializes {@link Document documents} using the Symfony Serializer component.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class DocumentDataSerializer implements DocumentDataSerializerInterface
{
/**
* @var SerializerInterface
*/
private $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
/**
* {@inheritDoc}
*/
public function serializeDocumentData(DocumentData $data)
{
return $this->serializer->serialize($data, 'json');
}
/**
* {@inheritDoc}
*/
public function deserializeDocumentData($data)
{
return $this->serializer->deserialize($data, 'Xabbuh\XApi\Model\DocumentData', 'json');
}
}

View File

@@ -0,0 +1,73 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Xabbuh\XApi\Model\Account;
use Xabbuh\XApi\Model\IRL;
/**
* Normalizes and denormalizes xAPI statement accounts.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class AccountNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof Account) {
return null;
}
return array(
'name' => $object->getName(),
'homePage' => $object->getHomePage()->getValue(),
);
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof Account;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$name = '';
$homePage = '';
if (isset($data['name'])) {
$name = $data['name'];
}
if (isset($data['homePage'])) {
$homePage = $data['homePage'];
}
return new Account($name, IRL::fromString($homePage));
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\Account' === $type;
}
}

View File

@@ -0,0 +1,162 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Xabbuh\XApi\Model\Actor;
use Xabbuh\XApi\Model\Agent;
use Xabbuh\XApi\Model\Group;
use Xabbuh\XApi\Model\InverseFunctionalIdentifier;
use Xabbuh\XApi\Model\IRI;
/**
* Normalizes and denormalizes xAPI statement actors.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class ActorNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof Actor) {
return null;
}
$data = array();
$this->normalizeInverseFunctionalIdentifier($object->getInverseFunctionalIdentifier(), $data, $format, $context);
if (null !== $name = $object->getName()) {
$data['name'] = $name;
}
if ($object instanceof Group) {
$members = array();
foreach ($object->getMembers() as $member) {
$members[] = $this->normalize($member);
}
if (count($members) > 0) {
$data['member'] = $members;
}
$data['objectType'] = 'Group';
} else {
$data['objectType'] = 'Agent';
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof Actor;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$inverseFunctionalIdentifier = $this->denormalizeInverseFunctionalIdentifier($data, $format, $context);
$name = isset($data['name']) ? $data['name'] : null;
if (isset($data['objectType']) && 'Group' === $data['objectType']) {
return $this->denormalizeGroup($inverseFunctionalIdentifier, $name, $data, $format, $context);
}
if (null === $inverseFunctionalIdentifier) {
throw new InvalidArgumentException('Missing inverse functional identifier for agent.');
}
return new Agent($inverseFunctionalIdentifier, $name);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\Actor' === $type || 'Xabbuh\XApi\Model\Agent' === $type || 'Xabbuh\XApi\Model\Group' === $type;
}
private function normalizeInverseFunctionalIdentifier(InverseFunctionalIdentifier $iri = null, &$data, $format = null, array $context = array())
{
if (null === $iri) {
return;
}
if (null !== $mbox = $iri->getMbox()) {
$data['mbox'] = $mbox->getValue();
}
if (null !== $mboxSha1Sum = $iri->getMboxSha1Sum()) {
$data['mbox_sha1sum'] = $mboxSha1Sum;
}
if (null !== $openId = $iri->getOpenId()) {
$data['openid'] = $openId;
}
if (null !== $account = $iri->getAccount()) {
$data['account'] = $this->normalizeAttribute($account, $format, $context);
}
}
private function denormalizeInverseFunctionalIdentifier($data, $format = null, array $context = array())
{
if (isset($data['mbox'])) {
return InverseFunctionalIdentifier::withMbox(IRI::fromString($data['mbox']));
}
if (isset($data['mbox_sha1sum'])) {
return InverseFunctionalIdentifier::withMboxSha1Sum($data['mbox_sha1sum']);
}
if (isset($data['openid'])) {
return InverseFunctionalIdentifier::withOpenId($data['openid']);
}
if (isset($data['account'])) {
return InverseFunctionalIdentifier::withAccount($this->denormalizeAccount($data, $format, $context));
}
}
private function denormalizeAccount($data, $format = null, array $context = array())
{
if (!isset($data['account'])) {
return null;
}
return $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account', $format, $context);
}
private function denormalizeGroup(InverseFunctionalIdentifier $iri = null, $name, $data, $format = null, array $context = array())
{
$members = array();
if (isset($data['member'])) {
foreach ($data['member'] as $member) {
$members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent', $format, $context);
}
}
return new Group($iri, $name, $members);
}
}

View File

@@ -0,0 +1,87 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Xabbuh\XApi\Model\Attachment;
use Xabbuh\XApi\Model\IRI;
use Xabbuh\XApi\Model\IRL;
/**
* Denormalizes PHP arrays to {@link Attachment} objects.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class AttachmentNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof Attachment) {
return;
}
$data = array(
'usageType' => $object->getUsageType()->getValue(),
'contentType' => $object->getContentType(),
'length' => $object->getLength(),
'sha2' => $object->getSha2(),
'display' => $this->normalizeAttribute($object->getDisplay(), $format, $context),
);
if (null !== $description = $object->getDescription()) {
$data['description'] = $this->normalizeAttribute($description, $format, $context);
}
if (null !== $fileUrl = $object->getFileUrl()) {
$data['fileUrl'] = $fileUrl->getValue();
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof Attachment;
}
public function denormalize($data, $class, $format = null, array $context = array())
{
$display = $this->denormalizeData($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
$description = null;
$fileUrl = null;
$content = null;
if (isset($data['description'])) {
$description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
}
if (isset($data['fileUrl'])) {
$fileUrl = IRL::fromString($data['fileUrl']);
}
if (isset($context['xapi_attachments'][$data['sha2']])) {
$content = $context['xapi_attachments'][$data['sha2']]['content'];
}
return new Attachment(IRI::fromString($data['usageType']), $data['contentType'], $data['length'], $data['sha2'], $display, $description, $fileUrl, $content);
}
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\Attachment' === $type;
}
}

View File

@@ -0,0 +1,101 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Xabbuh\XApi\Model\ContextActivities;
/**
* Normalizes and denormalizes xAPI statement context activities.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class ContextActivitiesNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof ContextActivities) {
return;
}
$data = array();
if (null !== $categoryActivities = $object->getCategoryActivities()) {
$data['category'] = $this->normalizeAttribute($categoryActivities);
}
if (null !== $parentActivities = $object->getParentActivities()) {
$data['parent'] = $this->normalizeAttribute($parentActivities);
}
if (null !== $groupingActivities = $object->getGroupingActivities()) {
$data['grouping'] = $this->normalizeAttribute($groupingActivities);
}
if (null !== $otherActivities = $object->getOtherActivities()) {
$data['other'] = $this->normalizeAttribute($otherActivities);
}
if (empty($data)) {
return new \stdClass();
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof ContextActivities;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$parentActivities = null;
$groupingActivities = null;
$categoryActivities = null;
$otherActivities = null;
if (isset($data['parent']) && null !== $data['parent']) {
$parentActivities = $this->denormalizeData($data['parent'], 'Xabbuh\XApi\Model\Activity[]', $format, $context);
}
if (isset($data['grouping']) && null !== $data['grouping']) {
$groupingActivities = $this->denormalizeData($data['grouping'], 'Xabbuh\XApi\Model\Activity[]', $format, $context);
}
if (isset($data['category']) && null !== $data['category']) {
$categoryActivities = $this->denormalizeData($data['category'], 'Xabbuh\XApi\Model\Activity[]', $format, $context);
}
if (isset($data['other']) && null !== $data['other']) {
$otherActivities = $this->denormalizeData($data['other'], 'Xabbuh\XApi\Model\Activity[]', $format, $context);
}
return new ContextActivities($parentActivities, $groupingActivities, $categoryActivities, $otherActivities);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\ContextActivities' === $type;
}
}

View File

@@ -0,0 +1,126 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Xabbuh\XApi\Model\Context;
/**
* Normalizes and denormalizes xAPI statement contexts.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class ContextNormalizer extends Normalizer
{
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof Context) {
return;
}
$data = array();
if (null !== $registration = $object->getRegistration()) {
$data['registration'] = $registration;
}
if (null !== $instructor = $object->getInstructor()) {
$data['instructor'] = $this->normalizeAttribute($instructor, $format, $context);
}
if (null !== $team = $object->getTeam()) {
$data['team'] = $this->normalizeAttribute($team, $format, $context);
}
if (null !== $contextActivities = $object->getContextActivities()) {
$data['contextActivities'] = $this->normalizeAttribute($contextActivities, $format, $context);
}
if (null !== $revision = $object->getRevision()) {
$data['revision'] = $revision;
}
if (null !== $platform = $object->getPlatform()) {
$data['platform'] = $platform;
}
if (null !== $language = $object->getLanguage()) {
$data['language'] = $language;
}
if (null !== $statement = $object->getStatement()) {
$data['statement'] = $this->normalizeAttribute($statement, $format, $context);
}
if (null !== $extensions = $object->getExtensions()) {
$data['extensions'] = $this->normalizeAttribute($extensions, $format, $context);
}
if (empty($data)) {
return new \stdClass();
}
return $data;
}
public function supportsNormalization($data, $format = null)
{
return $data instanceof Context;
}
public function denormalize($data, $class, $format = null, array $context = array())
{
$statementContext = new Context();
if (isset($data['registration'])) {
$statementContext = $statementContext->withRegistration($data['registration']);
}
if (isset($data['instructor'])) {
$statementContext = $statementContext->withInstructor($this->denormalizeData($data['instructor'], 'Xabbuh\XApi\Model\Actor', $format, $context));
}
if (isset($data['team'])) {
$statementContext = $statementContext->withTeam($this->denormalizeData($data['team'], 'Xabbuh\XApi\Model\Group', $format, $context));
}
if (isset($data['contextActivities'])) {
$statementContext = $statementContext->withContextActivities($this->denormalizeData($data['contextActivities'], 'Xabbuh\XApi\Model\ContextActivities', $format, $context));
}
if (isset($data['revision'])) {
$statementContext = $statementContext->withRevision($data['revision']);
}
if (isset($data['platform'])) {
$statementContext = $statementContext->withPlatform($data['platform']);
}
if (isset($data['language'])) {
$statementContext = $statementContext->withLanguage($data['language']);
}
if (isset($data['statement'])) {
$statementContext = $statementContext->withStatement($this->denormalizeData($data['statement'], 'Xabbuh\XApi\Model\StatementReference', $format, $context));
}
if (isset($data['extensions'])) {
$statementContext = $statementContext->withExtensions($this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context));
}
return $statementContext;
}
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\Context' === $type;
}
}

View File

@@ -0,0 +1,256 @@
<?php
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Xabbuh\XApi\Model\Definition;
use Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition;
use Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition;
use Xabbuh\XApi\Model\Interaction\InteractionDefinition;
use Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition;
use Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition;
use Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition;
use Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition;
use Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition;
use Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition;
use Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition;
use Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition;
use Xabbuh\XApi\Model\IRI;
use Xabbuh\XApi\Model\IRL;
/**
* Normalizes and denormalizes PHP arrays to {@link Definition} instances.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class DefinitionNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof Definition) {
return;
}
$data = array();
if (null !== $name = $object->getName()) {
$data['name'] = $this->normalizeAttribute($name, $format, $context);
}
if (null !== $description = $object->getDescription()) {
$data['description'] = $this->normalizeAttribute($description, $format, $context);
}
if (null !== $type = $object->getType()) {
$data['type'] = $type->getValue();
}
if (null !== $moreInfo = $object->getMoreInfo()) {
$data['moreInfo'] = $moreInfo->getValue();
}
if (null !== $extensions = $object->getExtensions()) {
$data['extensions'] = $this->normalizeAttribute($extensions, $format, $context);
}
if ($object instanceof InteractionDefinition) {
if (null !== $correctResponsesPattern = $object->getCorrectResponsesPattern()) {
$data['correctResponsesPattern'] = $object->getCorrectResponsesPattern();
}
switch (true) {
case $object instanceof ChoiceInteractionDefinition:
$data['interactionType'] = 'choice';
if (null !== $choices = $object->getChoices()) {
$data['choices'] = $this->normalizeAttribute($choices, $format, $context);
}
break;
case $object instanceof FillInInteractionDefinition:
$data['interactionType'] = 'fill-in';
break;
case $object instanceof LikertInteractionDefinition:
$data['interactionType'] = 'likert';
if (null !== $scale = $object->getScale()) {
$data['scale'] = $this->normalizeAttribute($scale, $format, $context);
}
break;
case $object instanceof LongFillInInteractionDefinition:
$data['interactionType'] = 'long-fill-in';
break;
case $object instanceof MatchingInteractionDefinition:
$data['interactionType'] = 'matching';
if (null !== $source = $object->getSource()) {
$data['source'] = $this->normalizeAttribute($source, $format, $context);
}
if (null !== $target = $object->getTarget()) {
$data['target'] = $this->normalizeAttribute($target, $format, $context);
}
break;
case $object instanceof NumericInteractionDefinition:
$data['interactionType'] = 'numeric';
break;
case $object instanceof OtherInteractionDefinition:
$data['interactionType'] = 'other';
break;
case $object instanceof PerformanceInteractionDefinition:
$data['interactionType'] = 'performance';
if (null !== $steps = $object->getSteps()) {
$data['steps'] = $this->normalizeAttribute($steps, $format, $context);
}
break;
case $object instanceof SequencingInteractionDefinition:
$data['interactionType'] = 'sequencing';
if (null !== $choices = $object->getChoices()) {
$data['choices'] = $this->normalizeAttribute($choices, $format, $context);
}
break;
case $object instanceof TrueFalseInteractionDefinition:
$data['interactionType'] = 'true-false';
break;
}
}
if (empty($data)) {
return new \stdClass();
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof Definition;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
if (isset($data['interactionType'])) {
switch ($data['interactionType']) {
case 'choice':
$definition = new ChoiceInteractionDefinition();
if (isset($data['choices'])) {
$definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
}
break;
case 'fill-in':
$definition = new FillInInteractionDefinition();
break;
case 'likert':
$definition = new LikertInteractionDefinition();
if (isset($data['scale'])) {
$definition = $definition->withScale($this->denormalizeData($data['scale'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
}
break;
case 'long-fill-in':
$definition = new LongFillInInteractionDefinition();
break;
case 'matching':
$definition = new MatchingInteractionDefinition();
if (isset($data['source'])) {
$definition = $definition->withSource($this->denormalizeData($data['source'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
}
if (isset($data['target'])) {
$definition = $definition->withTarget($this->denormalizeData($data['target'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
}
break;
case 'numeric':
$definition = new NumericInteractionDefinition();
break;
case 'other':
$definition = new OtherInteractionDefinition();
break;
case 'performance':
$definition = new PerformanceInteractionDefinition();
if (isset($data['steps'])) {
$definition = $definition->withSteps($this->denormalizeData($data['steps'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
}
break;
case 'sequencing':
$definition = new SequencingInteractionDefinition();
if (isset($data['choices'])) {
$definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
}
break;
case 'true-false':
$definition = new TrueFalseInteractionDefinition();
break;
default:
throw new InvalidArgumentException(sprintf('The interaction type "%s" is not supported.', $data['interactionType']));
}
if (isset($data['correctResponsesPattern'])) {
$definition = $definition->withCorrectResponsesPattern($data['correctResponsesPattern']);
}
} else {
$definition = new Definition();
}
if (isset($data['name'])) {
$name = $this->denormalizeData($data['name'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
$definition = $definition->withName($name);
}
if (isset($data['description'])) {
$description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
$definition = $definition->withDescription($description);
}
if (isset($data['type'])) {
$definition = $definition->withType(IRI::fromString($data['type']));
}
if (isset($data['moreInfo'])) {
$definition = $definition->withMoreInfo(IRL::fromString($data['moreInfo']));
}
if (isset($data['extensions'])) {
$extensions = $this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context);
$definition = $definition->withExtensions($extensions);
}
return $definition;
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
$supportedDefinitionClasses = array(
'Xabbuh\XApi\Model\Definition',
'Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition',
'Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition',
'Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition',
'Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition',
'Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition',
'Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition',
'Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition',
'Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition',
'Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition',
'Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition',
);
return in_array($type, $supportedDefinitionClasses, true);
}
}

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Xabbuh\XApi\Model\DocumentData;
/**
* Normalizes and denormalizes xAPI statement documents.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class DocumentDataNormalizer implements DenormalizerInterface, NormalizerInterface
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof DocumentData) {
return null;
}
return $object->getData();
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof DocumentData;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
return new DocumentData($data);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\DocumentData' === $type;
}
}

View File

@@ -0,0 +1,79 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Xabbuh\XApi\Model\Extensions;
use Xabbuh\XApi\Model\IRI;
/**
* Normalizes and denormalizes xAPI extensions.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class ExtensionsNormalizer implements DenormalizerInterface, NormalizerInterface
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof Extensions) {
return;
}
$extensions = $object->getExtensions();
if (count($extensions) === 0) {
return new \stdClass();
}
$data = array();
foreach ($extensions as $iri) {
$data[$iri->getValue()] = $extensions[$iri];
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof Extensions;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$extensions = new \SplObjectStorage();
foreach ($data as $iri => $value) {
$extensions->attach(IRI::fromString($iri), $value);
}
return new Extensions($extensions);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\Extensions' === $type;
}
}

View File

@@ -0,0 +1,65 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Normalizer wrapping Symfony's PropertyNormalizer to filter null values.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
class FilterNullValueNormalizer implements NormalizerInterface, SerializerAwareInterface
{
private $normalizer;
public function __construct()
{
$this->normalizer = new PropertyNormalizer();
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
$data = $this->normalizer->normalize($object, $format, $context);
$filteredData = new \ArrayObject();
foreach ($data as $key => $value) {
if (null !== $value) {
$filteredData[$key] = $value;
}
}
return $filteredData;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $this->normalizer->supportsNormalization($data, $format);
}
/**
* {@inheritdoc}
*/
public function setSerializer(SerializerInterface $serializer)
{
$this->normalizer->setSerializer($serializer);
}
}

View File

@@ -0,0 +1,74 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Xabbuh\XApi\Model\Interaction\InteractionComponent;
/**
* Denormalizes xAPI statement activity {@link InteractionComponent interaction components}.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class InteractionComponentNormalizer extends Normalizer implements DenormalizerInterface, NormalizerInterface
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof InteractionComponent) {
return;
}
$data = array(
'id' => $object->getId(),
);
if (null !== $description = $object->getDescription()) {
$data['description'] = $this->normalizeAttribute($description, $format, $context);
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof InteractionComponent;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$description = null;
if (isset($data['description'])) {
$description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
}
return new InteractionComponent($data['id'], $description);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\Interaction\InteractionComponent' === $type;
}
}

View File

@@ -0,0 +1,66 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Xabbuh\XApi\Model\LanguageMap;
/**
* Normalizes and denormalizes {@link LanguageMap} instances.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class LanguageMapNormalizer implements DenormalizerInterface, NormalizerInterface
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof LanguageMap) {
return;
}
$map = array();
foreach ($object->languageTags() as $languageTag) {
$map[$languageTag] = $object[$languageTag];
}
return $map;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof LanguageMap;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
return LanguageMap::create($data);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\LanguageMap' === $type;
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
abstract class Normalizer implements DenormalizerInterface, NormalizerInterface, SerializerAwareInterface
{
private $serializer;
/**
* {@inheritdoc}
*/
public function setSerializer(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
protected function normalizeAttribute($value, $format = null, array $context = array())
{
if (!$this->serializer instanceof NormalizerInterface) {
throw new LogicException('Cannot normalize attribute because the injected serializer is not a normalizer');
}
return $this->serializer->normalize($value, $format, $context);
}
protected function denormalizeData($data, $type, $format = null, array $context = array())
{
if (!$this->serializer instanceof DenormalizerInterface) {
throw new LogicException('Cannot denormalize because the injected serializer is not a denormalizer');
}
return $this->serializer->denormalize($data, $type, $format, $context);
}
}

View File

@@ -0,0 +1,151 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Xabbuh\XApi\Model\Activity;
use Xabbuh\XApi\Model\IRI;
use Xabbuh\XApi\Model\Object as LegacyStatementObject;
use Xabbuh\XApi\Model\StatementId;
use Xabbuh\XApi\Model\StatementObject;
use Xabbuh\XApi\Model\StatementReference;
use Xabbuh\XApi\Model\SubStatement;
/**
* Normalizes and denormalizes xAPI statement objects.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class ObjectNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if ($object instanceof Activity) {
$activityData = array(
'objectType' => 'Activity',
'id' => $object->getId()->getValue(),
);
if (null !== $definition = $object->getDefinition()) {
$activityData['definition'] = $this->normalizeAttribute($definition, $format, $context);
}
return $activityData;
}
if ($object instanceof StatementReference) {
return array(
'objectType' => 'StatementRef',
'id' => $object->getStatementId()->getValue(),
);
}
if ($object instanceof SubStatement) {
$data = array(
'objectType' => 'SubStatement',
'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
);
if (null !== $result = $object->getResult()) {
$data['result'] = $this->normalizeAttribute($result, $format, $context);
}
if (null !== $statementContext = $object->getContext()) {
$data['context'] = $this->normalizeAttribute($statementContext, $format, $context);
}
return $data;
}
return null;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof LegacyStatementObject || $data instanceof StatementObject;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!isset($data['objectType']) || 'Activity' === $data['objectType']) {
return $this->denormalizeActivity($data, $format, $context);
}
if (isset($data['objectType']) && ('Agent' === $data['objectType'] || 'Group' === $data['objectType'])) {
return $this->denormalizeData($data, 'Xabbuh\XApi\Model\Actor', $format, $context);
}
if (isset($data['objectType']) && 'SubStatement' === $data['objectType']) {
return $this->denormalizeSubStatement($data, $format, $context);
}
if (isset($data['objectType']) && 'StatementRef' === $data['objectType']) {
return new StatementReference(StatementId::fromString($data['id']));
}
return null;
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return in_array($type, array(Activity::class, LegacyStatementObject::class, StatementObject::class, StatementReference::class, SubStatement::class), true);
}
private function denormalizeActivity(array $data, $format = null, array $context = array())
{
$definition = null;
if (isset($data['definition'])) {
$definition = $this->denormalizeData($data['definition'], 'Xabbuh\XApi\Model\Definition', $format, $context);
}
return new Activity(IRI::fromString($data['id']), $definition);
}
private function denormalizeSubStatement(array $data, $format = null, array $context = array())
{
$actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
$verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
if (class_exists(StatementObject::class)) {
$object = $this->denormalizeData($data['object'], StatementObject::class, $format, $context);
} else {
$object = $this->denormalizeData($data['object'], LegacyStatementObject::class, $format, $context);
}
$result = null;
$statementContext = null;
if (isset($data['result'])) {
$result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
}
if (isset($data['context'])) {
$statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context);
}
return new SubStatement($actor, $verb, $object, $result, $statementContext);
}
}

View File

@@ -0,0 +1,95 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Xabbuh\XApi\Model\Result;
/**
* Normalizes and denormalizes xAPI statement results.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class ResultNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof Result) {
return null;
}
$data = array();
if (null !== $object->getScore()) {
$data['score'] = $this->normalizeAttribute($object->getScore(), 'Xabbuh\XApi\Model\Score', $context);
}
if (null !== $success = $object->getSuccess()) {
$data['success'] = $success;
}
if (null !== $completion = $object->getCompletion()) {
$data['completion'] = $completion;
}
if (null !== $response = $object->getResponse()) {
$data['response'] = $response;
}
if (null !== $duration = $object->getDuration()) {
$data['duration'] = $duration;
}
if (null !== $extensions = $object->getExtensions()) {
$data['extensions'] = $this->normalizeAttribute($extensions, 'Xabbuh\XApi\Model\Extensions', $context);
}
if (empty($data)) {
return new \stdClass();
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof Result;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$score = isset($data['score']) ? $this->denormalizeData($data['score'], 'Xabbuh\XApi\Model\Score', $format, $context) : null;
$success = isset($data['success']) ? $data['success'] : null;
$completion = isset($data['completion']) ? $data['completion'] : null;
$response = isset($data['response']) ? $data['response'] : null;
$duration = isset($data['duration']) ? $data['duration'] : null;
$extensions = isset($data['extensions']) ? $this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context) : null;
return new Result($score, $success, $completion, $response, $duration, $extensions);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\Result' === $type;
}
}

View File

@@ -0,0 +1,151 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Xabbuh\XApi\Common\Exception\UnsupportedStatementVersionException;
use Xabbuh\XApi\Model\Object as LegacyStatementObject;
use Xabbuh\XApi\Model\Statement;
use Xabbuh\XApi\Model\StatementId;
use Xabbuh\XApi\Model\StatementObject;
/**
* Normalizes and denormalizes xAPI statements.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class StatementNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof Statement) {
return null;
}
$data = array(
'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
);
if (null !== $id = $object->getId()) {
$data['id'] = $id->getValue();
}
if (null !== $authority = $object->getAuthority()) {
$data['authority'] = $this->normalizeAttribute($authority, $format, $context);
}
if (null !== $result = $object->getResult()) {
$data['result'] = $this->normalizeAttribute($result, $format, $context);
}
if (null !== $result = $object->getCreated()) {
$data['timestamp'] = $this->normalizeAttribute($result, $format, $context);
}
if (null !== $result = $object->getStored()) {
$data['stored'] = $this->normalizeAttribute($result, $format, $context);
}
if (null !== $object->getContext()) {
$data['context'] = $this->normalizeAttribute($object->getContext(), $format, $context);
}
if (null !== $attachments = $object->getAttachments()) {
$data['attachments'] = $this->normalizeAttribute($attachments, $format, $context);
}
if (null !== $version = $object->getVersion()) {
$data['version'] = $version;
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof Statement;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$version = null;
if (isset($data['version'])) {
$version = $data['version'];
if (!preg_match('/^1\.0(?:\.\d+)?$/', $version)) {
throw new UnsupportedStatementVersionException(sprintf('Statements at version "%s" are not supported.', $version));
}
}
$id = isset($data['id']) ? StatementId::fromString($data['id']) : null;
$actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
$verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
if (class_exists(StatementObject::class)) {
$object = $this->denormalizeData($data['object'], StatementObject::class, $format, $context);
} else {
$object = $this->denormalizeData($data['object'], LegacyStatementObject::class, $format, $context);
}
$result = null;
$authority = null;
$created = null;
$stored = null;
$statementContext = null;
$attachments = null;
if (isset($data['result'])) {
$result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
}
if (isset($data['authority'])) {
$authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context);
}
if (isset($data['timestamp'])) {
$created = $this->denormalizeData($data['timestamp'], 'DateTime', $format, $context);
}
if (isset($data['stored'])) {
$stored = $this->denormalizeData($data['stored'], 'DateTime', $format, $context);
}
if (isset($data['context'])) {
$statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context);
}
if (isset($data['attachments'])) {
$attachments = $this->denormalizeData($data['attachments'], 'Xabbuh\XApi\Model\Attachment[]', $format, $context);
}
return new Statement($id, $actor, $verb, $object, $result, $authority, $created, $stored, $statementContext, $attachments, $version);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\Statement' === $type;
}
}

View File

@@ -0,0 +1,78 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Xabbuh\XApi\Model\IRL;
use Xabbuh\XApi\Model\StatementResult;
/**
* Normalizes and denormalizes xAPI statement collections.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class StatementResultNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof StatementResult) {
return null;
}
$data = array(
'statements' => array(),
);
foreach ($object->getStatements() as $statement) {
$data['statements'][] = $this->normalizeAttribute($statement, $format, $context);
}
if (null !== $moreUrlPath = $object->getMoreUrlPath()) {
$data['more'] = $moreUrlPath->getValue();
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof StatementResult;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$statements = $this->denormalizeData($data['statements'], 'Xabbuh\XApi\Model\Statement[]', $format, $context);
$moreUrlPath = null;
if (isset($data['more'])) {
$moreUrlPath = IRL::fromString($data['more']);
}
return new StatementResult($statements, $moreUrlPath);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\StatementResult' === $type;
}
}

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Normalizes and denormalizes xAPI statement timestamps.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class TimestampNormalizer implements DenormalizerInterface, NormalizerInterface
{
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
return new \DateTime($data);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'DateTime' === $type;
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!($object instanceof \DateTime || $object instanceof \DateTimeInterface)) {
throw new InvalidArgumentException(sprintf('Expected \DateTime object or object implementing \DateTimeInterface (got "%s").', is_object($object) ? get_class($object) : gettype($object)));
}
return $object->format('c');
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof \DateTime || $data instanceof \DateTimeInterface;
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
use Xabbuh\XApi\Model\IRI;
use Xabbuh\XApi\Model\Verb;
/**
* Denormalizes PHP arrays to {@link Verb} objects.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class VerbNormalizer extends Normalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof Verb) {
return;
}
$data = array(
'id' => $object->getId()->getValue(),
);
if (null !== $display = $object->getDisplay()) {
$data['display'] = $this->normalizeAttribute($display, $format, $context);
}
return $data;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof Verb;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$id = IRI::fromString($data['id']);
$display = null;
if (isset($data['display'])) {
$display = $this->denormalizeData($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
}
return new Verb($id, $display);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'Xabbuh\XApi\Model\Verb' === $type;
}
}

View File

@@ -0,0 +1,79 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\AccountNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\ActorNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\AttachmentNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\ContextActivitiesNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\ContextNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\DefinitionNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\DocumentDataNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\ExtensionsNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\FilterNullValueNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\InteractionComponentNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\LanguageMapNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\ObjectNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\ResultNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\StatementNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\StatementResultNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\TimestampNormalizer;
use Xabbuh\XApi\Serializer\Symfony\Normalizer\VerbNormalizer;
/**
* Entry point to set up the {@link \Symfony\Component\Serializer\Serializer Symfony Serializer component}
* for the Experience API.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
class Serializer
{
/**
* Creates a new Serializer.
*
* @return SerializerInterface The Serializer
*/
public static function createSerializer()
{
$normalizers = array(
new AccountNormalizer(),
new ActorNormalizer(),
new AttachmentNormalizer(),
new ContextNormalizer(),
new ContextActivitiesNormalizer(),
new DefinitionNormalizer(),
new DocumentDataNormalizer(),
new ExtensionsNormalizer(),
new InteractionComponentNormalizer(),
new LanguageMapNormalizer(),
new ObjectNormalizer(),
new ResultNormalizer(),
new StatementNormalizer(),
new StatementResultNormalizer(),
new TimestampNormalizer(),
new VerbNormalizer(),
new ArrayDenormalizer(),
new FilterNullValueNormalizer(new PropertyNormalizer()),
new PropertyNormalizer(),
);
$encoders = array(
new JsonEncoder(),
);
return new SymfonySerializer($normalizers, $encoders);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony;
use Symfony\Component\Serializer\SerializerInterface;
use Xabbuh\XApi\Serializer\SerializerFactoryInterface;
/**
* Creates serializer instances that use the Symfony Serializer component.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class SerializerFactory implements SerializerFactoryInterface
{
private $serializer;
public function __construct(SerializerInterface $serializer = null)
{
$this->serializer = $serializer ?: Serializer::createSerializer();
}
/**
* {@inheritdoc}
*/
public function createStatementSerializer()
{
return new StatementSerializer($this->serializer);
}
/**
* {@inheritdoc}
*/
public function createStatementResultSerializer()
{
return new StatementResultSerializer($this->serializer);
}
/**
* {@inheritdoc}
*/
public function createActorSerializer()
{
return new ActorSerializer($this->serializer);
}
/**
* {@inheritdoc}
*/
public function createDocumentDataSerializer()
{
return new DocumentDataSerializer($this->serializer);
}
}

View File

@@ -0,0 +1,57 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony;
use Symfony\Component\Serializer\SerializerInterface;
use Xabbuh\XApi\Model\StatementResult;
use Xabbuh\XApi\Serializer\StatementResultSerializerInterface;
/**
* Serializes and deserializes {@link StatementResult statement results} using the Symfony Serializer component.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class StatementResultSerializer implements StatementResultSerializerInterface
{
/**
* @var SerializerInterface The underlying serializer
*/
private $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
/**
* {@inheritDoc}
*/
public function serializeStatementResult(StatementResult $statementResult)
{
return $this->serializer->serialize($statementResult, 'json');
}
/**
* {@inheritDoc}
*/
public function deserializeStatementResult($data, array $attachments = array())
{
return $this->serializer->deserialize(
$data,
'Xabbuh\XApi\Model\StatementResult',
'json',
array(
'xapi_attachments' => $attachments,
)
);
}
}

View File

@@ -0,0 +1,80 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony;
use Symfony\Component\Serializer\SerializerInterface;
use Xabbuh\XApi\Model\Statement;
use Xabbuh\XApi\Serializer\StatementSerializerInterface;
/**
* Serializes and deserializes {@link Statement statements} using the Symfony Serializer component.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
final class StatementSerializer implements StatementSerializerInterface
{
/**
* @var SerializerInterface The underlying serializer
*/
private $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
/**
* {@inheritDoc}
*/
public function serializeStatement(Statement $statement)
{
return $this->serializer->serialize($statement, 'json');
}
/**
* {@inheritDoc}
*/
public function serializeStatements(array $statements)
{
return $this->serializer->serialize($statements, 'json');
}
/**
* {@inheritDoc}
*/
public function deserializeStatement($data, array $attachments = array())
{
return $this->serializer->deserialize(
$data,
'Xabbuh\XApi\Model\Statement',
'json',
array(
'xapi_attachments' => $attachments,
)
);
}
/**
* {@inheritDoc}
*/
public function deserializeStatements($data, array $attachments = array())
{
return $this->serializer->deserialize(
$data,
'Xabbuh\XApi\Model\Statement[]',
'json',
array(
'xapi_attachments' => $attachments,
)
);
}
}

View File

@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Tests;
use Xabbuh\XApi\Serializer\Symfony\ActorSerializer;
use Xabbuh\XApi\Serializer\Symfony\Serializer;
use Xabbuh\XApi\Serializer\Tests\ActorSerializerTest as BaseActorSerializerTest;
class ActorSerializerTest extends BaseActorSerializerTest
{
protected function createActorSerializer()
{
return new ActorSerializer(Serializer::createSerializer());
}
}

View File

@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Tests;
use Xabbuh\XApi\Serializer\Symfony\DocumentDataSerializer;
use Xabbuh\XApi\Serializer\Symfony\Serializer;
use Xabbuh\XApi\Serializer\Tests\DocumentDataSerializerTest as BaseDocumentDataSerializerTest;
class DocumentDataSerializerTest extends BaseDocumentDataSerializerTest
{
protected function createDocumentDataSerializer()
{
return new DocumentDataSerializer(Serializer::createSerializer());
}
}

View File

@@ -0,0 +1,533 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Tests;
use PHPUnit\Framework\TestCase;
use Xabbuh\XApi\Model\Account;
use Xabbuh\XApi\Model\Activity;
use Xabbuh\XApi\Model\Actor;
use Xabbuh\XApi\Model\Attachment;
use Xabbuh\XApi\Model\Context;
use Xabbuh\XApi\Model\ContextActivities;
use Xabbuh\XApi\Model\Definition;
use Xabbuh\XApi\Model\Extensions;
use Xabbuh\XApi\Model\Interaction\InteractionComponent;
use Xabbuh\XApi\Model\Result;
use Xabbuh\XApi\Model\Score;
use Xabbuh\XApi\Model\StatementReference;
use Xabbuh\XApi\Model\SubStatement;
use Xabbuh\XApi\Model\Verb;
use Xabbuh\XApi\Serializer\Symfony\Serializer;
class SerializerTest extends TestCase
{
private $serializer;
protected function setUp()
{
$this->serializer = Serializer::createSerializer();
}
/**
* @dataProvider serializeAccountData
*/
public function testSerializeAccount(Account $account, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($account, 'json'));
}
public function serializeAccountData()
{
return $this->buildSerializeTestCases('Account');
}
/**
* @dataProvider deserializeAccountData
*/
public function testDeserializeAccount($json, Account $expectedAccount)
{
$account = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Account', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\Account', $account);
$this->assertTrue($expectedAccount->equals($account), 'Deserialized account has the expected properties');
}
public function deserializeAccountData()
{
return $this->buildDeserializeTestCases('Account');
}
/**
* @dataProvider serializeActorData
*/
public function testSerializeActor(Actor $actor, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($actor, 'json'));
}
public function serializeActorData()
{
return $this->buildSerializeTestCases('Actor');
}
/**
* @dataProvider deserializeActorData
*/
public function testDeserializeActor($json, Actor $expectedActor)
{
$actor = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Actor', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\Actor', $actor);
$this->assertTrue($expectedActor->equals($actor), 'Deserialized actor has the expected properties');
}
public function deserializeActorData()
{
return $this->buildDeserializeTestCases('Actor');
}
/**
* @dataProvider serializeActivityData
*/
public function testSerializeActivity(Activity $activity, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($activity, 'json'));
}
public function serializeActivityData()
{
return $this->buildSerializeTestCases('Activity');
}
/**
* @dataProvider deserializeActivityData
*/
public function testDeserializeActivity($json, Activity $expectedActivity)
{
$activity = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Activity', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\Activity', $activity);
$this->assertTrue($expectedActivity->equals($activity), 'Deserialized activity has the expected properties');
}
public function deserializeActivityData()
{
return $this->buildDeserializeTestCases('Activity');
}
/**
* @dataProvider serializeAttachmentData
*/
public function testSerializeAttachment(Attachment $attachment, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($attachment, 'json'));
}
public function serializeAttachmentData()
{
$tests = array();
foreach (get_class_methods('Xabbuh\XApi\DataFixtures\AttachmentFixtures') as $method) {
if (false !== strpos($method, 'ForQuery')) {
continue;
}
$jsonFixture = json_decode(call_user_func(array('XApi\Fixtures\Json\AttachmentJsonFixtures', $method)));
$tests[$method] = array(
call_user_func(array('Xabbuh\XApi\DataFixtures\AttachmentFixtures', $method)),
json_encode($jsonFixture->metadata),
);
}
return $tests;
}
/**
* @dataProvider deserializeAttachmentData
*/
public function testDeserializeAttachment($json, $content, Attachment $expectedAttachment)
{
$context = array();
if (null !== $content) {
$context['xapi_attachments'] = array(
hash('sha256', $content) => array(
'content' => $content,
)
);
}
$attachment = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Attachment', 'json', $context);
$this->assertInstanceOf('Xabbuh\XApi\Model\Attachment', $attachment);
$this->assertTrue($expectedAttachment->equals($attachment), 'Deserialized attachment has the expected properties');
}
public function deserializeAttachmentData()
{
$tests = array();
foreach (get_class_methods('XApi\Fixtures\Json\AttachmentJsonFixtures') as $method) {
$jsonFixture = json_decode(call_user_func(array('XApi\Fixtures\Json\AttachmentJsonFixtures', $method)));
$tests[$method] = array(
json_encode($jsonFixture->metadata),
isset($jsonFixture->content) ? $jsonFixture->content : null,
call_user_func(array('Xabbuh\XApi\DataFixtures\AttachmentFixtures', $method)),
);
}
return $tests;
}
/**
* @dataProvider serializeContextData
*/
public function testSerializeContext(Context $context, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($context, 'json'));
}
public function serializeContextData()
{
return $this->buildSerializeTestCases('Context');
}
/**
* @dataProvider deserializeContextData
*/
public function testDeserializeContext($json, Context $expectedContext)
{
$context = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Context', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\Context', $context);
$this->assertTrue($expectedContext == $context, 'Deserialized context has the expected properties');
}
public function deserializeContextData()
{
return $this->buildDeserializeTestCases('Context');
}
/**
* @dataProvider serializeContextActivitiesData
*/
public function testSerializeContextActivities(ContextActivities $contextActivities, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($contextActivities, 'json'));
}
public function serializeContextActivitiesData()
{
return $this->buildSerializeTestCases('ContextActivities');
}
/**
* @dataProvider deserializeContextActivitiesData
*/
public function testDeserializeContextActivities($json, ContextActivities $expectedContextActivities)
{
$contextActivities = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\ContextActivities', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\ContextActivities', $contextActivities);
$this->assertTrue($expectedContextActivities == $contextActivities, 'Deserialized context activities have the expected properties');
}
public function deserializeContextActivitiesData()
{
return $this->buildDeserializeTestCases('ContextActivities');
}
/**
* @dataProvider serializeDefinitionData
*/
public function testSerializeDefinition(Definition $definition, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($definition, 'json'));
}
public function serializeDefinitionData()
{
return $this->buildSerializeTestCases('Definition');
}
/**
* @dataProvider deserializeDefinitionData
*/
public function testDeserializeDefinition($json, Definition $expectedDefinition)
{
$expectedClass = get_class($expectedDefinition);
$definition = $this->serializer->deserialize($json, $expectedClass, 'json');
$this->assertSame($expectedClass, get_class($definition), sprintf('Deserialized definition is an instance of "%s"', $expectedClass));
$this->assertTrue($expectedDefinition->equals($definition), 'Deserialized definition has the expected properties');
}
public function deserializeDefinitionData()
{
return $this->buildDeserializeTestCases('Definition');
}
/**
* @dataProvider serializeExtensionsData
*/
public function testSerializeExtensions(Extensions $extensions, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($extensions, 'json'));
}
public function serializeExtensionsData()
{
return $this->buildSerializeTestCases('Extensions');
}
/**
* @dataProvider deserializeExtensionsData
*/
public function testDeserializeExtensions($json, Extensions $expectedExtensions)
{
$extensions = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Extensions', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\Extensions', $extensions);
$this->assertTrue($expectedExtensions->equals($expectedExtensions), 'Deserialized extensions have the expected properties');
}
public function deserializeExtensionsData()
{
return $this->buildDeserializeTestCases('Extensions');
}
/**
* @dataProvider serializeInteractionComponentData
*/
public function testSerializeInteractionComponent(InteractionComponent $interactionComponent, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($interactionComponent, 'json'));
}
public function serializeInteractionComponentData()
{
return $this->buildSerializeTestCases('InteractionComponent');
}
/**
* @dataProvider deserializeInteractionComponentData
*/
public function testDeserializeInteractionComponent($json, InteractionComponent $expectedInteractionComponent)
{
$interactionComponent = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Interaction\InteractionComponent', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\Interaction\InteractionComponent', $interactionComponent);
$this->assertTrue($expectedInteractionComponent->equals($interactionComponent), 'Deserialized interaction component has the expected properties');
}
public function deserializeInteractionComponentData()
{
return $this->buildDeserializeTestCases('InteractionComponent');
}
/**
* @dataProvider serializeResultData
*/
public function testSerializeResult(Result $result, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($result, 'json'));
}
public function serializeResultData()
{
return $this->buildSerializeTestCases('Result');
}
/**
* @dataProvider deserializeResultData
*/
public function testDeserializeResult($json, Result $expectedResult)
{
$result = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Result', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\Result', $result);
$this->assertTrue($expectedResult->equals($result), 'Deserialized result has the expected properties');
}
public function deserializeResultData()
{
return $this->buildDeserializeTestCases('Result');
}
/**
* @dataProvider serializeScoreData
*/
public function testSerializeScore(Score $score, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($score, 'json'));
}
public function serializeScoreData()
{
return $this->buildSerializeTestCases('Score');
}
/**
* @dataProvider deserializeScoreData
*/
public function testDeserializeScore($json, Score $expectedScore)
{
$score = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Score', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\Score', $score);
$this->assertTrue($expectedScore->equals($score), 'Deserialized score has the expected properties');
}
public function deserializeScoreData()
{
return $this->buildDeserializeTestCases('Score');
}
/**
* @dataProvider serializeStatementReferenceData
*/
public function testSerializeStatementReference(StatementReference $statementReference, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($statementReference, 'json'));
}
public function serializeStatementReferenceData()
{
return $this->buildSerializeTestCases('StatementReference');
}
/**
* @dataProvider deserializeStatementReferenceData
*/
public function testDeserializeStatementReference($json, StatementReference $expectedStatementReference)
{
$statementReference = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\StatementReference', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\StatementReference', $statementReference);
$this->assertTrue($expectedStatementReference->equals($statementReference), 'Deserialized StatementReference has the expected properties');
}
public function deserializeStatementReferenceData()
{
return $this->buildDeserializeTestCases('StatementReference');
}
/**
* @dataProvider serializeSubStatementData
*/
public function testSerializeSubStatement(SubStatement $subStatement, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($subStatement, 'json'));
}
public function serializeSubStatementData()
{
return $this->buildSerializeTestCases('SubStatement');
}
/**
* @dataProvider deserializeSubStatementData
*/
public function testDeserializeSubStatement($json, SubStatement $expectedSubStatement)
{
$subStatement = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\SubStatement', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\SubStatement', $subStatement);
$this->assertTrue($expectedSubStatement->equals($subStatement), 'Deserialized SubStatement has the expected properties');
}
public function deserializeSubStatementData()
{
return $this->buildDeserializeTestCases('SubStatement');
}
/**
* @dataProvider serializeVerbData
*/
public function testSerializeVerb(Verb $verb, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->serializer->serialize($verb, 'json'));
}
public function serializeVerbData()
{
return $this->buildSerializeTestCases('Verb');
}
/**
* @dataProvider deserializeVerbData
*/
public function testDeserializeVerb($json, Verb $expectedVerb)
{
$verb = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Verb', 'json');
$this->assertInstanceOf('Xabbuh\XApi\Model\Verb', $verb);
$this->assertTrue($expectedVerb->equals($verb), 'Deserialized verb has the expected properties');
}
public function deserializeVerbData()
{
return $this->buildDeserializeTestCases('Verb');
}
private function buildSerializeTestCases($objectType)
{
$tests = array();
$phpFixturesClass = 'Xabbuh\XApi\DataFixtures\\'.$objectType.'Fixtures';
$jsonFixturesClass = 'XApi\Fixtures\Json\\'.$objectType.'JsonFixtures';
$jsonFixturesMethods = get_class_methods($jsonFixturesClass);
foreach (get_class_methods($phpFixturesClass) as $method) {
if (false !== strpos($method, 'ForQuery')) {
continue;
}
// serialized data will always contain type information
if (in_array($method.'WithType', $jsonFixturesMethods)) {
$jsonMethod = $method.'WithType';
} else {
$jsonMethod = $method;
}
$tests[$method] = array(
call_user_func(array($phpFixturesClass, $method)),
call_user_func(array($jsonFixturesClass, $jsonMethod)),
);
}
return $tests;
}
private function buildDeserializeTestCases($objectType)
{
$tests = array();
$jsonFixturesClass = 'XApi\Fixtures\Json\\'.$objectType.'JsonFixtures';
$phpFixturesClass = 'Xabbuh\XApi\DataFixtures\\'.$objectType.'Fixtures';
foreach (get_class_methods($jsonFixturesClass) as $method) {
// PHP objects do not contain the type information as a dedicated property
if ('WithType' === substr($method, -8)) {
continue;
}
$tests[$method] = array(
call_user_func(array($jsonFixturesClass, $method)),
call_user_func(array($phpFixturesClass, $method)),
);
}
return $tests;
}
}

View File

@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Tests;
use Xabbuh\XApi\Serializer\Symfony\Serializer;
use Xabbuh\XApi\Serializer\Symfony\StatementResultSerializer;
use Xabbuh\XApi\Serializer\Tests\StatementResultSerializerTest as BaseStatementResultSerializerTest;
class StatementResultSerializerTest extends BaseStatementResultSerializerTest
{
protected function createStatementResultSerializer()
{
return new StatementResultSerializer(Serializer::createSerializer());
}
}

View File

@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Symfony\Tests;
use Xabbuh\XApi\Serializer\Symfony\Serializer;
use Xabbuh\XApi\Serializer\Symfony\StatementSerializer;
use Xabbuh\XApi\Serializer\Tests\StatementSerializerTest as BaseStatementSerializerTest;
class StatementSerializerTest extends BaseStatementSerializerTest
{
protected function createStatementSerializer()
{
return new StatementSerializer(Serializer::createSerializer());
}
}