This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
AcceptedModel.php
135 lines (114 loc) · 3.73 KB
/
AcceptedModel.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/
namespace Zend\Mvc\Controller\Plugin;
use Zend\Http\Request;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\View\Model\JsonModel;
use Zend\View\Model\FeedModel;
use Zend\View\Model\ModelInterface;
use Zend\View\Model\ViewModel;
use Zend\Mvc\InjectApplicationEventInterface;
use Zend\Mvc\MvcEvent;
/**
* @category Zend
* @package Zend_Mvc
* @subpackage Controller
*/
class AcceptedModel extends AbstractPlugin
{
protected $event;
protected $request;
protected $detected;
/**
* Default match string to match against. Could be set 'somewhere'
*
* @var String
*/
protected $defaultMatchArray;
/**
* Detects an appropriate model for view.
*
* @return ModelInterface
*/
public function __invoke(array $matchAgainst = null, $returnMatchedAcceptTypeOnly = false)
{
$this->detected = 'Zend\View\Model\ViewModel';
$request = $this->getRequest();
$headers = $request->getHeaders();
if ((!$matchAgainst && !$this->defaultMatchString) || !$headers->has('accept')) {
return new $this->detected;
}
if (!$matchAgainst) {
$matchAgainst = $this->defaultMatchString;
}
$matchAgainstString = '';
foreach ($matchAgainst as $modelName => $modelStrings) {
$modelName = str_replace('\\', '|', $modelName);
foreach ((array) $modelStrings as $modelString) {
$matchAgainstString .= $modelString
. '; _internalViewModel="' . $modelName . '", ';
}
}
/** @var $accept \Zend\Http\Header\Accept */
$accept = $headers->get('Accept');
if (($res = $accept->match($matchAgainstString)) === false) {
return new $this->detected;
}
if ($returnMatchedAcceptTypeOnly) {
return $res;
}
//@todo return actual instance, make sure to preserve \
$modelName = $res->matchedAgainst->params['_internalViewModel'];
return str_replace('|', '\\', $modelName);
}
/**
* Get the request
*
* @return Request
* @throws Exception\DomainException if unable to find request
*/
protected function getRequest()
{
if ($this->request) {
return $this->request;
}
$event = $this->getEvent();
$request = $event->getRequest();
if (!$request instanceof Request) {
throw new Exception\DomainException('AcceptedModel plugin requires event compose a request');
}
$this->request = $request;
return $this->request;
}
/**
* Get the event
*
* @return MvcEvent
* @throws Exception\DomainException if unable to find event
*/
protected function getEvent()
{
if ($this->event) {
return $this->event;
}
$controller = $this->getController();
if (!$controller instanceof InjectApplicationEventInterface) {
throw new Exception\DomainException('AcceptedModel plugin requires a controller that implements InjectApplicationEventInterface');
}
$event = $controller->getEvent();
if (!$event instanceof MvcEvent) {
$params = $event->getParams();
$event = new MvcEvent();
$event->setParams($params);
}
$this->event = $event;
return $this->event;
}
}