Skip to content

Add a StateDocumentsFilter class #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions spec/StateDocumentsFilterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 spec\Xabbuh\XApi\Model;

use PhpSpec\ObjectBehavior;
use Xabbuh\XApi\Model\Activity;
use Xabbuh\XApi\Model\Agent;
use Xabbuh\XApi\Model\InverseFunctionalIdentifier;
use Xabbuh\XApi\Model\IRI;
use Xabbuh\XApi\Model\LanguageMap;
use Xabbuh\XApi\Model\Verb;

class StateDocumentsFilterSpec extends ObjectBehavior
{
function it_does_not_filter_anything_by_default()
{
$filter = $this->getFilter();
$filter->shouldHaveCount(0);
}

function it_can_filter_by_activity()
{
$this->byActivity(new Activity(IRI::fromString('http://tincanapi.com/conformancetest/activityid')))->shouldReturn($this);

$filter = $this->getFilter();
$filter->shouldHaveCount(1);
$filter->shouldHaveKeyWithValue('activity', 'http://tincanapi.com/conformancetest/activityid');
}

function it_can_filter_by_agent()
{
$actor = new Agent(InverseFunctionalIdentifier::withMbox(IRI::fromString('mailto:conformancetest@tincanapi.com')));
$this->byAgent($actor)->shouldReturn($this);

$filter = $this->getFilter();
$filter->shouldHaveCount(1);
$filter->shouldHaveKeyWithValue('agent', $actor);
}

function it_can_filter_by_registration()
{
$this->byRegistration('foo')->shouldReturn($this);

$filter = $this->getFilter();
$filter->shouldHaveCount(1);
$filter->shouldHaveKeyWithValue('registration', 'foo');
}

function it_can_filter_by_timestamp()
{
$this->since(\DateTime::createFromFormat(\DateTime::ISO8601, '2013-05-18T05:32:34Z'))->shouldReturn($this);
$this->getFilter()->shouldHaveKeyWithValue('since', '2013-05-18T05:32:34+00:00');
}
}
91 changes: 91 additions & 0 deletions src/StateDocumentsFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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\Model;

/**
* Filter to apply on GET requests to the states API.
*
* @author Jérôme Parmentier <jerome.parmentier@acensi.fr>
*/
class StateDocumentsFilter
{
/**
* @var array The generated filter
*/
private $filter = array();

/**
* Filter by an Activity.
*
* @param Activity $activity The Activity to filter by
*
* @return $this
*/
public function byActivity(Activity $activity)
{
$this->filter['activity'] = $activity->getId()->getValue();

return $this;
}

/**
* Filters by an Agent.
*
* @param Agent $agent The Agent to filter by
*
* @return $this
*/
public function byAgent(Agent $agent)
{
$this->filter['agent'] = $agent;

return $this;
}

/**
* Filters for State documents matching the given registration id.
*
* @param string $registration A registration id
*
* @return $this
*/
public function byRegistration($registration)
{
$this->filter['registration'] = $registration;

return $this;
}

/**
* Filters for State documents stored since the specified timestamp (exclusive).
*
* @param \DateTime $timestamp The timestamp
*
* @return $this
*/
public function since(\DateTime $timestamp)
{
$this->filter['since'] = $timestamp->format('c');

return $this;
}

/**
* Returns the generated filter.
*
* @return array The filter
*/
public function getFilter()
{
return $this->filter;
}
}