From 811d5efa189b181b6bcd3c5ca81dc1c693a682eb Mon Sep 17 00:00:00 2001 From: Hans Ott Date: Sun, 13 Mar 2016 17:15:48 +0100 Subject: [PATCH] PagedList: Verify that all items are pinterest objects --- src/Pinterest/Objects/PagedList.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Pinterest/Objects/PagedList.php b/src/Pinterest/Objects/PagedList.php index 01971ee..b6b87ae 100644 --- a/src/Pinterest/Objects/PagedList.php +++ b/src/Pinterest/Objects/PagedList.php @@ -13,6 +13,8 @@ namespace Pinterest\Objects; +use InvalidArgumentException; + /** * This class represents a paged list. * @@ -42,6 +44,7 @@ final class PagedList */ public function __construct(array $items = array(), $nextUrl = null) { + $this->guardThatTheseAreAllPinterestObjects($items); $this->items = $items; $this->nextUrl = $nextUrl; } @@ -75,4 +78,23 @@ public function getNextUrl() { return $this->nextUrl; } + + /** + * Checks if all items are pinterest objects. + * + * @param array $items + * + * @throws InvalidArgumentException + */ + private function guardThatTheseAreAllPinterestObjects(array $items) + { + foreach ($items as $item) { + if (! ($item instanceof BaseObject)) { + throw new InvalidArgumentException(sprintf( + 'Expected "Pinterest\Objects\BaseObject" but instead got: "%s"', + is_object($item) ? get_class($item) : gettype($item) + )); + } + } + } }