Skip to content

Commit ef5357c

Browse files
authored
Merge pull request #1 from EpicKris/develop
Added Native, CSV, JSON & XML Data Formats
2 parents d54a0ab + 3e0ed9c commit ef5357c

12 files changed

+866
-1
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
trim_trailing_whitespace = true
7+
insert_final_newline = false
8+
9+
[*.php]
10+
indent_style = space
11+
insert_final_newline = true

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
# php-data-format
1+
# PHP Data Format
22
Convert between various data formats.
3+
4+
## Data Formats
5+
* Native
6+
* CSV
7+
* JSON
8+
* XML

composer.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "epickris/data-format",
3+
"autoload": {
4+
"psr-4": {
5+
"EpicKris\\": "src/"
6+
}
7+
}
8+
}

src/DataFormat.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace EpicKris;
4+
5+
use EpicKris\DataFormats\CsvInterface;
6+
use EpicKris\DataFormats\JsonInterface;
7+
use EpicKris\DataFormats\NativeInterface;
8+
use EpicKris\DataFormats\XmlInterface;
9+
10+
/**
11+
* Data format.
12+
*
13+
* Convert between various data formats.
14+
*
15+
* @package EpicKris
16+
*/
17+
class DataFormat implements NativeInterface, CsvInterface, JsonInterface, XmlInterface
18+
{
19+
use DataFormats\Native;
20+
use DataFormats\Csv;
21+
use DataFormats\Json;
22+
use DataFormats\Xml;
23+
}

src/DataFormats/Csv.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace EpicKris\DataFormats;
4+
5+
/**
6+
* CSV data format trait.
7+
*
8+
* @package EpicKris\DataFormats
9+
*/
10+
trait Csv
11+
{
12+
/**
13+
* Format data to CSV string.
14+
*
15+
* @param array|object $data Data.
16+
*
17+
* @return string Returns CSV string.
18+
* @throws \InvalidArgumentException If data argument is not an array or object.
19+
*/
20+
public static function toCsv($data)
21+
{
22+
if (! is_array($data) && ! is_object($data)) {
23+
throw new \InvalidArgumentException('Data argument must be an array or object.');
24+
}
25+
26+
if (is_object($data)) {
27+
$data = get_object_vars($data);
28+
}
29+
30+
$headings = array_keys($data);
31+
32+
$output = implode(',', $headings) . '\r\n';
33+
foreach ($data as $value) {
34+
$output .= '"' . implode('","', $value) . '"\r\n';
35+
}
36+
37+
return $output;
38+
}
39+
40+
/**
41+
* Format data array to CSV string.
42+
*
43+
* @param array $data Data array.
44+
*
45+
* @return string Returns CSV string.
46+
* @throws \InvalidArgumentException If data argument is not an array.
47+
*/
48+
public static function arrayToCsv($data)
49+
{
50+
if (! is_array($data)) {
51+
throw new \InvalidArgumentException('Data argument must be an array.');
52+
}
53+
54+
return self::toCsv($data);
55+
}
56+
57+
/**
58+
* Format data object to CSV string.
59+
*
60+
* @param object $data Data object.
61+
*
62+
* @return string Returns CSV string.
63+
* @throws \InvalidArgumentException If CSV argument is not an object.
64+
*/
65+
public static function objectToCsv($data)
66+
{
67+
if (! is_object($data)) {
68+
throw new \InvalidArgumentException('Data argument must be an object.');
69+
}
70+
71+
return self::toCsv($data);
72+
}
73+
74+
/**
75+
* Format CSV string to data array.
76+
*
77+
* @param string $csv CSV string.
78+
*
79+
* @return array Returns data array.
80+
* @throws \InvalidArgumentException If CSV argument is not a CSV string.
81+
*/
82+
public static function csvToArray($csv)
83+
{
84+
if (! is_string($csv)) {
85+
throw new \InvalidArgumentException('CSV argument must be a CSV string.');
86+
}
87+
88+
$csv = trim($csv);
89+
90+
return str_getcsv($csv);
91+
}
92+
93+
/**
94+
* Format CSV string to data object.
95+
*
96+
* @param string $csv CSV string.
97+
*
98+
* @return object Returns data object.
99+
* @throws \InvalidArgumentException If CSV argument is not a CSV string.
100+
*/
101+
public static function csvToObject($csv)
102+
{
103+
if (! is_string($csv)) {
104+
throw new \InvalidArgumentException('CSV argument must be a CSV string.');
105+
}
106+
107+
$csv = trim($csv);
108+
$array = str_getcsv($csv);
109+
110+
$object = new \stdClass();
111+
foreach ($array as $key => $value) {
112+
$object->$key = $value;
113+
}
114+
}
115+
}

src/DataFormats/CsvInterface.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace EpicKris\DataFormats;
4+
5+
/**
6+
* CSV data format interface.
7+
*
8+
* @package EpicKris\DataFormats
9+
*/
10+
interface CsvInterface
11+
{
12+
/**
13+
* Format data to CSV string.
14+
*
15+
* @param array|object $data Data.
16+
*
17+
* @return string Returns CSV string.
18+
*/
19+
public static function toCsv($data);
20+
21+
/**
22+
* Format data array to CSV string.
23+
*
24+
* @param array $data Data array.
25+
*
26+
* @return string Returns CSV string.
27+
*/
28+
public static function arrayToCsv($data);
29+
30+
/**
31+
* Format data object to CSV string.
32+
*
33+
* @param object $data Data object.
34+
*
35+
* @return string Returns CSV string.
36+
*/
37+
public static function objectToCsv($data);
38+
39+
/**
40+
* Format CSV string to data array.
41+
*
42+
* @param string $csv CSV string.
43+
*
44+
* @return array Returns data array.
45+
*/
46+
public static function csvToArray($csv);
47+
48+
/**
49+
* Format CSV string to data object.
50+
*
51+
* @param string $csv CSV string.
52+
*
53+
* @return object Returns data object.
54+
*/
55+
public static function csvToObject($csv);
56+
}

src/DataFormats/Json.php

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace EpicKris\DataFormats;
4+
5+
/**
6+
* JSON data format trait.
7+
*
8+
* @package EpicKris\DataFormats
9+
*/
10+
trait Json
11+
{
12+
/**
13+
* Format data to JSON string.
14+
*
15+
* @param array|object $data Data.
16+
*
17+
* @return string Returns JSON string.
18+
* @throws \InvalidArgumentException If data argument is not an array or object.
19+
*/
20+
public static function toJson($data)
21+
{
22+
if (! is_array($data) && ! is_object($data)) {
23+
throw new \InvalidArgumentException('Data argument must be an array or object.');
24+
}
25+
26+
return json_encode($data);
27+
}
28+
29+
/**
30+
* Format array data to JSON string.
31+
*
32+
* @param array $data Data array.
33+
*
34+
* @return string Returns JSON string.
35+
* @throws \InvalidArgumentException If data argument is not an array.
36+
*/
37+
public static function arrayToJson($data)
38+
{
39+
if (! is_array($data)) {
40+
throw new \InvalidArgumentException('Data argument must be an array.');
41+
}
42+
43+
return self::toJson($data);
44+
}
45+
46+
/**
47+
* Format object data to JSON string.
48+
*
49+
* @param object $data Data object.
50+
*
51+
* @return string Returns JSON string.
52+
* @throw \InvalidArgumentException If data argument is not an object.
53+
*/
54+
public static function objectToJson($data)
55+
{
56+
if (! is_object($data)) {
57+
throw new \InvalidArgumentException('Data argument must be an object.');
58+
}
59+
60+
return self::toJson($data);
61+
}
62+
63+
/**
64+
* Format JSON string to data array.
65+
*
66+
* @param string $json JSON string.
67+
*
68+
* @return array Returns data array.
69+
* @throws \InvalidArgumentException If JSON argument is not a JSON string.
70+
*/
71+
public static function jsonToArray($json)
72+
{
73+
if (! is_string($json)) {
74+
throw new \InvalidArgumentException('JSON argument must be a JSON string.');
75+
}
76+
77+
$json = trim($json);
78+
$data = json_decode($json);
79+
80+
if (is_null($data)) {
81+
throw new \InvalidArgumentException('JSON argument must be a JSON string.');
82+
}
83+
84+
return $data;
85+
}
86+
87+
/**
88+
* Format JSON string to data object.
89+
*
90+
* @param string $json JSON string.
91+
*
92+
* @return object Returns data object.
93+
* @throws \InvalidArgumentException If JSON argument is not a JSON string.
94+
*/
95+
public static function jsonToObject($json)
96+
{
97+
if (! is_string($json)) {
98+
throw new \InvalidArgumentException('JSON argument must be a JSON string.');
99+
}
100+
101+
$json = trim($json);
102+
$object = json_decode($json, true);
103+
104+
if (is_null($object)) {
105+
throw new \InvalidArgumentException('JSON argument must be a JSON string.');
106+
}
107+
108+
return $data;
109+
}
110+
}

src/DataFormats/JsonInterface.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace EpicKris\DataFormats;
4+
5+
/**
6+
* JSON data format interface.
7+
*
8+
* @package EpicKris\DataFormats
9+
*/
10+
interface JsonInterface
11+
{
12+
/**
13+
* Format data to JSON string.
14+
*
15+
* @param array|object $data Data.
16+
*
17+
* @return string Returns JSON string.
18+
*/
19+
public static function toJson($data);
20+
21+
/**
22+
* Format data array to JSON string.
23+
*
24+
* @param array $data Data array.
25+
*
26+
* @return string Returns JSON string.
27+
*/
28+
public static function arrayToJson($data);
29+
30+
/**
31+
* Format data object to JSON string.
32+
*
33+
* @param object $data Data object.
34+
*
35+
* @return string Returns JSON string.
36+
*/
37+
public static function objectToJson($data);
38+
39+
/**
40+
* Format JSON string to data array.
41+
*
42+
* @param string $json JSON string.
43+
*
44+
* @return array Returns data array.
45+
*/
46+
public static function jsonToArray($json);
47+
48+
/**
49+
* Format JSON string to data object.
50+
*
51+
* @param string $json JSON string.
52+
*
53+
* @return object Returns data object.
54+
*/
55+
public static function jsonToObject($json);
56+
}

0 commit comments

Comments
 (0)