領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
1.9KB

  1. <?php
  2. namespace App\Http\Controllers\Web;
  3. use Illuminate\Support\Str;
  4. class RuleAnalyzer
  5. {
  6. static public function convertToRegEx(array $rules)
  7. {
  8. $ret = [];
  9. foreach ($rules as $name => $ruleList) {
  10. $pattern = '/^' . Str::replace('*', '\d+', Str::replace('.', '\.', $name)) . '$/';
  11. $ret[$pattern] = $ruleList;
  12. }
  13. return $ret;
  14. }
  15. private string $path;
  16. private string $pattern;
  17. private int|null $arrayIndex = null;
  18. private bool $mathed = false;
  19. private array $rules;
  20. public function __construct(string $path, array &$rules)
  21. {
  22. $this->path = $path;
  23. // パターンマッチング
  24. foreach ($rules as $pattern => $ruleList) {
  25. if (preg_match($pattern, $path, $matcheds)) {
  26. $this->pattern = $pattern;
  27. $this->rules = $ruleList;
  28. $this->mathed = true;
  29. break;
  30. }
  31. }
  32. if (!$this->mathed) {
  33. return;
  34. }
  35. // 配列インデックスの取得
  36. $this->arrayIndex = $this->getArrayIndexFromPath($path);
  37. }
  38. public function isMathed()
  39. {
  40. return $this->mathed;
  41. }
  42. public function getRules()
  43. {
  44. return $this->rules;
  45. }
  46. public function getType()
  47. {
  48. return $this->rules[1];
  49. }
  50. public function isArrayMember()
  51. {
  52. return $this->arrayIndex !== null;
  53. }
  54. public function getArrayIndex()
  55. {
  56. return $this->arrayIndex;
  57. }
  58. private function getArrayIndexFromPath(string $path)
  59. {
  60. preg_match('/^.+\.(\d+)\.[0-9A-Za-z_]+$/', $path, $matches);
  61. if (count($matches) === 0) {
  62. return null;
  63. }
  64. return intval($matches[1]);
  65. }
  66. public function getArrayName()
  67. {
  68. $list = explode('.*.', $this->pattern);
  69. array_pop($list);
  70. return implode('.*.');
  71. }
  72. }