1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Fuel\Foundation |
4
|
|
|
* @version 2.0 |
5
|
|
|
* @author Fuel Development Team |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2010 - 2017 Fuel Development Team |
8
|
|
|
* @link http://fuelphp.com |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Fuel\Foundation; |
14
|
|
|
|
15
|
|
|
use Fuel\Foundation\Exception\Formatter; |
16
|
|
|
use Fuel\Foundation\Exception\FormatterLoad; |
17
|
|
|
use Fuel\Foundation\Formatter\FormatterInterface; |
18
|
|
|
use League\Container\ContainerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Keeps track of active formatters and facilitates the formatting of a controller response. |
22
|
|
|
*/ |
23
|
|
|
class ResponseFormatter |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FormatterInterface[] |
27
|
|
|
*/ |
28
|
|
|
protected $formatterClasses = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ContainerInterface |
32
|
|
|
*/ |
33
|
|
|
protected $dependencyContainer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ResponseFormatter constructor. |
37
|
|
|
* |
38
|
|
|
* @param string[] $formatters List of class names or DIC instance names |
39
|
|
|
* @param ContainerInterface $dependencyContainer |
40
|
|
|
*/ |
41
|
6 |
|
public function __construct($formatters, $dependencyContainer) |
42
|
|
|
{ |
43
|
6 |
|
$this->dependencyContainer = $dependencyContainer; |
44
|
|
|
|
45
|
6 |
|
foreach ($formatters as $formatter) { |
46
|
4 |
|
$formatterInstance = $dependencyContainer->get($formatter); |
47
|
|
|
|
48
|
4 |
|
if (! $formatterInstance instanceof FormatterInterface) { |
49
|
1 |
|
throw new FormatterLoad("FOU-003: Unable to load [$formatter]: Does not implement FormatterInterface"); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
$formatterInstance->setContainer($dependencyContainer); |
53
|
3 |
|
$this->formatterClasses[$formatter] = $formatterInstance; |
54
|
|
|
} |
55
|
5 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return FormatterInterface[] |
59
|
|
|
*/ |
60
|
1 |
|
public function getFormatters() |
61
|
|
|
{ |
62
|
1 |
|
return $this->formatterClasses; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $data Result returned by the controller. |
67
|
|
|
* |
68
|
|
|
* @return FormatterInterface |
69
|
|
|
*/ |
70
|
4 |
|
public function getFormatter($data) |
71
|
|
|
{ |
72
|
4 |
|
foreach ($this->formatterClasses as $formatter) { |
73
|
2 |
|
if ($formatter->canActivate($data)) { |
74
|
2 |
|
return $formatter; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Attempts to run a registered formatter on the given data. |
83
|
|
|
* |
84
|
|
|
* @param mixed $data |
85
|
|
|
*/ |
86
|
2 |
|
public function format($data) |
87
|
|
|
{ |
88
|
2 |
|
$formatter = $this->getFormatter($data); |
89
|
|
|
|
90
|
2 |
|
if ($formatter === null) { |
91
|
1 |
|
throw new Formatter('FOU-004: No formatter could be found'); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$formatter->format($data); |
95
|
1 |
|
} |
96
|
|
|
} |
97
|
|
|
|