-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring, added Repository and Factory classes, stream validation …
…support (#6) refactoring, added Repository and Factory classes, stream validation support
- Loading branch information
Showing
44 changed files
with
3,263 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
namespace frictionlessdata\datapackage\DataStreams; | ||
|
||
/** | ||
* Provides a standard interface for streaming | ||
* | ||
* functionality could mostly be replaced by php generators (http://php.net/manual/en/language.generators.syntax.php) | ||
* however, they are only supported on PHP 5.5 and above | ||
*/ | ||
abstract class BaseDataStream implements \Iterator | ||
{ | ||
/** | ||
* @param string $dataSource | ||
* @param mixed $dataSourceOptions | ||
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException | ||
*/ | ||
abstract public function __construct($dataSource, $dataSourceOptions=null); | ||
|
||
/** | ||
* @return mixed | ||
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamValidationException | ||
*/ | ||
abstract public function current(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
namespace frictionlessdata\datapackage\DataStreams; | ||
|
||
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException; | ||
|
||
/** | ||
* streams the raw data without processing - used for default data package resources | ||
*/ | ||
class DefaultDataStream extends BaseDataStream | ||
{ | ||
/** | ||
* @param string $dataSource | ||
* @throws DataStreamOpenException | ||
*/ | ||
public function __construct($dataSource, $dataSourceOptions=null) | ||
{ | ||
try { | ||
$this->fopenResource = fopen($dataSource, "r"); | ||
} catch (\Exception $e) { | ||
throw new DataStreamOpenException("Failed to open data source ".json_encode($dataSource).": ".json_encode($e->getMessage())); | ||
} | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
fclose($this->fopenResource); | ||
} | ||
|
||
public function rewind() { | ||
if ($this->currentLineNumber == 0) { | ||
// starting iterations | ||
$this->currentLineNumber = 1; | ||
} else { | ||
throw new \Exception("DataStream does not support rewinding a stream, sorry"); | ||
} | ||
} | ||
|
||
public function current() { | ||
return fgets($this->fopenResource); | ||
} | ||
|
||
public function key() { | ||
return $this->currentLineNumber; | ||
} | ||
|
||
public function next() { | ||
$this->currentLineNumber++; | ||
} | ||
|
||
public function valid() { | ||
return (!feof($this->fopenResource)); | ||
} | ||
|
||
protected $currentLineNumber = 0; | ||
protected $fopenResource; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
namespace frictionlessdata\datapackage\DataStreams; | ||
|
||
use frictionlessdata\datapackage\Exceptions\DataStreamValidationException; | ||
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException; | ||
use frictionlessdata\tableschema\DataSources\CsvDataSource; | ||
use frictionlessdata\tableschema\Schema; | ||
use frictionlessdata\tableschema\Table; | ||
use frictionlessdata\tableschema\Exceptions\TableRowValidationException; | ||
|
||
|
||
class TabularDataStream extends BaseDataStream | ||
{ | ||
public function __construct($dataSource, $schema=null) | ||
{ | ||
if (empty($schema)) { | ||
throw new \Exception("schema is required for tabular data stream"); | ||
} else { | ||
try { | ||
$dataSource = new CsvDataSource($dataSource); | ||
$schema = new Schema($schema); | ||
$this->table = new Table($dataSource, $schema); | ||
} catch (\Exception $e) { | ||
throw new DataStreamOpenException("Failed to open tabular data source ".json_encode($dataSource).": ".json_encode($e->getMessage())); | ||
} | ||
} | ||
} | ||
|
||
protected $table; | ||
|
||
public function rewind() { | ||
$this->table->rewind(); | ||
} | ||
|
||
/** | ||
* @return array | ||
* @throws DataStreamValidationException | ||
*/ | ||
public function current() { | ||
try { | ||
return $this->table->current(); | ||
} catch (TableRowValidationException $e) { | ||
throw new DataStreamValidationException($e->getMessage()); | ||
} | ||
} | ||
|
||
public function key() { | ||
return $this->table->key(); | ||
} | ||
|
||
public function next() { | ||
$this->table->next(); | ||
} | ||
|
||
public function valid() { | ||
return $this->table->valid(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.