You are here

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\Traits
View 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

Namesort descending Modifiers Type Description Overrides
JsonTrait::readJson protected function Reads JSON data from a file and returns it as an array.
JsonTrait::writeJson protected function Writes an array of data to a file as JSON.