diff options
author | Arnold Daniels <arnold@jasny.net> | 2017-01-26 11:35:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-26 11:35:16 +0100 |
commit | 51c88bded1a907b19e44344de35871822ba95829 (patch) | |
tree | afa90e7d24ebe2d7eadb87945fd3f07c66afdb15 /src/Controller/ContentNegotiation.php | |
parent | 9701118b9b34e19d5de1fe56755afdd29c0c93a1 (diff) | |
parent | 36cece638bb83b9755eee620d5132893aa969b28 (diff) | |
download | controller-1.1.0.zip controller-1.1.0.tar.gz controller-1.1.0.tar.bz2 |
Merge pull request #12 from Minstel/11-Add_content_negotiationv1.1.0
Add content negotiation (fixes #11)
Diffstat (limited to 'src/Controller/ContentNegotiation.php')
-rw-r--r-- | src/Controller/ContentNegotiation.php | 108 |
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'; + } +} |