Config.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace NavOnlineInvoice;
  3. use Exception;
  4. class Config {
  5. const TEST_URL = 'https://api-test.onlineszamla.nav.gov.hu/invoiceService/v3';
  6. const PROD_URL = 'https://api.onlineszamla.nav.gov.hu/invoiceService/v3';
  7. public $user;
  8. public $software;
  9. public $baseUrl;
  10. public $verifySSL = true;
  11. public $validateApiSchema = true;
  12. public $curlTimeout = null;
  13. /** @var RequestIdGeneratorInterface */
  14. public $requestIdGenerator;
  15. /**
  16. * NavOnlineInvoice Reporter osztály számára szükséges konfigurációs objektum készítése
  17. *
  18. * @param string $baseUrl NAV API URL
  19. * @param array|string $user User data array vagy json fájlnév
  20. * @param array|string $software Software data array vagy json fájlnév
  21. * @throws \Exception
  22. */
  23. function __construct($baseUrl, $user, $software) {
  24. if (!$baseUrl) {
  25. throw new Exception("A baseUrl paraméter megadása kötelező!");
  26. }
  27. if (!$software) {
  28. throw new Exception("A 2.0-ás API-tól a Software adatok megadása kötelező!");
  29. }
  30. $this->setBaseUrl($baseUrl);
  31. if (!$user) {
  32. throw new Exception("A user paraméter megadása kötelező!");
  33. }
  34. if (is_string($user)) {
  35. $this->loadUser($user);
  36. } else {
  37. $this->setUser($user);
  38. }
  39. if (is_string($software)) {
  40. $this->loadSoftware($software);
  41. } else {
  42. $this->setSoftware($software);
  43. }
  44. $this->setRequestIdGenerator(new RequestIdGeneratorBasic());
  45. }
  46. function setRequestIdGenerator(RequestIdGeneratorInterface $obj) {
  47. $this->requestIdGenerator = $obj;
  48. }
  49. function getRequestIdGenerator() {
  50. return $this->requestIdGenerator;
  51. }
  52. /**
  53. * NAV online számla API eléréséhez használt URL
  54. *
  55. * Teszt: https://api-test.onlineszamla.nav.gov.hu/invoiceService/v3
  56. * Éles: https://api.onlineszamla.nav.gov.hu/invoiceService/v3
  57. *
  58. * @param string $baseUrl NAV eléréséhez használt környezet
  59. */
  60. public function setBaseUrl($baseUrl) {
  61. $this->baseUrl = $baseUrl;
  62. }
  63. /**
  64. * NAV szerverrel való kommunikáció előtt ellenőrizze az XML adatot az API sémával szemben
  65. *
  66. * @param boolean $flag
  67. */
  68. public function useApiSchemaValidation($flag = true) {
  69. $this->validateApiSchema = $flag;
  70. }
  71. /**
  72. *
  73. * @param array $data
  74. */
  75. public function setSoftware($data) {
  76. $this->software = $data;
  77. }
  78. /**
  79. *
  80. * @param string $jsonFile JSON file name
  81. */
  82. public function loadSoftware($jsonFile) {
  83. $data = $this->loadJsonFile($jsonFile);
  84. $this->setSoftware($data);
  85. }
  86. /**
  87. *
  88. * @param array $data
  89. */
  90. public function setUser($data) {
  91. $this->user = $data;
  92. }
  93. /**
  94. *
  95. * @param string $jsonFile JSON file name
  96. */
  97. public function loadUser($jsonFile) {
  98. $data = $this->loadJsonFile($jsonFile);
  99. $this->setUser($data);
  100. }
  101. /**
  102. * JSON fájl betöltése
  103. *
  104. * @param string $jsonFile
  105. * @return array
  106. * @throws \Exception
  107. */
  108. protected function loadJsonFile($jsonFile) {
  109. if (!file_exists($jsonFile)) {
  110. throw new Exception("A megadott json fájl nem létezik: $jsonFile");
  111. }
  112. $content = file_get_contents($jsonFile);
  113. $data = json_decode($content, true);
  114. if ($data === null) {
  115. throw new Exception("A megadott json fájlt nem sikerült dekódolni!");
  116. }
  117. return $data;
  118. }
  119. /**
  120. * cURL hívásnál timeout beállítása másodpercekben.
  121. * null vagy 0 esetén nincs explicit timeout beállítás
  122. *
  123. * @param null|int $timeoutSeconds
  124. */
  125. public function setCurlTimeout($timeoutSeconds) {
  126. $this->curlTimeout = $timeoutSeconds;
  127. }
  128. public static function getDataXsdFilename() {
  129. return __DIR__ . "/xsd/invoiceData.xsd";
  130. }
  131. public static function getAnnulmentXsdFilename() {
  132. return __DIR__ . "/xsd/invoiceAnnulment.xsd";
  133. }
  134. public static function getApiXsdFilename() {
  135. return __DIR__ . "/xsd/invoiceApi.xsd";
  136. }
  137. }