Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

112 lines
4.0KB

  1. <?php
  2. namespace App\Transmission\Layouts;
  3. use Illuminate\Http\Client\Response;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Validator;
  6. class IFCommonHeader
  7. {
  8. public string $customerCode = '';
  9. public string $parkingManagementCode = '';
  10. public int $adjusterSerialNo = 0;
  11. public string $interfaceId = '';
  12. public string $sendDatetime = '';
  13. public string $resultCode = '';
  14. const COL_NAME_HEADER = 'Header';
  15. const COL_NAME_BODY = 'Body';
  16. const COL_NAME_CUSTOMER_CODE = 'CustomerCode';
  17. const COL_NAME_PARKING_MANAGEMENT_CODE = 'ParkingManagementCode';
  18. const COL_NAME_ADJUSTER_SERIAL_NO = 'AdjusterSerialNo';
  19. const COL_NAME_INTERFACE_ID = 'InterfaceID';
  20. const COL_NAME_SEND_DATETIME = 'SendDatetime';
  21. const COL_NAME_RESULT_CODE = 'ResultCode';
  22. private static function getRules()
  23. {
  24. return [
  25. self::COL_NAME_HEADER => 'required|array',
  26. self::COL_NAME_HEADER . "." . self::COL_NAME_CUSTOMER_CODE => 'required|string',
  27. self::COL_NAME_HEADER . "." . self::COL_NAME_PARKING_MANAGEMENT_CODE => 'required|string',
  28. self::COL_NAME_HEADER . "." . self::COL_NAME_ADJUSTER_SERIAL_NO => 'required|numeric',
  29. self::COL_NAME_HEADER . "." . self::COL_NAME_INTERFACE_ID => 'required|string',
  30. self::COL_NAME_HEADER . "." . self::COL_NAME_SEND_DATETIME => 'required|string',
  31. self::COL_NAME_HEADER . "." . self::COL_NAME_RESULT_CODE => 'required|string',
  32. ];
  33. }
  34. public static function validate(array $data)
  35. {
  36. $validator = Validator::make($data, self::getRules());
  37. $validator->validate();
  38. }
  39. public static function validateLayout(Request $request)
  40. {
  41. $request->validate(self::getRules());
  42. }
  43. /**
  44. * Requestはバリデート済みである前提
  45. *
  46. * @param Request $request
  47. * @return IFCommonHeader
  48. */
  49. public static function createFromRequest(Request $request): self
  50. {
  51. $requestHeader = $request[self::COL_NAME_HEADER];
  52. $header = new IFCommonHeader();
  53. $header->customerCode = $requestHeader[self::COL_NAME_CUSTOMER_CODE];
  54. $header->parkingManagementCode = $requestHeader[self::COL_NAME_PARKING_MANAGEMENT_CODE];
  55. $header->adjusterSerialNo = $requestHeader[self::COL_NAME_ADJUSTER_SERIAL_NO];
  56. $header->interfaceId = $requestHeader[self::COL_NAME_INTERFACE_ID];
  57. $header->sendDatetime = $requestHeader[self::COL_NAME_SEND_DATETIME];
  58. return $header;
  59. }
  60. /**
  61. * Requestはバリデート済みである前提
  62. *
  63. * @param Response $request
  64. * @return IFCommonHeader
  65. */
  66. public static function createFromResponse(Response $response): self
  67. {
  68. $responseHeader = $response[self::COL_NAME_HEADER];
  69. $header = new IFCommonHeader();
  70. $header->customerCode = $responseHeader[self::COL_NAME_CUSTOMER_CODE];
  71. $header->parkingManagementCode = $responseHeader[self::COL_NAME_PARKING_MANAGEMENT_CODE];
  72. $header->adjusterSerialNo = $responseHeader[self::COL_NAME_ADJUSTER_SERIAL_NO];
  73. $header->interfaceId = $responseHeader[self::COL_NAME_INTERFACE_ID];
  74. $header->sendDatetime = $responseHeader[self::COL_NAME_SEND_DATETIME];
  75. $header->resultCode = $responseHeader[self::COL_NAME_RESULT_CODE];
  76. return $header;
  77. }
  78. public function toArray(): array
  79. {
  80. $result = [];
  81. $result[self::COL_NAME_CUSTOMER_CODE] = $this->customerCode;
  82. $result[self::COL_NAME_PARKING_MANAGEMENT_CODE] = $this->parkingManagementCode;
  83. $result[self::COL_NAME_ADJUSTER_SERIAL_NO] = $this->adjusterSerialNo;
  84. $result[self::COL_NAME_INTERFACE_ID] = $this->interfaceId;
  85. $result[self::COL_NAME_SEND_DATETIME] = $this->sendDatetime;
  86. $result[self::COL_NAME_RESULT_CODE] = $this->resultCode;
  87. return $result;
  88. }
  89. public function ok(): bool
  90. {
  91. return $this->resultCode === '100';
  92. }
  93. public function ng(): bool
  94. {
  95. return !$this->ok();
  96. }
  97. }