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