Skip to content

Commit

Permalink
Merge e472cae into 513b923
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanlaak committed Jun 15, 2021
2 parents 513b923 + e472cae commit a90f075
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 37 deletions.
43 changes: 41 additions & 2 deletions src/Micrometa/Ports/Item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Ports
*/
class Item extends ItemList implements ItemInterface
class Item implements ItemInterface, ItemListFacade
{
/**
* Application item
Expand All @@ -64,6 +64,11 @@ class Item extends ItemList implements ItemInterface
*/
protected $item;

/**
* @var ItemList
*/
private $itemList;

/**
* Item constructor
*
Expand All @@ -72,7 +77,7 @@ class Item extends ItemList implements ItemInterface
public function __construct(ApplicationItemInterface $item)
{
$this->item = $item;
parent::__construct(ItemFactory::createFromApplicationItems($this->item->getChildren()));
$this->itemList = new ItemList(ItemFactory::createFromApplicationItems($this->item->getChildren()));
}

/**
Expand Down Expand Up @@ -328,4 +333,38 @@ public function getValue()
{
return $this->item->getValue();
}

/**
* Filter the items by item type(s).
*
* @param array ...$types Item types
*
* @return ItemInterface[] Items matching the requested types
*/
public function getItems(...$types)
{
return $this->itemList->getItems(...$types);
}

/**
* Return the first item, optionally of particular types.
*
* @param array ...$types Item types
*
* @return ItemInterface Item
*/
public function getFirstItem(...$types)
{
return $this->itemList->getFirstItem(...$types);
}

/**
* Count the elements in the item list.
*
* @return int
*/
public function count()
{
return $this->itemList->count();
}
}
2 changes: 1 addition & 1 deletion src/Micrometa/Ports/Item/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Ports
*/
interface ItemInterface extends ItemListInterface
interface ItemInterface
{
/**
* Return whether the item is of a particular type (or contained in a list of types)
Expand Down
41 changes: 41 additions & 0 deletions src/Micrometa/Ports/Item/ItemListFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Jkphl\Micrometa\Ports\Item;

/**
* Hides all other methods that ItemListInterface inherits from PHP core.
*
* Prefer typehinting against this facade over the actual interface.
*
* @see ItemListInterface
*/
interface ItemListFacade extends \Countable
{
/**
* Return an object representation of the item list
*
* @return \stdClass Micro information items
* @api
*/
public function toObject();

/**
* Filter the items by item type(s)
*
* @param array ...$types Item types
*
* @return ItemInterface[] Items matching the requested types
* @api
*/
public function getItems(...$types);

/**
* Return the first item, optionally of particular types
*
* @param array ...$types Item types
*
* @return ItemInterface Item
* @api
*/
public function getFirstItem(...$types);
}
29 changes: 1 addition & 28 deletions src/Micrometa/Ports/Item/ItemListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,6 @@
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Ports
*/
interface ItemListInterface extends \Iterator, \Countable, \ArrayAccess
interface ItemListInterface extends ItemListFacade, \Iterator, \ArrayAccess
{
/**
* Return an object representation of the item list
*
* @return \stdClass Micro information items
* @api
*/
public function toObject();

/**
* Filter the items by item type(s)
*
* @param array ...$types Item types
*
* @return ItemInterface[] Items matching the requested types
* @api
*/
public function getItems(...$types);

/**
* Return the first item, optionally of particular types
*
* @param array ...$types Item types
*
* @return ItemInterface Item
* @api
*/
public function getFirstItem(...$types);
}
13 changes: 7 additions & 6 deletions src/Micrometa/Tests/Ports/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ public function testItemPropertyItem()
*/
public function testItemNestedItems()
{
$this->expectException('Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException');
$this->expectExceptionCode('1492418709');
$this->expectException(\Error::class);
$this->expectExceptionMessageMatches('/Call to undefined method/i');

$feedItem = $this->getFeedItem();
$this->assertInstanceOf(Item::class, $feedItem);

Expand Down Expand Up @@ -292,12 +293,12 @@ protected function runFeedNestedEntryItemTest(ItemInterface $feedItem)
*/
public function testItemNonExistentNestedItems()
{
$this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException');
$this->expectExceptionCode('1492418999');
$this->expectException(\Error::class);
$this->expectExceptionMessageMatches('/Call to undefined method/i');

$feedItem = $this->getFeedItem();
/** @noinspection PhpUndefinedMethodInspection */

$this->assertEquals('John Doe', $feedItem->hEntry()->author->name);
/** @noinspection PhpUndefinedMethodInspection */
$feedItem->hEntry(2);
}

Expand Down

0 comments on commit a90f075

Please sign in to comment.