public function testToArrayWithoutNulls() { $obj = new CollectionJson\Collection\Error('not found', 404, 'Example message'); $result = $obj->toArray(); foreach (array('code', 'message', 'title') as $key) { $this->assertArrayHasKey($key, $result); $this->assertTrue(is_string($result[$key])); } }
/** * Converts the whole object to an array * * @return array */ public function toArray() { // defaults $collection = array('version' => $this->getVersion(), 'href' => $this->getHref()); // we have an error object if ($this->error) { $collection['error'] = $this->error->toArray(); } // we have a template object if ($this->template) { $collection = array_merge($collection, $this->template->toArray()); } // we have some other objects foreach (array('items', 'queries', 'links') as $type) { if (count($this->{$type})) { foreach ($this->{$type} as $item) { $collection[$type][] = $item->toArray(); } } } // encoding data return array('collection' => $collection); }