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

ParsingDispatcher.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Common\Input;
8
9 use eZ\Publish\Core\REST\Common\Exceptions;
10
14 class ParsingDispatcher
15 {
32 protected $parsers = [];
33
39 public function __construct(array $parsers = [])
40 {
41 foreach ($parsers as $mediaType => $parser) {
42 $this->addParser($mediaType, $parser);
43 }
44 }
45
52 public function addParser($mediaType, Parser $parser)
53 {
54 list($mediaType, $version) = $this->parseMediaTypeVersion($mediaType);
55 $this->parsers[$mediaType][$version] = $parser;
56 }
57
66 public function parse(array $data, $mediaType)
67 {
68 list($mediaType, $version) = $this->parseMediaTypeVersion($mediaType);
69
70 // Remove encoding type
71 if (($plusPos = strrpos($mediaType, '+')) !== false) {
72 $mediaType = substr($mediaType, 0, $plusPos);
73 }
74
75 if (!isset($this->parsers[$mediaType][$version])) {
76 throw new Exceptions\Parser("Unknown content type specification: '{$mediaType} (version: $version)'.");
77 }
78
79 return $this->parsers[$mediaType][$version]->parse($data, $this);
80 }
81
89 protected function parseMediaTypeVersion($mediaType)
90 {
91 $version = '1.0';
92 $contentType = explode('; ', $mediaType);
93 if (count($contentType) > 1) {
94 $mediaType = $contentType[0];
95 foreach (array_slice($contentType, 1) as $parameterString) {
96 if (strpos($contentType[1], '=') === false) {
97 throw new Exceptions\Parser("Unknown parameter format: '{$parameterString}'");
98 }
99 list($parameterName, $parameterValue) = explode('=', $parameterString);
100 if (trim($parameterName) === 'version') {
101 $version = trim($parameterValue);
102 break;
103 }
104 }
105 }
106
107 return [$mediaType, $version];
108 }
109 }
eZ\Publish\Core\REST\Common\Exceptions\Parser
Exception thrown if a parser discovers an error.
Definition: REST/Common/Exceptions/Parser.php:15
eZ\Publish\Core\REST\Common\Input\ParsingDispatcher
Parsing dispatcher.
Definition: ParsingDispatcher.php:15
eZ\Publish\Core\REST\Common\Input\ParsingDispatcher\parse
parse(array $data, $mediaType)
Parses the given $data according to $mediaType.
Definition: ParsingDispatcher.php:66
eZ\Publish\Core\REST\Common\Input\ParsingDispatcher\parseMediaTypeVersion
parseMediaTypeVersion($mediaType)
Parses and returns the version from a MediaType.
Definition: ParsingDispatcher.php:89
eZ\Publish\Core\REST\Common\Input\ParsingDispatcher\$parsers
$parsers
Definition: ParsingDispatcher.php:32
eZ\Publish\Core\REST\Common\Input\ParsingDispatcher\addParser
addParser($mediaType, Parser $parser)
Adds another parser for the given Content Type.
Definition: ParsingDispatcher.php:52
eZ\Publish\Core\REST\Common\Input\ParsingDispatcher\__construct
__construct(array $parsers=[])
Construct from optional parsers array.
Definition: ParsingDispatcher.php:39
eZ\Publish\Core\REST\Common\Exceptions
Definition: Core/REST/Common/Exceptions/ForbiddenException.php:7
eZ\Publish\Core\REST\Common\Input
Definition: BaseParser.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.