Skip to content

Commit

Permalink
Setters for reference_time and timezone in Context.
Browse files Browse the repository at this point in the history
Also the serialization methods returns stdClass to enforce a json object in case the context is empty.
  • Loading branch information
hfinck committed Aug 15, 2016
1 parent 67cb679 commit bb73e19
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
21 changes: 8 additions & 13 deletions spec/Model/ContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace spec\Tgallice\Wit\Model;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Tgallice\Wit\Model\Entity;
use Tgallice\Wit\Model\Location;

Expand All @@ -21,13 +20,9 @@ function it_has_no_default_reference_time()

function it_can_define_a_reference_time()
{
$date = new \DateTime();
$asString = $date->format(DATE_ISO8601);

$this->beConstructedWith([
'reference_time' => $asString,
]);
$this->getReferenceTime()->shouldReturn($asString);
$dt = new \DateTimeImmutable();
$this->setReferenceTime($dt);
$this->getReferenceTime()->shouldReturn($dt->format(DATE_ISO8601));
}

function it_has_no_default_location()
Expand Down Expand Up @@ -74,12 +69,11 @@ function it_has_no_default_timezone()
$this->getTimezone()->shouldReturn(null);
}

function it_can_define_timezone()
function it_can_define_a_timezone()
{
$this->beConstructedWith([
'timezone' => 'europe/paris',
]);
$this->getTimezone()->shouldReturn('europe/paris');
$timezone = 'Europe/Paris';
$this->setTimezone($timezone);
$this->getTimezone()->shouldReturn($timezone);
}

function it_can_add_custom_context_field()
Expand Down Expand Up @@ -113,6 +107,7 @@ function it_can_check_if_context_is_empty()
function it_must_be_json_serializable()
{
$this->shouldHaveType(\JsonSerializable::class);
$this->add('custom', 'value');
$serialized = json_encode($this->getWrappedObject());
$this->jsonSerialize()->shouldReturn(json_decode($serialized, true));
}
Expand Down
23 changes: 21 additions & 2 deletions src/Model/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Context implements \JsonSerializable
*/
public function __construct($data = [])
{

$this->data = $data;
}

Expand All @@ -35,7 +34,7 @@ public function getLocation()
}

/**
* Return the reference date in the ISO8601 format
* Return the reference date in ISO8601 format
*
* @return string|null
*/
Expand All @@ -44,6 +43,17 @@ public function getReferenceTime()
return $this->getContextField('reference_time');
}

/**
* Set the reference time in ISO8601 format
*
* @param \DateTimeInterface $dateTime
*/
public function setReferenceTime(\DateTimeInterface $dateTime)
{
$dt = $dateTime->format(DATE_ISO8601);
$this->add('reference_time', $dt);
}

/**
* @return array|string|null
*/
Expand All @@ -60,6 +70,15 @@ public function getTimezone()
return $this->getContextField('timezone');
}

/**
* @param string $timezone Timezone identifier e.g 'Europe/Berlin'
*/
public function setTimezone($timezone)
{
$tz = new \DateTimeZone($timezone);
$this->add('timezone', $tz->getName());
}

/**
* @param string $name
* @param mixed $value
Expand Down

0 comments on commit bb73e19

Please sign in to comment.