Skip to content

Commit

Permalink
Implemented ArrayAccess, AggregateIterator and Countable on `Ar…
Browse files Browse the repository at this point in the history
…rayOfRealItemsType`, `FindItemParentType` and

 `GroupedItemsType` to let you iterate over them directly. This should allow you to use responses more effectively and
 access other data that was previous lost (such as whether or not the responses returned were paginated). This is a breaking
 change because `getCalendarItems()` now returned a `FindItemParentType` object which, while you can use as an array,
 will return false on is_array()
  • Loading branch information
Garethp committed Dec 4, 2015
1 parent da29314 commit 61f6fc0
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 13 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.7.0 - 2015-12-*
* Implemented `ArrayAccess`, `AggregateIterator` and `Countable` on `ArrayOfRealItemsType`, `FindItemParentType` and
`GroupedItemsType` to let you iterate over them directly. This should allow you to use responses more effectively and
access other data that was previous lost (such as whether or not the responses returned were paginated). This is a breaking
change because `getCalendarItems()` now returned a `FindItemParentType` object which, while you can use as an array,
will return false on is_array()

## 0.6.7 - 2015-12-04
* Adding an option to get the raw responses from requests instead of having them drilled down automatically

Expand Down
107 changes: 106 additions & 1 deletion src/API/Type/ArrayOfRealItemsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* @method PostItemType[] getPostItem()
* @method ArrayOfRealItemsType setPostItem(array $postItem)
*/
class ArrayOfRealItemsType extends Type
class ArrayOfRealItemsType extends Type implements \Countable, \ArrayAccess, \IteratorAggregate
{

/**
Expand Down Expand Up @@ -101,4 +101,109 @@ class ArrayOfRealItemsType extends Type
* @var \jamesiarmes\PEWS\API\Type\PostItemType[]
*/
protected $postItem = null;

private $itemsArray;

public function getItems()
{
if ($this->itemsArray) {
return $this->itemsArray;
}

$items = null;

if ($this->item) {
$items = $this->item;
}

if ($this->message) {
$items = $this->message;
}

if ($this->calendarItem) {
$items = $this->calendarItem;
}

if ($this->contact) {
$items = $this->contact;
}

if ($this->distributionList) {
$items = $this->distributionList;
}

if ($this->meetingMessage) {
$items = $this->meetingMessage;
}

if ($this->meetingRequest) {
$items = $this->meetingRequest;
}

if ($this->meetingResponse) {
$items = $this->meetingResponse;
}

if ($this->meetingCancellation) {
$items = $this->meetingCancellation;
}

if ($this->task) {
$items = $this->task;
}

if ($this->postItem) {
$items = $this->postItem;
}

if ($items === null) {
$items = array();
}

if (!is_array($items)) {
$items = array($items);
}

$this->itemsArray = $items;
return $this->itemsArray;
}

public function count()
{
return count($this->getItems());
}

public function offsetExists($offset)
{
return isset($this->getItems()[$offset]);
}

public function offsetGet($offset)
{
$this->getItems();
return isset($this->itemsArray[$offset]) ? $this->itemsArray[$offset] : null;
}

public function offsetSet($offset, $value)
{
$this->getItems();

if (is_null($offset)) {
$this->itemsArray[] = $value;
} else {
$this->itemsArray[$offset] = $value;
}
}

public function offsetUnset($offset)
{
$this->getItems();
unset($this->itemsArray[$offset]);
}

public function getIterator()
{
$this->getItems();
return new \ArrayIterator($this->itemsArray);
}
}
42 changes: 41 additions & 1 deletion src/API/Type/FindItemParentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @method GroupedItemsType[] getGroups()
* @method FindItemParentType setGroups(array $groups)
*/
class FindItemParentType extends Type
class FindItemParentType extends Type implements \Countable, \ArrayAccess, \IteratorAggregate
{

/**
Expand Down Expand Up @@ -64,4 +64,44 @@ class FindItemParentType extends Type
* @var \jamesiarmes\PEWS\API\Type\GroupedItemsType[]
*/
protected $groups = null;

public function count()
{
return count($this->items);
}

public function offsetExists($offset)
{
$arrayAccessName = ($this->items != null ? 'items' : 'groups');
return isset($this->{$arrayAccessName}[$offset]);
}

public function offsetGet($offset)
{
$arrayAccessName = ($this->items != null ? 'items' : 'groups');
return isset($this->{$arrayAccessName}[$offset]) ? $this->{$arrayAccessName}[$offset] : null;
}

public function offsetSet($offset, $value)
{
$arrayAccessName = ($this->items != null ? 'items' : 'groups');

if (is_null($offset)) {
array_push($this->{$arrayAccessName}, $value);
} else {
$this->{$arrayAccessName}[$offset] = $value;
}
}

public function offsetUnset($offset)
{
$arrayAccessName = ($this->items != null ? 'items' : 'groups');
unset($this->{$arrayAccessName}[$offset]);
}

public function getIterator()
{
$arrayAccessName = ($this->items != null ? 'items' : 'groups');
return new \ArrayIterator($this->{$arrayAccessName}->getIterator());
}
}
36 changes: 35 additions & 1 deletion src/API/Type/GroupedItemsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @method ArrayOfRealItemsType getItems()
* @method GroupedItemsType setItems(ArrayOfRealItemsType $items)
*/
class GroupedItemsType extends Type
class GroupedItemsType extends Type implements \Countable, \ArrayAccess, \IteratorAggregate
{

/**
Expand All @@ -27,4 +27,38 @@ class GroupedItemsType extends Type
* @var \jamesiarmes\PEWS\API\Type\ArrayOfRealItemsType
*/
protected $items = null;

public function count()
{
return count($this->items);
}

public function offsetExists($offset)
{
return isset($this->items[$offset]);
}

public function offsetGet($offset)
{
return isset($this->items[$offset]) ? $this->items[$offset] : null;
}

public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
}

public function offsetUnset($offset)
{
unset($this->items[$offset]);
}

public function getIterator()
{
return new \ArrayIterator($this->items->getIterator());
}
}
11 changes: 2 additions & 9 deletions src/Calendar/CalendarAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function createCalendarItems($items, $options = array())
* @param string|DateTime $start
* @param string|DateTime $end
* @param array $options
* @return CalendarItemType[]
* @return CalendarItemType[]|Type\FindItemParentType
*/
public function getCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array())
{
Expand Down Expand Up @@ -123,14 +123,7 @@ public function getCalendarItems($start = '12:00 AM', $end = '11:59 PM', $option

$request = Type::buildFromArray($request);
$response = $this->getClient()->FindItem($request);
$items = $response->getItems()->getCalendarItem();
if ($items == null) {
return array();
}

if (!is_array($items)) {
$items = array($items);
}
$items = $response;

return $items;
}
Expand Down
1 change: 0 additions & 1 deletion tests/src/Calendar/CalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function testGetCalendarItems()
$client = $this->getClient();
$items = $client->getCalendarItems('2015-07-01 00:00', '2015-07-01 23:59');

$this->assertTrue(is_array($items));
$this->assertCount(0, $items);
}

Expand Down

0 comments on commit 61f6fc0

Please sign in to comment.