/
jagepard
/
Rudra-Annotation
Обзор
Документация
Войти
/
jagepard
/
Rudra-Annotation
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/ParamsExtractor.php
67 строк
2 KB
Jagepard
fix
05 июн 2026, 16:02
05 июн 2026, 16:02
ade0713
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. * * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> * @license https://mozilla.org/MPL/2.0/ MPL-2.0 */ namespace Rudra\Annotation; class ParamsExtractor { /** * Parses an array of parameter strings into an associative array * * `from: "param1, param2 = 'param2', param3={param1;param2:'param2'}"` * `to: ["param1", "param2" => "param2", "param3" => ["param1", "param2" => "param2"]]` */ public function getParams(array $exploded, string $assignment): array { $processed = []; foreach ($exploded as $key => $item) { if (str_contains($item, $assignment)) { $item = $this->handleData($item, explode($assignment, $item, 2)); } $processed += is_array($item) ? $item : [$key => $item]; } return $processed; } /** * Parses data into `key => value` pairs * * ⚠️ IMPORTANT: Values inside arrays (curly braces) * must not contain the array assignment mark (`:`) * * @codeCoverageIgnore */ private function handleData(string $data, array $exploded): ?array { // If in data an array of type param3={param1;param2:'param2'} if (preg_match("/=\s*{/", $data) && preg_match("/{(.*)}/", $exploded[1], $matches)) { return [ trim($exploded[0]) => $this->getParams( explode(Annotation::DELIMITER["array"], trim($matches[1])), Annotation::ASSIGNMENT["array"] ), ]; } /** * Remove quotation marks around parameter * matches[1] = 'param2'; */ if (preg_match("/'(.*)'/", $exploded[1], $matches)) { return [trim($exploded[0]) => $matches[1]]; } return null; } }