Overview

Namespaces

  • Crunchmail
    • Collections
    • Entities
    • Exception
    • PHPUnit
    • Resources

Classes

  • Crunchmail\Client
  • Crunchmail\Collections\GenericCollection
  • Crunchmail\Entities\AttachmentEntity
  • Crunchmail\Entities\ContactEntity
  • Crunchmail\Entities\ContactListEntity
  • Crunchmail\Entities\ContactQueueEntity
  • Crunchmail\Entities\DomainEntity
  • Crunchmail\Entities\GenericEntity
  • Crunchmail\Entities\MessageEntity
  • Crunchmail\Entities\RecipientEntity
  • Crunchmail\PHPUnit\IsEntityConstraint
  • Crunchmail\PHPUnit\IsGenericCollectionConstraint
  • Crunchmail\PHPUnit\IsGenericEntityConstraint
  • Crunchmail\PHPUnit\IsGenericResourceConstraint
  • Crunchmail\PHPUnit\IsResourceConstraint
  • Crunchmail\PHPUnit\TestCase
  • Crunchmail\Resources\DomainsResource
  • Crunchmail\Resources\GenericResource
  • Crunchmail\Resources\PreviewSendResource

Exceptions

  • Crunchmail\Exception\ApiException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Generic collection for Crunchmail API
  4:  *
  5:  * @author    Yannick Huerre <dev@sheoak.fr>
  6:  * @copyright 2015 (c) Oasiswork
  7:  * @license   https://opensource.org/licenses/MIT MIT
  8:  *
  9:  * @todo accessing directly a page (adding filter page)
 10:  */
 11: namespace Crunchmail\Collections;
 12: 
 13: use Crunchmail\Resources\GenericResource;
 14: use Crunchmail\Client;
 15: 
 16: /**
 17:  * Generic collection for Crunchmail API
 18:  */
 19: class GenericCollection implements \countable
 20: {
 21:     /**
 22:      * Resource that created the collection
 23:      *
 24:      * @var mixed
 25:      */
 26:     private $resource;
 27: 
 28:     /**
 29:      * Current data, set of Entities
 30:      *
 31:      * @var array
 32:      */
 33:     private $collection = [];
 34: 
 35:     /**
 36:      * Raw collection
 37:      *
 38:      * @var GuzzleHttp\Psr7\Response
 39:      */
 40:     private $response;
 41: 
 42:     /**
 43:       * Initilialize the collection by populating the collection as an array of
 44:       * entities
 45:       *
 46:       * @param GenericResource $resource parent resource
 47:       * @param array $config API configuration
 48:       *
 49:       * @return object
 50:      */
 51:     public function __construct(GenericResource $resource, $data)
 52:     {
 53:         $this->resource = $resource;
 54:         $this->response = $data;
 55: 
 56:         $class = $this->resource->getEntityClass();
 57: 
 58:         foreach ($this->response->results as $row)
 59:         {
 60:             // add the new entity to collection
 61:             $this->collection[] = new $class($this->resource, $row);
 62:         }
 63:     }
 64: 
 65:     /**
 66:      * Returns the raw response
 67:      *
 68:      * @return GuzzleHttp\Psr7\Response
 69:      */
 70:     public function getResponse()
 71:     {
 72:         return $this->response;
 73:     }
 74: 
 75:     /**
 76:      * Return the number of results
 77:      *
 78:      * @return int
 79:      */
 80:     public function count()
 81:     {
 82:         return (int) $this->response->count;
 83:     }
 84: 
 85:     /**
 86:      * Return the number of pages
 87:      *
 88:      * @return int
 89:      */
 90:     public function pageCount()
 91:     {
 92:         return (int) $this->response->page_count;
 93:     }
 94: 
 95:     /**
 96:      * Return the current set of results
 97:      *
 98:      * @return array
 99:      */
100:     public function current()
101:     {
102:         return $this->collection;
103:     }
104: 
105:     /**
106:      * Repopulate collection with next results
107:      *
108:      * @return Crunchmail\Collections\GenericCollection
109:      */
110:     public function next()
111:     {
112:         $this->getAdjacent('next');
113:     }
114: 
115:     /**
116:      * Repopulate current collection with previous results
117:      *
118:      * @return Crunchmail\Collections\GenericCollection
119:      */
120:     public function previous()
121:     {
122:         $this->getAdjacent('previous');
123:     }
124: 
125:     /**
126:      * Return next or previous page
127:      *
128:      * @param string $direction next or previous
129:      *
130:      * @return Crunchmail\Collections\GenericCollection
131:      */
132:     public function getAdjacent($direction)
133:     {
134:         $url = $this->response->$direction;
135:         return !empty($url) ? $this->resource->get($url) : null;
136:     }
137: 
138:     /**
139:      * Repopulate current collection with fresh data
140:      *
141:      * @return Crunchmail\Collections\GenericCollection
142:      */
143:     public function refresh()
144:     {
145:         return $this->resource->get();
146:     }
147: }
148: 
API documentation generated by ApiGen