Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Visitor.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Common\Output;
8
9 use Symfony\Component\HttpFoundation\Response;
10
14 class Visitor
15 {
17 protected $valueObjectVisitorDispatcher = [];
18
24 protected $generator;
25
31 protected $response;
32
38 private $statusCode;
39
48 public function __construct(Generator $generator, ValueObjectVisitorDispatcher $valueObjectVisitorDispatcher)
49 {
50 $this->generator = $generator;
51 $this->valueObjectVisitorDispatcher = $valueObjectVisitorDispatcher;
52 $this->response = new Response('', 200);
53 }
54
64 public function setHeader($name, $value)
65 {
66 if (!$this->response->headers->has($name)) {
67 $this->response->headers->set($name, $value);
68 }
69 }
70
78 public function setStatus($statusCode)
79 {
80 if ($this->statusCode === null) {
81 $this->statusCode = $statusCode;
82 $this->response->setStatusCode($statusCode);
83 }
84 }
85
93 public function visit($data)
94 {
95 $this->generator->reset();
96 $this->generator->startDocument($data);
97
98 $this->visitValueObject($data);
99
100 //@todo Needs refactoring!
101 // A hackish solution to enable outer visitors to disable setting
102 // of certain headers in inner visitors, for example Accept-Patch header
103 // which is valid in GET/POST/PATCH for a resource, but must not appear
104 // in the list of resources
105 foreach ($this->response->headers->all() as $headerName => $headerValue) {
106 if ($headerValue[0] === false) {
107 $this->response->headers->remove($headerName);
108 }
109 }
110
111 $response = clone $this->response;
112
113 $response->setContent($this->generator->isEmpty() ? null : $this->generator->endDocument($data));
114
115 // reset the inner response
116 $this->response = new Response(null, 200);
117 $this->statusCode = null;
118
119 return $response;
120 }
121
131 public function visitValueObject($data)
132 {
133 $this->valueObjectVisitorDispatcher->setOutputGenerator($this->generator);
134 $this->valueObjectVisitorDispatcher->setOutputVisitor($this);
135
136 return $this->valueObjectVisitorDispatcher->visit($data);
137 }
138
148 public function getMediaType($type)
149 {
150 return $this->generator->getMediaType($type);
151 }
152
156 public function getResponse()
157 {
158 return $this->response;
159 }
160 }
eZ\Publish\Core\REST\Common\Output\Generator
Output generator.
Definition: REST/Common/Output/Generator.php:13
eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher
Dispatches value objects to a visitor depending on the class name.
Definition: ValueObjectVisitorDispatcher.php:13
eZ\Publish\Core\REST\Common\Output\Visitor
Visits a value object into an HTTP Response.
Definition: Visitor.php:15
eZ\Publish\Core\REST\Common\Output\Visitor\visitValueObject
visitValueObject($data)
Visit struct returned by controllers.
Definition: Visitor.php:131
eZ\Publish\Core\REST\Common\Output\Visitor\$valueObjectVisitorDispatcher
$valueObjectVisitorDispatcher
Definition: Visitor.php:17
eZ\Publish\Core\REST\Common\Output\Visitor\getResponse
getResponse()
Definition: Visitor.php:156
eZ\Publish\Core\REST\Common\Output\Visitor\getMediaType
getMediaType($type)
Generates a media type for $type based on the used generator.
Definition: Visitor.php:148
eZ\Publish\Core\REST\Common\Output\Visitor\$statusCode
$statusCode
Definition: Visitor.php:38
eZ\Publish\Core\REST\Common\Output\Visitor\setStatus
setStatus($statusCode)
Sets the given status code in the corresponding header.
Definition: Visitor.php:78
eZ\Publish\Core\REST\Common\Output\Visitor\$response
$response
Definition: Visitor.php:31
eZ\Publish\Core\REST\Common\Output\Visitor\visit
visit($data)
Visit struct returned by controllers.
Definition: Visitor.php:93
eZ\Publish\Core\REST\Common\Output\Visitor\__construct
__construct(Generator $generator, ValueObjectVisitorDispatcher $valueObjectVisitorDispatcher)
Construct from Generator and an array of concrete view model visitors.
Definition: Visitor.php:48
eZ\Publish\Core\REST\Common\Output\Visitor\$generator
$generator
Definition: Visitor.php:24
eZ\Publish\Core\REST\Common\Output\Visitor\setHeader
setHeader($name, $value)
Set HTTP response header.
Definition: Visitor.php:64
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.