|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file contains the base class for converters which PhpSoda will support |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright 2015 Vladimir Jimenez |
|
7
|
|
|
* @license https://github.com/allejo/PhpSoda/blob/master/LICENSE.md MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace allejo\Socrata\Converters; |
|
11
|
|
|
|
|
12
|
|
|
use allejo\Socrata\Exceptions\FileNotFoundException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The base class to support custom data formats other than JSON; this is so PhpSoda can support for your any data |
|
16
|
|
|
* format. |
|
17
|
|
|
* |
|
18
|
|
|
* TosSupport custom data types other than JSON, you must provide a conversion method from your data format to JSON. |
|
19
|
|
|
* This base class provides a method to create an instance of itself from a file and it defines the abstract method |
|
20
|
|
|
* `toJson()` that must be implemented in your converter. |
|
21
|
|
|
* |
|
22
|
|
|
* As an example, PhpSoda's official support of CSV is a converter extended from this class and implements a conversion |
|
23
|
|
|
* from CSV to JSON. |
|
24
|
|
|
* |
|
25
|
|
|
* @package allejo\Socrata\Converters |
|
26
|
|
|
* @since 0.1.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class Converter |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* The data to be converted into the custom data format. For example, in the CSV converter, this variable stores |
|
32
|
|
|
* the CSV formatted data. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $data; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create a converter that will convert data from your data format into JSON |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $customFormattedData The data (in your custom data format) to be converted into JSON |
|
42
|
|
|
* |
|
43
|
|
|
* @since 0.1.0 |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct ($customFormattedData) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->data = $customFormattedData; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* A convenience method to create a Converter instance from a file name without having to read the file data and |
|
52
|
|
|
* then give it to the CsvConverter constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $filename The path or filename of the CSV file to open and create a CsvConverter for |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \allejo\Socrata\Exceptions\FileNotFoundException |
|
57
|
|
|
* |
|
58
|
|
|
* @return static |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function fromFile ($filename) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!file_exists($filename) || !is_readable($filename)) |
|
63
|
|
|
{ |
|
64
|
|
|
throw new FileNotFoundException($filename); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$data = file_get_contents($filename); |
|
68
|
|
|
|
|
69
|
|
|
return new static($data); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Convert the current data stored into a JSON formatted string to be submitted to Socrata |
|
74
|
|
|
* |
|
75
|
|
|
* @return string A JSON formatted string |
|
76
|
|
|
*/ |
|
77
|
|
|
abstract public function toJson (); |
|
78
|
|
|
} |
|
79
|
|
|
|