1
|
|
|
<?php |
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\ContextActivities; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Normalizes and denormalizes xAPI statement context activities. |
18
|
|
|
* |
19
|
|
|
* @author Christian Flothmann <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class ContextActivitiesNormalizer extends Normalizer |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function normalize($object, $format = null, array $context = array()) |
27
|
|
|
{ |
28
|
|
|
if (!$object instanceof ContextActivities) { |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$data = array(); |
33
|
|
|
|
34
|
|
|
if (null !== $categoryActivities = $object->getCategoryActivities()) { |
35
|
|
|
$data['category'] = $this->normalizeAttribute($categoryActivities); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (null !== $parentActivities = $object->getParentActivities()) { |
39
|
|
|
$data['parent'] = $this->normalizeAttribute($parentActivities); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (null !== $groupingActivities = $object->getGroupingActivities()) { |
43
|
|
|
$data['grouping'] = $this->normalizeAttribute($groupingActivities); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (null !== $otherActivities = $object->getOtherActivities()) { |
47
|
|
|
$data['other'] = $this->normalizeAttribute($otherActivities); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (empty($data)) { |
51
|
|
|
return new \stdClass(); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $data; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function supportsNormalization($data, $format = null) |
61
|
|
|
{ |
62
|
|
|
return $data instanceof ContextActivities; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
69
|
|
|
{ |
70
|
|
|
$parentActivities = null; |
71
|
|
|
$groupingActivities = null; |
72
|
|
|
$categoryActivities = null; |
73
|
|
|
$otherActivities = null; |
74
|
|
|
|
75
|
|
View Code Duplication |
if (isset($data['parent']) && null !== $data['parent']) { |
|
|
|
|
76
|
|
|
$parentActivities = $this->denormalizeData($data['parent'], 'Xabbuh\XApi\Model\Activity[]', $format, $context); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
if (isset($data['grouping']) && null !== $data['grouping']) { |
|
|
|
|
80
|
|
|
$groupingActivities = $this->denormalizeData($data['grouping'], 'Xabbuh\XApi\Model\Activity[]', $format, $context); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
if (isset($data['category']) && null !== $data['category']) { |
|
|
|
|
84
|
|
|
$categoryActivities = $this->denormalizeData($data['category'], 'Xabbuh\XApi\Model\Activity[]', $format, $context); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
if (isset($data['other']) && null !== $data['other']) { |
|
|
|
|
88
|
|
|
$otherActivities = $this->denormalizeData($data['other'], 'Xabbuh\XApi\Model\Activity[]', $format, $context); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return new ContextActivities($parentActivities, $groupingActivities, $categoryActivities, $otherActivities); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
98
|
|
|
{ |
99
|
|
|
return 'Xabbuh\XApi\Model\ContextActivities' === $type; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.