|
| 1 | +<?php namespace SoapBox\Formatter; |
| 2 | + |
| 3 | +use InvalidArgumentException; |
| 4 | +use SoapBox\Formatter\Formatter as Formatter; |
| 5 | +use SoapBox\Formatter\Parsers\ArrayParser; |
| 6 | +use SoapBox\Formatter\Parsers\CsvParser; |
| 7 | +use SoapBox\Formatter\Parsers\JsonParser; |
| 8 | +use SoapBox\Formatter\Parsers\XmlParser; |
| 9 | +use SoapBox\Formatter\Parsers\YamlParser; |
| 10 | + |
| 11 | +class Formatter |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Add class constants that help define input format |
| 15 | + */ |
| 16 | + const CSV = 'csv'; |
| 17 | + const JSON = 'json'; |
| 18 | + const XML = 'xml'; |
| 19 | + const ARR = 'array'; |
| 20 | + const YAML = 'yaml'; |
| 21 | + |
| 22 | + private static $supportedTypes = [self::CSV, self::JSON, self::XML, self::ARR, self::YAML]; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var Parser |
| 26 | + */ |
| 27 | + private $parser; |
| 28 | + |
| 29 | + /** |
| 30 | + * Make: Returns an instance of formatter initialized with data and type |
| 31 | + * |
| 32 | + * @param mixed $data The data that formatter should parse |
| 33 | + * @param string $type The type of data formatter is expected to parse |
| 34 | + * @param string $delimiter The delimitation of data formatter to csv |
| 35 | + * @return Formatter |
| 36 | + */ |
| 37 | + public static function make($data, $type, $delimiter = null) |
| 38 | + { |
| 39 | + if (in_array($type, self::$supportedTypes)) { |
| 40 | + $parser = null; |
| 41 | + switch ($type) { |
| 42 | + case self::CSV: |
| 43 | + $parser = new CsvParser($data, $delimiter); |
| 44 | + break; |
| 45 | + case self::JSON: |
| 46 | + $parser = new JsonParser($data); |
| 47 | + break; |
| 48 | + case self::XML: |
| 49 | + $parser = new XmlParser($data); |
| 50 | + break; |
| 51 | + case self::ARR: |
| 52 | + $parser = new ArrayParser($data); |
| 53 | + break; |
| 54 | + case self::YAML: |
| 55 | + $parser = new YamlParser($data); |
| 56 | + break; |
| 57 | + } |
| 58 | + return new Formatter($parser, $type); |
| 59 | + } |
| 60 | + throw new InvalidArgumentException( |
| 61 | + 'make function only accepts [csv, json, xml, array] for $type but ' . $type . ' was provided.' |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + private function __construct($parser) |
| 66 | + { |
| 67 | + $this->parser = $parser; |
| 68 | + } |
| 69 | + |
| 70 | + public function toJson() |
| 71 | + { |
| 72 | + return $this->parser->toJson(); |
| 73 | + } |
| 74 | + |
| 75 | + public function toArray() |
| 76 | + { |
| 77 | + return $this->parser->toArray(); |
| 78 | + } |
| 79 | + |
| 80 | + public function toYaml() |
| 81 | + { |
| 82 | + return $this->parser->toYaml(); |
| 83 | + } |
| 84 | + |
| 85 | + public function toXml($baseNode = 'xml', $encoding = 'utf-8', $formated = false) |
| 86 | + { |
| 87 | + return $this->parser->toXml($baseNode, $encoding, $formated); |
| 88 | + } |
| 89 | + |
| 90 | + public function toCsv($newline = "\n", $delimiter = ",", $enclosure = '"', $escape = "\\") |
| 91 | + { |
| 92 | + return $this->parser->toCsv($newline, $delimiter, $enclosure, $escape); |
| 93 | + } |
| 94 | +} |
0 commit comments