1 | <?php |
||
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 |