summaryrefslogtreecommitdiffstats
path: root/src/Controller/ContentNegotiation.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Controller/ContentNegotiation.php')
-rw-r--r--src/Controller/ContentNegotiation.php108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/Controller/ContentNegotiation.php b/src/Controller/ContentNegotiation.php
new file mode 100644
index 0000000..75aa4f1
--- /dev/null
+++ b/src/Controller/ContentNegotiation.php
@@ -0,0 +1,108 @@
+<?php
+
+namespace Jasny\Controller;
+
+/**
+ * Controller methods to negotiate content
+ */
+trait ContentNegotiation
+{
+ /**
+ * Get request, set for controller
+ *
+ * @return ServerRequestInterface
+ */
+ abstract public function getRequest();
+
+ /**
+ * Pick best content type
+ *
+ * @param array $priorities
+ * @return string
+ */
+ public function negotiateContentType(array $priorities)
+ {
+ return $this->negotiate($priorities);
+ }
+
+ /**
+ * Pick best language
+ *
+ * @param array $priorities
+ * @return string
+ */
+ public function negotiateLanguage(array $priorities)
+ {
+ return $this->negotiate($priorities, 'language');
+ }
+
+ /**
+ * Pick best encoding
+ *
+ * @param array $priorities
+ * @return string
+ */
+ public function negotiateEncoding(array $priorities)
+ {
+ return $this->negotiate($priorities, 'encoding');
+ }
+
+ /**
+ * Pick best charset
+ *
+ * @param array $priorities
+ * @return string
+ */
+ public function negotiateCharset(array $priorities)
+ {
+ return $this->negotiate($priorities, 'charset');
+ }
+
+ /**
+ * Generalize negotiation
+ *
+ * @param array $priorities
+ * @param string $type Negotiator type
+ * @return string
+ */
+ protected function negotiate(array $priorities, $type = '')
+ {
+ $header = 'Accept';
+
+ if ($type) {
+ $header .= '-' . ucfirst($type);
+ }
+
+ $header = $this->getRequest()->getHeader($header);
+ $header = join(', ', $header);
+
+ $negotiator = $this->getNegotiator($type);
+ $chosen = $negotiator->getBest($header, $priorities);
+
+ return $chosen ? $chosen->getType() : '';
+ }
+
+ /**
+ * Get negotiation library instance
+ *
+ * @param string $type Negotiator type
+ * @return Negotiation\AbstractNegotiator
+ */
+ protected function getNegotiator($type = '')
+ {
+ $class = $this->getNegotiatorName($type);
+
+ return new $class();
+ }
+
+ /**
+ * Get negotiator name
+ *
+ * @param string $type
+ * @return string
+ */
+ protected function getNegotiatorName($type = '')
+ {
+ return 'Negotiation\\' . ucfirst($type) . 'Negotiator';
+ }
+}