trait JsonTrait in Automatic Updates 8.2
Provides assertive methods to read and write JSON data in files.
Hierarchy
- trait \Drupal\Tests\automatic_updates\Traits\JsonTrait
File
- tests/
src/ Traits/ JsonTrait.php, line 11
Namespace
Drupal\Tests\automatic_updates\TraitsView source
trait JsonTrait {
/**
* Reads JSON data from a file and returns it as an array.
*
* @param string $path
* The path of the file to read.
*
* @return mixed[]
* The parsed data in the file.
*/
protected function readJson(string $path) : array {
Assert::assertIsReadable($path);
$data = file_get_contents($path);
return Json::decode($data);
}
/**
* Writes an array of data to a file as JSON.
*
* @param string $path
* The path of the file to write.
* @param array $data
* The data to be written.
*/
protected function writeJson(string $path, array $data) : void {
Assert::assertIsWritable(file_exists($path) ? $path : dirname($path));
file_put_contents($path, Json::encode($data));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
JsonTrait:: |
protected | function | Reads JSON data from a file and returns it as an array. | |
JsonTrait:: |
protected | function | Writes an array of data to a file as JSON. |