/** * @throws InvalidKey When an invalid key is encountered * @throws InvalidValue When an invalid value is encountered */ private static function flattenNestedArray(array $nestedArray, string $prefix = '') : array { $flatArray = []; foreach ($nestedArray as $key => $value) { if (!is_string($key) && ('' === $prefix || !is_int($key))) { throw InvalidKey::fromNonNestedKey($key); } if ('' !== $prefix) { $key = $prefix . '[' . $key . ']'; } if (is_string($value)) { $flatArray[$key] = $value; continue; } if (is_array($value)) { $flatArray += self::flattenNestedArray($value, $key); continue; } throw InvalidValue::fromNonNestedValue($value); } return $flatArray; }
public function testFromNonNestedKey() { $this->assertSame('Expected string or nested integer key, but "boolean" was provided', InvalidKey::fromNonNestedKey(true)->getMessage()); }