Completed
Pull Request — master (#9)
by Abdelouahab
01:46
created

ObjectNormalizer::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
13
14
use Xabbuh\XApi\Model\Activity;
15
use Xabbuh\XApi\Model\IRI;
16
use Xabbuh\XApi\Model\StatementObject;
17
use Xabbuh\XApi\Model\Statement;
18
use Xabbuh\XApi\Model\StatementId;
19
use Xabbuh\XApi\Model\StatementReference;
20
use Xabbuh\XApi\Model\SubStatement;
21
22
/**
23
 * Normalizes and denormalizes xAPI statement objects.
24
 *
25
 * @author Christian Flothmann <[email protected]>
26
 */
27
final class ObjectNormalizer extends Normalizer
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function normalize($object, $format = null, array $context = array())
33
    {
34
        if ($object instanceof Activity) {
35
            $activityData = array(
36
                'objectType' => 'Activity',
37
                'id' => $object->getId()->getValue(),
38
            );
39
40
            if (null !== $definition = $object->getDefinition()) {
41
                $activityData['definition'] = $this->normalizeAttribute($definition, $format, $context);
42
            }
43
44
            return $activityData;
45
        }
46
47
        if ($object instanceof StatementReference) {
48
            return array(
49
                'objectType' => 'StatementRef',
50
                'id' => $object->getStatementId()->getValue(),
51
            );
52
        }
53
54
        if ($object instanceof SubStatement) {
55
            $data = array(
56
                'objectType' => 'SubStatement',
57
                'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
58
                'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
59
                'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
60
            );
61
62
            if (null !== $result = $object->getResult()) {
63
                $data['result'] = $this->normalizeAttribute($result, $format, $context);
64
            }
65
66
            if (null !== $statementContext = $object->getContext()) {
67
                $data['context'] = $this->normalizeAttribute($statementContext, $format, $context);
68
            }
69
70
            return $data;
71
        }
72
73
        return null;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function supportsNormalization($data, $format = null)
80
    {
81
        return $data instanceof Object || $data instanceof StatementObject;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function denormalize($data, $class, $format = null, array $context = array())
88
    {
89
        if (!isset($data['objectType']) || 'Activity' === $data['objectType']) {
90
            return $this->denormalizeActivity($data, $format, $context);
91
        }
92
93
        if (isset($data['objectType']) && ('Agent' === $data['objectType'] || 'Group' === $data['objectType'])) {
94
            return $this->denormalizeData($data, 'Xabbuh\XApi\Model\Actor', $format, $context);
95
        }
96
97
        if (isset($data['objectType']) && 'SubStatement' === $data['objectType']) {
98
            return $this->denormalizeSubStatement($data, $format, $context);
99
        }
100
101
        if (isset($data['objectType']) && 'StatementRef' === $data['objectType']) {
102
            return new StatementReference(StatementId::fromString($data['id']));
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function supportsDenormalization($data, $type, $format = null)
112
    {
113
        return 'Xabbuh\XApi\Model\Activity' === $type || 'Xabbuh\XApi\Model\StatementObject' === $type || 'Xabbuh\XApi\Model\Object' === $type || 'Xabbuh\XApi\Model\StatementReference' === $type || 'Xabbuh\XApi\Model\SubStatement' === $type;
114
    }
115
116
    private function denormalizeActivity(array $data, $format = null, array $context = array())
117
    {
118
        $definition = null;
119
120
        if (isset($data['definition'])) {
121
            $definition = $this->denormalizeData($data['definition'], 'Xabbuh\XApi\Model\Definition', $format, $context);
122
        }
123
124
        return new Activity(IRI::fromString($data['id']), $definition);
125
    }
126
127
    private function denormalizeSubStatement(array  $data, $format = null, array $context = array())
128
    {
129
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
130
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
131
        if (class_exists('Xabbuh\XApi\Model\Object')) {
132
        $object = (class_exists('Xabbuh\XApi\Model\Object'))?$this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context):$this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\StatementObject', $format, $context);
133
        $result = null;
134
        $statementContext = null;
135
136
        if (isset($data['result'])) {
137
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
138
        }
139
140
        if (isset($data['context'])) {
141
            $statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context);
142
        }
143
144
        return new SubStatement($actor, $verb, $object, $result, $statementContext);
145
    }
146
}
147
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected EOF, expecting T_FUNCTION or T_CONST
Loading history...