|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 5 | + * license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright |
| 7 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 8 | + * the Apache License, Version 2.0 (the "License"); you may |
| 9 | + * not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +declare(strict_types=1); |
| 23 | + |
| 24 | +namespace Elastic\Apm\Impl\Config; |
| 25 | + |
| 26 | +use Elastic\Apm\Impl\Log\LoggableToString; |
| 27 | +use Elastic\Apm\Impl\Util\TextUtil; |
| 28 | + |
| 29 | +/** |
| 30 | + * Code in this file is part of implementation internals and thus it is not covered by the backward compatibility. |
| 31 | + * |
| 32 | + * @internal |
| 33 | + * |
| 34 | + * @extends OptionParser<array<string>> |
| 35 | + */ |
| 36 | +final class KeyValuePairsOptionParser extends OptionParser |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @param string $rawValue |
| 40 | + * |
| 41 | + * @return array<string> |
| 42 | + */ |
| 43 | + public function parse(string $rawValue): array |
| 44 | + { |
| 45 | + // Value format: |
| 46 | + // key=value[,key=value[,...]] |
| 47 | + |
| 48 | + // Treat empty string as zero key-value pairs |
| 49 | + if (TextUtil::isEmptyString($rawValue)) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + |
| 53 | + $pairs = explode(',', $rawValue); |
| 54 | + $result = []; |
| 55 | + foreach ($pairs as $keyValuePair) { |
| 56 | + $keyValueSeparatorPos = strpos($keyValuePair, '='); |
| 57 | + if ($keyValueSeparatorPos === false) { |
| 58 | + throw new ParseException('One of key-value pairs is missing key-value separator' . ' ;' . LoggableToString::convert(['keyValuePair' => $keyValuePair, 'rawValue' => $rawValue])); |
| 59 | + } |
| 60 | + $key = trim(substr($keyValuePair, /* offset */ 0, /* length */ $keyValueSeparatorPos)); |
| 61 | + $value = ($keyValueSeparatorPos === (strlen($keyValuePair) - 1)) ? '' : trim(substr($keyValuePair, /* offset */ $keyValueSeparatorPos + 1)); |
| 62 | + if (array_key_exists($key, $result)) { |
| 63 | + throw new ParseException( |
| 64 | + 'Key is present more than once' |
| 65 | + . ' ;' . LoggableToString::convert(['key' => $key, '1st value' => $result[$key], '2nd value' => $value, '2nd keyValuePair' => $keyValuePair, 'rawValue' => $rawValue]) |
| 66 | + ); |
| 67 | + } |
| 68 | + $result[$key] = $value; |
| 69 | + } |
| 70 | + return $result; |
| 71 | + } |
| 72 | +} |
0 commit comments