Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

148 lines
3.6KB

  1. <?php
  2. namespace App\Http\API\SMBC;
  3. use App\Codes\EnvironmentName;
  4. use App\Util\EncodingUtil;
  5. use Exception;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Http;
  10. use Illuminate\Support\Str;
  11. class PollResult
  12. {
  13. // 共通
  14. private const IDX_RECORD_CODE = 0;
  15. // ヘッダー
  16. private const IDX_HEADER_RESULT = 1;
  17. private const IDX_HEADER_MESSAGE = 2;
  18. // ボディー
  19. private const IDX_FOOTER_COUNT = 1;
  20. // フッター
  21. private const RECORD_CODE_HEADER = "10";
  22. private const RECORD_CODE_BODY = "20";
  23. private const RECORD_CODE_FOOTER = "80";
  24. private const RESULT_CODE_SUCCESS = ["000000", "943037"];
  25. /**
  26. * @var Collection<int, Collection<int, string>>
  27. */
  28. private Collection $lines;
  29. /**
  30. * @var Collection<int, PollResultRecord>
  31. */
  32. private Collection $body;
  33. private bool $success = false;
  34. private string $message = "";
  35. private int $count = 0;
  36. public function __construct(string $data)
  37. {
  38. $this->lines = collect();
  39. $lines = Str::of($data)->replace('"', '')->explode("\r\n");
  40. foreach ($lines as $lineStr) {
  41. if ($lineStr) {
  42. $lineStr = EncodingUtil::toUtf8FromSjis($lineStr);
  43. $this->lines->push(Str::of($lineStr)->explode(','));
  44. }
  45. }
  46. if (!$this->parseHeader()) {
  47. return;
  48. }
  49. if (!$this->parseBody()) {
  50. return;
  51. }
  52. if (!$this->parseFooter()) {
  53. return;
  54. }
  55. $this->success = true;
  56. }
  57. public function ok(): bool
  58. {
  59. return $this->success;
  60. }
  61. public function getMessage(): string
  62. {
  63. return $this->message;
  64. }
  65. public function getRecord()
  66. {
  67. return $this->body;
  68. }
  69. public function getCount(): int
  70. {
  71. return $this->count;
  72. }
  73. private function parseHeader(): bool
  74. {
  75. $header = $this->lines->first();
  76. if (!$header) {
  77. $this->success = false;
  78. $this->message = "ヘッダーなし";
  79. return false;
  80. }
  81. $resultCode = $header->get(self::IDX_HEADER_RESULT);
  82. if (!in_array($resultCode, self::RESULT_CODE_SUCCESS)) {
  83. $this->success = false;
  84. $this->message = sprintf("結果コードNG %s", $resultCode);
  85. $readMessage = EncodingUtil::toUtf8FromSjis($header->get(self::IDX_HEADER_MESSAGE));
  86. return false;
  87. }
  88. return true;
  89. }
  90. private function parseBody(): bool
  91. {
  92. $this->body = collect();
  93. try {
  94. foreach ($this->lines as $line) {
  95. if ($line[self::IDX_RECORD_CODE] === self::RECORD_CODE_BODY) {
  96. $this->body->push(new PollResultRecord($line));
  97. }
  98. }
  99. } catch (Exception $e) {
  100. $this->success = false;
  101. $this->message = sprintf("Bodyパース失敗 %s", $e->getMessage());
  102. return false;
  103. }
  104. return true;
  105. }
  106. private function parseFooter(): bool
  107. {
  108. $footer = $this->lines->last();
  109. if (!$footer) {
  110. $this->success = false;
  111. $this->message = "フッターなし";
  112. return false;
  113. }
  114. $count = intval($footer->get(self::IDX_FOOTER_COUNT));
  115. if ($count !== $this->body->count()) {
  116. $this->success = false;
  117. $this->message = sprintf("読込件数に差異あり %d : %d", $count, $this->body->count());
  118. return false;
  119. }
  120. $this->count = $count;
  121. return true;
  122. }
  123. }