1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Format\Formatter; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Format\CsvFormatInterface; |
17
|
|
|
use Graze\DataFile\Format\FormatInterface; |
18
|
|
|
use Graze\DataFile\Format\JsonFormatInterface; |
19
|
|
|
use InvalidArgumentException; |
20
|
|
|
|
21
|
|
|
class FormatterFactory implements FormatterFactoryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param FormatInterface $format |
25
|
|
|
* |
26
|
|
|
* @return FormatterInterface |
|
|
|
|
27
|
|
|
*/ |
28
|
5 |
|
public function getFormatter(FormatInterface $format) |
29
|
|
|
{ |
30
|
5 |
|
switch ($format->getType()) { |
31
|
5 |
|
case 'csv': |
32
|
2 |
|
if ($format instanceof CsvFormatInterface) { |
33
|
1 |
|
return new CsvFormatter($format); |
34
|
|
|
} else { |
35
|
1 |
|
throw new InvalidArgumentException( |
36
|
|
|
"Format indicates it is csv but does not implement CsvFormatInterface" |
37
|
1 |
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// this will never be called but phpcf doesn't understand the concept of exceptions |
41
|
|
|
break; |
|
|
|
|
42
|
|
|
|
43
|
3 |
|
case 'json': |
44
|
2 |
|
if ($format instanceof JsonFormatInterface) { |
45
|
1 |
|
return new JsonFormatter($format); |
46
|
|
|
} else { |
47
|
1 |
|
throw new InvalidArgumentexception( |
48
|
|
|
"Format indicates it is json but does not implement JsonFormatInterface" |
49
|
1 |
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// this will never be called but phpcf doesn't understand the concept of exceptions |
53
|
|
|
break; |
|
|
|
|
54
|
1 |
|
default: |
55
|
1 |
|
throw new InvalidArgumentException("Supplied format: {$format->getType()} is unknown"); |
56
|
1 |
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.