* @param string $file_path file_path of file to open * @param string $mode one of AvroFile::READ_MODE or AvroFile::WRITE_MODE * @param string $schemaJson JSON of writer's schema * @param string $codec compression codec * @returns AvroDataIOWriter instance of AvroDataIOWriter * * @throws AvroDataIOException if $writers_schema is not provided * or if an inv
(
$file_path,
$mode = AvroFile::READ_MODE,
$schemaJson = null,
$codec = self::NULL_CODEC
)
| 132 | * or if an invalid $mode is given. |
| 133 | */ |
| 134 | public static function openFile( |
| 135 | $file_path, |
| 136 | $mode = AvroFile::READ_MODE, |
| 137 | $schemaJson = null, |
| 138 | $codec = self::NULL_CODEC |
| 139 | ) { |
| 140 | $schema = !is_null($schemaJson) |
| 141 | ? AvroSchema::parse($schemaJson) : null; |
| 142 | |
| 143 | $io = false; |
| 144 | switch ($mode) { |
| 145 | case AvroFile::WRITE_MODE: |
| 146 | if (is_null($schema)) { |
| 147 | throw new AvroDataIOException('Writing an Avro file requires a schema.'); |
| 148 | } |
| 149 | $file = new AvroFile($file_path, AvroFile::WRITE_MODE); |
| 150 | $io = self::openWriter($file, $schema, $codec); |
| 151 | break; |
| 152 | case AvroFile::READ_MODE: |
| 153 | $file = new AvroFile($file_path, AvroFile::READ_MODE); |
| 154 | $io = self::openReader($file, $schema); |
| 155 | break; |
| 156 | default: |
| 157 | throw new AvroDataIOException( |
| 158 | sprintf( |
| 159 | "Only modes '%s' and '%s' allowed. You gave '%s'.", |
| 160 | AvroFile::READ_MODE, |
| 161 | AvroFile::WRITE_MODE, |
| 162 | $mode |
| 163 | ) |
| 164 | ); |
| 165 | } |
| 166 | return $io; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param AvroIO $io |