<?php
namespace Engine\Mvc\Response\Components; class Headers { private $headers = []; private $json = null; public function __construct($headers) { $this->headers = $headers; } public function setJson($json) { $this->json = $json; } public function getJson() { if ($this->json===null) { $this->json = new \stdClass(); } return $this->json; } public function append($name, $value) { $this->headers[] = sprintf("%s: %s", $name, $value); } public function loadMessages(\App\MessagesCollector $msgs) { $jsonMessages = $this->getJson(); if (property_exists($jsonMessages, 'messages')) { $displayedMessages = (array)$jsonMessages->messages; } else { $displayedMessages = []; } foreach($msgs->getMessages() as $message) { $displayedMessages[] = ['type' => $message->getType(), 'message' => $message->getText()]; } $jsonMessages->messages = $displayedMessages; $this->setJson($jsonMessages); } public function getAll() { $headers = []; foreach ($this->headers as $header) { $headers[] = $header; } if ($this->json!==null && $this->json!=new \stdClass()) { $headers[] = sprintf("X-JSON: %s", json_encode((object)$this->json)); } return $headers; } } 