Skip to content

Return Types

supergrecko edited this page Apr 3, 2019 · 5 revisions

To make working with results a lot simpler every RiotQuest response returns a RiotQuest\Components\Framework\Collections\Collection object or an object which extends it. This object has a lot of helper functions which makes working with data a lot simpler.

The class source can be located here: src/RiotQuest/Framework/Collections/Collection.php

Working with a Collection

The Collection class implements \ArrayAccess, \Countable, \Serializable and \IteratorAggregate. This means we can run native php functions as count(), serialize() and unserialize() on it. We are also able to access it's members as if it was a regular array and you can foreach loop over it. Here's an example.

<?php
use RiotQuest\Components\Framework\Collections\Collection;

// create a new collection and fill it with some data
$collection = new Collection(['abc' => 123, 'def' => 456]); 

count($collection); // 2

serialize($collection); // C:53:"RiotQuest\Components\Framework\Collections\Collection":38:{a:2:{s:3:"abc";i:123;s:3:"def";i:456;}}

unserialize('C:53:"RiotQuest\Components\Framework\Collections\Collection":38:{a:2:{s:3:"abc";i:123;s:3:"def";i:456;}}'); // returns the collection object

$collection['abc']; // 123
$collection['def']; // 456

foreach ($collection as $key => $value) {
    echo "$key: $value";
}
// outputs:
// abc:123def:456
?>

Why use Collections?

The reason RiotQuest uses collections over native arrays for data management is because we can append our own methods to the results. By implementing the mentioned interfaces we can make the object behave almost like an array, and we can add methods to it.

Clone this wiki locally
You can’t perform that action at this time.