function testJunctions() { $junctions = new Collection($this->backend->junctions); $actual = $junctions->orderBy('name')->select('name', null)->toArray(); $expected = ['Membership', 'Rating']; $this->assertEquals($expected, $actual); }
public function where($conditions) { if ($this->valueField === null && $this->data instanceof Collection) { // The valueField is not set, pass the conditions to the original collection. return new self($this->data->where($conditions), null, $this->keyField); } return parent::where($conditions); }
public function orderByDescending($selector, $method = SORT_REGULAR) { if ($this->isConverted === false && is_string($selector) && isset($this->options['mapping'][$selector]) && $this->data instanceof Collection) { return new self($this->data->orderByDescending($this->options['mapping'][$selector], $method), $this->model, $this->repository, $this->options); } return parent::orderByDescending($selector, $method); }
public function test_reduce() { $collection = new Collection(array(1, 2, 3, 4)); $result = $collection->reduce(function ($result, $v) { return $result + $v; }); $this->assertSame(10, $result, '1 + 2 + 3 + 4 = 10'); $this->assertSame(array(1, 2, 3, 4), $collection->toArray(), 'Original array should remain intact'); }
/** * Converts $this->data into an array. */ protected function dataToArray() { if (is_array($this->data)) { return; } if ($this->data === null) { $db = Connection::instance($this->dbLink); $this->data = $db->fetchAll($this->sql); return; } if ($this->data instanceof PDOStatement) { $this->data = $this->data->fetchAll(); return; } if ($this->data === false) { throw new InfoException('Unable to "fetchAll()", the query failed', (string) $this->sql); } return parent::dataToArray(); }