-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathItemsOptions.php
115 lines (99 loc) · 3.28 KB
/
ItemsOptions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace JsonMachine;
use JsonMachine\Exception\InvalidArgumentException;
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\JsonDecoder\ItemDecoder;
class ItemsOptions extends \ArrayObject
{
private $options = [];
/**
* @throws InvalidArgumentException
*/
public function __construct(array $options = [])
{
$this->validateOptions($options);
parent::__construct($this->options);
}
public function toArray(): array
{
return $this->options;
}
/**
* @throws InvalidArgumentException
*/
private function validateOptions(array $options)
{
$mergedOptions = array_merge(self::defaultOptions(), $options);
try {
foreach ($mergedOptions as $optionName => $optionValue) {
if ( ! isset(self::defaultOptions()[$optionName])) {
$exceptionMessage = "Option '$optionName' does not exist.";
$suggestion = self::getSuggestion(array_keys(self::defaultOptions()), $optionName);
if ($suggestion) {
$exceptionMessage .= " Did you mean '$suggestion'?";
}
throw new InvalidArgumentException($exceptionMessage);
}
$this->options[$optionName] = $this->{"opt_$optionName"}($optionValue);
}
} catch (\TypeError $typeError) {
throw new InvalidArgumentException(
preg_replace('~Argument #[0-9]+~', "Option '$optionName'", $typeError->getMessage())
);
}
}
private function opt_pointer($pointer)
{
if (is_array($pointer)) {
(function (string ...$p) {})(...$pointer);
} else {
(function (string $p) {})($pointer);
}
return $pointer;
}
private function opt_decoder(?ItemDecoder $decoder = null)
{
return $decoder;
}
private function opt_debug(bool $debug)
{
return $debug;
}
public static function defaultOptions(): array
{
return [
'pointer' => '',
'decoder' => new ExtJsonDecoder(),
'debug' => false,
];
}
/**
* From Nette ObjectHelpers.
*
* @see https://github.com/nette/utils/blob/master/src/Utils/ObjectHelpers.php
*
* Finds the best suggestion (for 8-bit encoding).
*
* @param (\ReflectionFunctionAbstract|\ReflectionParameter|\ReflectionClass|\ReflectionProperty|string)[] $possibilities
*
* @internal
*/
private static function getSuggestion(array $possibilities, string $value): ?string
{
$norm = preg_replace($re = '#^(get|set|has|is|add)(?=[A-Z])#', '+', $value);
$best = null;
$min = (strlen($value) / 4 + 1) * 10 + .1;
foreach (array_unique($possibilities, SORT_REGULAR) as $item) {
$item = $item instanceof \Reflector ? $item->name : $item;
if ($item !== $value && (
($len = levenshtein($item, $value, 10, 11, 10)) < $min
|| ($len = levenshtein(preg_replace($re, '*', $item), $norm, 10, 11, 10)) < $min
)) {
$min = $len;
$best = $item;
}
}
return $best;
}
}