/** * Writes data to a JSON file. * * If a $threshold is given, files will be enumerated with ordinal numbers, * for example if $path = "/path/to/file.json" is given, the data will be * written in files: * * - /path/to/file.00001.json * - /path/to/file.00002.json * - /path/to/file.00003.json * - etc. * * By default, the writer will output a JSON array if given $data has * integer keys, and a JSON object for string keys. This can be forced * by setting one of FLAG_AS_ARRAY or FLAG_AS_OBJECT in $flags. * * @param string $path Path to the file to write. * @param Iterator $data An iterator or generator yielding the data. * @param numeric $threshold Max. file size in bytes (null for no limit). * @param integer $flags Additional options, see FLAG_* constants. */ public function write($path, Iterator $data, $threshold = null, $flags = 0) { $asObject = $this->isAsObject($data, $flags); $pretty = $flags & self::FLAG_PRETTY_PRINT; $jsonFlags = $pretty ? JSON_PRETTY_PRINT : 0; // When a threshold is given, enumerate files $ord = isset($threshold) ? 1 : null; $file = $this->open($path, $asObject, $pretty, $ord); while ($data->valid()) { if ($pretty) { $file->fwrite("\t"); } if ($asObject) { $file->fwrite('"' . $data->key() . '": '); } $file->fwrite(Json::encode($data->current(), $jsonFlags)); // Move to next item to see if next exists $data->next(); $hasNext = $data->valid(); // Check whether a rollover is necessary $shouldRollover = $this->shouldRollover($file, $threshold); // Write a comma separator if it's not the last item for this file if (!$shouldRollover && $hasNext) { $file->fwrite(","); } if ($pretty) { $file->fwrite("\n"); } if ($shouldRollover && $hasNext) { $ord += 1; $file = $this->rollover($file, $asObject, $path, $pretty, $ord); } } $this->close($file, $asObject, $pretty); }
/** * @expectedException Exception * @expectedExceptionMessage Failed encoding JSON */ public function testEncodeError() { // Unencodable stuff Json::encode(INF); }
public function testOptimize() { $coreName = "xyz"; $expected = "expected response"; $headers = ['Content-Type' => 'application/json']; $response = m::mock(Response::class); $response->shouldReceive('getBody->getContents')->andReturn(Json::encode($expected)); $client = m::mock(Client::class); $client->shouldReceive('post')->with("{$coreName}/update?optimize=true&wt=json", null, $headers)->once()->andReturn($response); $core = new Core($client, $coreName); $actual = $core->optimize(); $this->assertSame($expected, $actual); }
/** * Deletes records matching the given query. */ public function deleteByQuery($select, $commit = true) { $body = Json::encode(["delete" => ["query" => $select]]); $update = Solr::update()->body($body)->commit($commit); return $this->update($update); }