-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatter.php
More file actions
51 lines (47 loc) · 1.59 KB
/
Formatter.php
File metadata and controls
51 lines (47 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* @package whotrades\formatter
*/
namespace whotrades\formatter;
use whotrades\formatter\Format;
class Formatter
{
/**
* @param string $data
* @param string | null $forceFormat
*
* @return Format\Base
*/
public static function factory($data, $forceFormat = null)
{
$errorList = [];
if ($forceFormat) {
switch ($forceFormat) {
case Format\Base::DATA_FORMAT_XML:
$formatObject = new Format\Xml($data, (bool) $forceFormat);
break;
case Format\Base::DATA_FORMAT_JSON:
$formatObject = new Format\Json($data, (bool) $forceFormat);
break;
case Format\Base::DATA_FORMAT_YAML:
$formatObject = new Format\Yaml($data, (bool) $forceFormat);
break;
default:
$formatObject = new Format\Unsupported($data);
}
} else {
switch (true) {
case !$errorList['xml'] = ($formatObject = new Format\Xml($data))->getErrorList():
break;
case !$errorList['json'] = ($formatObject = new Format\Json($data))->getErrorList():
break;
case !$errorList['yaml'] = ($formatObject = new Format\Yaml($data))->getErrorList():
break;
default:
$formatObject = new Format\Unsupported($data);
$formatObject->setErrorList($errorList);
}
}
return $formatObject;
}
}