Skip to content

Commit dbfed05

Browse files
committed
implement the state API
1 parent c4614fc commit dbfed05

File tree

8 files changed

+303
-10
lines changed

8 files changed

+303
-10
lines changed

Model/State.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\Common\Model;
13+
14+
/**
15+
* An activity provider's state stored on a remote LRS.
16+
*
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
class State implements StateInterface
20+
{
21+
/**
22+
* @var ActivityInterface The associated activity
23+
*/
24+
protected $activity;
25+
26+
/**
27+
* @var ActorInterface The associated actor
28+
*/
29+
protected $actor;
30+
31+
/**
32+
* @var string An optional registration id
33+
*/
34+
protected $registrationId;
35+
36+
/**
37+
* @var string The state id
38+
*/
39+
protected $stateId;
40+
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function setActivity(ActivityInterface $activity)
45+
{
46+
$this->activity = $activity;
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function getActivity()
53+
{
54+
return $this->activity;
55+
}
56+
57+
/**
58+
* {@inheritDoc}
59+
*/
60+
public function setActor(ActorInterface $actor)
61+
{
62+
$this->actor = $actor;
63+
}
64+
65+
/**
66+
* {@inheritDoc}
67+
*/
68+
public function getActor()
69+
{
70+
return $this->actor;
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*/
76+
public function setRegistrationId($registrationId)
77+
{
78+
$this->registrationId = $registrationId;
79+
}
80+
81+
/**
82+
* {@inheritDoc}
83+
*/
84+
public function getRegistrationId()
85+
{
86+
return $this->registrationId;
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*/
92+
public function setStateId($stateId)
93+
{
94+
$this->stateId = $stateId;
95+
}
96+
97+
/**
98+
* {@inheritDoc}
99+
*/
100+
public function getStateId()
101+
{
102+
return $this->stateId;
103+
}
104+
}

Model/StateDocument.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Common\Model;
13+
14+
/**
15+
* A document associated to an activity provider state.
16+
*
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
class StateDocument extends Document implements StateDocumentInterface
20+
{
21+
/**
22+
* @var StateInterface The state the document is associated to
23+
*/
24+
protected $state;
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function setState(StateInterface $state)
30+
{
31+
$this->state = $state;
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function getState()
38+
{
39+
return $this->state;
40+
}
41+
}

Model/StateDocumentInterface.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Common\Model;
13+
14+
/**
15+
* A document associated to an activity provider state.
16+
*
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
interface StateDocumentInterface extends DocumentInterface
20+
{
21+
/**
22+
* Sets the document's state.
23+
*
24+
* @param StateInterface $state The state
25+
*/
26+
public function setState(StateInterface $state);
27+
28+
/**
29+
* Returns the document's state.
30+
*
31+
* @return StateInterface The state
32+
*/
33+
public function getState();
34+
}

Model/StateInterface.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Common\Model;
13+
14+
/**
15+
* An activity provider's state stored on a remote LRS.
16+
*
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
interface StateInterface
20+
{
21+
/**
22+
* Sets the activity.
23+
*
24+
* @param ActivityInterface $activity The activity
25+
*/
26+
public function setActivity(ActivityInterface $activity);
27+
28+
/**
29+
* Returns the activity.
30+
*
31+
* @return ActivityInterface The activity
32+
*/
33+
public function getActivity();
34+
35+
/**
36+
* Sets the actor.
37+
*
38+
* @param ActorInterface $actor The actor
39+
*/
40+
public function setActor(ActorInterface $actor);
41+
42+
/**
43+
* Returns the actor.
44+
*
45+
* @return ActorInterface The actor
46+
*/
47+
public function getActor();
48+
49+
/**
50+
* Sets the optional registration id.
51+
*
52+
* @param string $registrationId The registration id
53+
*/
54+
public function setRegistrationId($registrationId);
55+
56+
/**
57+
* Returns the registration id.
58+
*
59+
* @return string The registration id
60+
*/
61+
public function getRegistrationId();
62+
63+
/**
64+
* Sets the state's id.
65+
*
66+
* @param string $stateId The id
67+
*/
68+
public function setStateId($stateId);
69+
70+
/**
71+
* Returns the state's id.
72+
*
73+
* @return string The id
74+
*/
75+
public function getStateId();
76+
}

Serializer/Event/DocumentDataWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function wrapData(PreDeserializeEvent $event)
3838
{
3939
$type = $event->getType();
4040

41-
if ('Xabbuh\XApi\Common\Model\Document' === $type['name']) {
41+
if (is_subclass_of($type['name'], 'Xabbuh\XApi\Common\Model\DocumentInterface')) {
4242
$data = $event->getData();
4343
$event->setData(array('data' => $data));
4444
}

Serializer/Handler/DocumentDataUnwrapper.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ class DocumentDataUnwrapper implements SubscribingHandlerInterface
2929
*/
3030
public static function getSubscribingMethods()
3131
{
32-
return array(array(
33-
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
34-
'format' => 'json',
35-
'type' => 'Xabbuh\XApi\Common\Model\Document',
36-
'method' => 'unwrapData',
37-
));
32+
return array(
33+
array(
34+
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
35+
'format' => 'json',
36+
'type' => 'Xabbuh\XApi\Common\Model\Document',
37+
'method' => 'unwrapData',
38+
),
39+
array(
40+
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
41+
'format' => 'json',
42+
'type' => 'Xabbuh\XApi\Common\Model\StateDocument',
43+
'method' => 'unwrapData',
44+
),
45+
);
3846
}
3947

4048
public function unwrapData(JsonSerializationVisitor $visitor, Document $document, array $type, Context $context)

Tests/Model/DocumentTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DocumentTest extends ModelTest
2020
{
2121
public function testArrayAccessMethods()
2222
{
23-
$document = new Document();
23+
$document = $this->createDocument();
2424

2525
$this->assertFalse(isset($document['x']));
2626

@@ -40,7 +40,7 @@ public function testArrayAccessMethods()
4040

4141
public function testGetData()
4242
{
43-
$document = new Document();
43+
$document = $this->createDocument();
4444
$document['x'] = 'foo';
4545
$document['y'] = 'bar';
4646

@@ -58,10 +58,15 @@ public function testDeserializeDocument()
5858

5959
public function testSerializeDocument()
6060
{
61-
$document = new Document();
61+
$document = $this->createDocument();
6262
$document['x'] = 'foo';
6363
$document['y'] = 'bar';
6464

6565
$this->serializeAndValidateData($this->loadFixture('document'), $document);
6666
}
67+
68+
protected function createDocument()
69+
{
70+
return new Document();
71+
}
6772
}

Tests/Model/StateDocumentTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Common\Tests\Model;
13+
14+
use Xabbuh\XApi\Common\Model\StateDocument;
15+
16+
/**
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
class StateDocumentTest extends DocumentTest
20+
{
21+
protected function createDocument()
22+
{
23+
return new StateDocument();
24+
}
25+
}

0 commit comments

Comments
 (0)