1111
1212namespace Xabbuh \XApi \Serializer \Normalizer ;
1313
14- use Xabbuh \XApi \Model \Account ;
1514use Xabbuh \XApi \Model \Actor ;
1615use Xabbuh \XApi \Model \Agent ;
1716use Xabbuh \XApi \Model \Group ;
17+ use Xabbuh \XApi \Model \InverseFunctionalIdentifier ;
1818
1919/**
2020 * Normalizes and denormalizes xAPI statement actors.
@@ -34,21 +34,7 @@ public function normalize($object, $format = null, array $context = array())
3434
3535 $ data = array ();
3636
37- if (null !== $ mbox = $ object ->getMbox ()) {
38- $ data ['mbox ' ] = $ mbox ;
39- }
40-
41- if (null !== $ mboxSha1Sum = $ object ->getMboxSha1Sum ()) {
42- $ data ['mbox_sha1sum ' ] = $ mboxSha1Sum ;
43- }
44-
45- if (null !== $ openId = $ object ->getOpenId ()) {
46- $ data ['openid ' ] = $ openId ;
47- }
48-
49- if (null !== $ account = $ object ->getAccount ()) {
50- $ data ['account ' ] = $ this ->normalizeAttribute ($ account );
51- }
37+ $ this ->normalizeInverseFunctionalIdentifier ($ object ->getInverseFunctionalIdentifier (), $ data );
5238
5339 if (null !== $ name = $ object ->getName ()) {
5440 $ data ['name ' ] = $ name ;
@@ -82,17 +68,14 @@ public function supportsNormalization($data, $format = null)
8268 */
8369 public function denormalize ($ data , $ class , $ format = null , array $ context = array ())
8470 {
85- $ mbox = isset ($ data ['mbox ' ]) ? $ data ['mbox ' ] : null ;
86- $ mboxSha1Sum = isset ($ data ['mboxSha1Sum ' ]) ? $ data ['mboxSha1Sum ' ] : null ;
87- $ openId = isset ($ data ['openid ' ]) ? $ data ['openid ' ] : null ;
71+ $ iri = $ this ->denormalizeInverseFunctionalIdentifier ($ data , $ format , $ context );
8872 $ name = isset ($ data ['name ' ]) ? $ data ['name ' ] : null ;
89- $ account = $ this ->denormalizeAccount ($ data , $ format , $ context );
9073
9174 if (isset ($ data ['objectType ' ]) && 'Group ' === $ data ['objectType ' ]) {
92- return $ this ->denormalizeGroup ($ mbox , $ mboxSha1Sum , $ openId , $ account , $ name , $ data , $ format , $ context );
75+ return $ this ->denormalizeGroup ($ iri , $ name , $ data , $ format , $ context );
9376 }
9477
95- return new Agent ($ mbox , $ mboxSha1Sum , $ openId , $ account , $ name );
78+ return new Agent ($ iri , $ name );
9679 }
9780
9881 /**
@@ -103,16 +86,58 @@ public function supportsDenormalization($data, $type, $format = null)
10386 return 'Xabbuh\XApi\Model\Actor ' === $ type ;
10487 }
10588
89+ private function normalizeInverseFunctionalIdentifier (InverseFunctionalIdentifier $ iri = null , &$ data )
90+ {
91+ if (null === $ iri ) {
92+ return ;
93+ }
94+
95+ if (null !== $ mbox = $ iri ->getMbox ()) {
96+ $ data ['mbox ' ] = $ mbox ;
97+ }
98+
99+ if (null !== $ mboxSha1Sum = $ iri ->getMboxSha1Sum ()) {
100+ $ data ['mbox_sha1sum ' ] = $ mboxSha1Sum ;
101+ }
102+
103+ if (null !== $ openId = $ iri ->getOpenId ()) {
104+ $ data ['openid ' ] = $ openId ;
105+ }
106+
107+ if (null !== $ account = $ iri ->getAccount ()) {
108+ $ data ['account ' ] = $ this ->normalizeAttribute ($ account );
109+ }
110+ }
111+
112+ private function denormalizeInverseFunctionalIdentifier ($ data , $ format = null , array $ context = array ())
113+ {
114+ if (isset ($ data ['mbox ' ])) {
115+ return InverseFunctionalIdentifier::withMbox ($ data ['mbox ' ]);
116+ }
117+
118+ if (isset ($ data ['mboxSha1Sum ' ])) {
119+ return InverseFunctionalIdentifier::withMboxSha1Sum ($ data ['mboxSha1Sum ' ]);
120+ }
121+
122+ if (isset ($ data ['openid ' ])) {
123+ return InverseFunctionalIdentifier::withOpenId ($ data ['openid ' ]);
124+ }
125+
126+ if (isset ($ data ['account ' ])) {
127+ return InverseFunctionalIdentifier::withAccount ($ this ->denormalizeAccount ($ data , $ format , $ context ));
128+ }
129+ }
130+
106131 private function denormalizeAccount ($ data , $ format = null , array $ context = array ())
107132 {
108133 if (!isset ($ data ['account ' ])) {
109134 return null ;
110135 }
111136
112- return $ this ->denormalizeData ($ data ['account ' ], 'Xabbuh\XApi\Model\Account ' , $ format ,$ context );
137+ return $ this ->denormalizeData ($ data ['account ' ], 'Xabbuh\XApi\Model\Account ' , $ format , $ context );
113138 }
114139
115- private function denormalizeGroup ($ mbox , $ mboxSha1Sum , $ openId , Account $ account = null , $ name , $ data , $ format = null , array $ context = array ())
140+ private function denormalizeGroup (InverseFunctionalIdentifier $ iri = null , $ name , $ data , $ format = null , array $ context = array ())
116141 {
117142 $ members = array ();
118143
@@ -122,6 +147,6 @@ private function denormalizeGroup($mbox, $mboxSha1Sum, $openId, Account $account
122147 }
123148 }
124149
125- return new Group ($ mbox , $ mboxSha1Sum , $ openId , $ account , $ name , $ members );
150+ return new Group ($ iri , $ name , $ members );
126151 }
127152}
0 commit comments