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,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,
)
);
}
}