public function testToJson() { $resultSet = new ResultSet([['key' => 1], ['key' => 2], ['key' => 3]]); $json = $resultSet->toJson(); $expected = '[{"key":1},{"key":2},{"key":3}]'; $this->assertEquals($expected, $json); }
public function __construct(array $result) { if (!isset($result['matches'])) { $result['matches'] = []; } $this->result = $result; parent::__construct($result['matches']); }
/** * @param callable $fn (Schema $item, int $i) * @return \Reach\ResultSet */ public function map($fn) { $i = 0; $resultSet = new BaseResultSet(); foreach ($this as $item) { $resultSet->append(call_user_func_array($fn, [$item, $i++])); } return $resultSet; }
/** * Perform a batchInsert of objects. * @param $list Collection of documents to insert * @param array $options * @return \Reach\ResultSet */ public function batchInsert($list, array $options = []) { if (!is_array($list) && !$list instanceof \Traversable && !$list instanceof ResultSet) { throw new \InvalidArgumentException('Parameter "list" is not traversable using foreach'); } $documents = []; $saved = []; foreach ($list as $model) { if ($model instanceof DocumentInterface) { if (!$model->getIsNew()) { continue; } if ($model->beforeInsert() === false) { continue; } if ($model->_id === null || self::isValidMongoId($model->_id)) { $model->_id = new MongoId($model->_id); } $documents[] = $model->getRawDocument(); $saved[] = $model; } } unset($list, $model); $result = new ResultSet(); if (!count($documents)) { return $result; } $response = $this->_mongoCollection->batchInsert($documents, $options); unset($documents); if (empty($response['ok'])) { return $result; } foreach ($saved as $model) { $model->setIsNew(false); $model->afterInsert(); $result->append($model); } return $result; }