QRDataBase.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. *
  4. * @filesource QRDataBase.php
  5. * @created 25.11.2015
  6. * @package codemasher\QRCode
  7. * @author Smiley <smiley@chillerlan.net>
  8. * @copyright 2015 Smiley
  9. * @license MIT
  10. */
  11. namespace codemasher\QRCode\Data;
  12. use codemasher\QRCode\BitBuffer;
  13. use codemasher\QRCode\QRConst;
  14. use codemasher\QRCode\QRCodeException;
  15. use codemasher\QRCode\Util;
  16. use codemasher\QRCode\Data\QRDataInterface;
  17. /**
  18. * Class QRDataBase
  19. */
  20. class QRDataBase implements QRDataInterface{
  21. /**
  22. * @var
  23. */
  24. protected $mode;
  25. /**
  26. * @var
  27. */
  28. protected $data;
  29. /**
  30. * @var \codemasher\QRCode\Util
  31. */
  32. protected $util;
  33. /**
  34. * QRDataBase constructor.
  35. *
  36. * @param $data
  37. */
  38. public function __construct($data){
  39. $this->data = $data;
  40. $this->util = new Util;
  41. }
  42. /**
  43. * @return mixed
  44. */
  45. public function getMode(){
  46. return $this->mode;
  47. }
  48. /**
  49. * @return mixed
  50. */
  51. public function getData(){
  52. return $this->data;
  53. }
  54. /**
  55. * @throws \codemasher\QRCode\QRCodeException
  56. */
  57. public function getLength(){
  58. throw new QRCodeException('not implemented.');
  59. }
  60. /**
  61. * @param $buffer
  62. *
  63. * @throws \codemasher\QRCode\QRCodeException
  64. */
  65. public function write(BitBuffer &$buffer){
  66. throw new QRCodeException('not implemented.');
  67. }
  68. /**
  69. * @param $type
  70. *
  71. * @return int
  72. * @throws \codemasher\QRCode\QRCodeException
  73. */
  74. public function getLengthInBits($type){
  75. if(1 <= $type && $type < 10){
  76. // 1 - 9
  77. switch($this->mode){
  78. case QRConst::MODE_NUMBER : return 10;
  79. case QRConst::MODE_ALPHA_NUM: return 9;
  80. case QRConst::MODE_8BIT_BYTE: return 8;
  81. case QRConst::MODE_KANJI : return 8;
  82. default :
  83. throw new QRCodeException('mode: '.$this->mode);
  84. }
  85. }
  86. else if($type < 27){
  87. // 10 - 26
  88. switch($this->mode){
  89. case QRConst::MODE_NUMBER : return 12;
  90. case QRConst::MODE_ALPHA_NUM: return 11;
  91. case QRConst::MODE_8BIT_BYTE: return 16;
  92. case QRConst::MODE_KANJI : return 10;
  93. default :
  94. throw new QRCodeException('mode: '.$this->mode);
  95. }
  96. }
  97. else if($type < 41){
  98. // 27 - 40
  99. switch($this->mode){
  100. case QRConst::MODE_NUMBER : return 14;
  101. case QRConst::MODE_ALPHA_NUM: return 13;
  102. case QRConst::MODE_8BIT_BYTE: return 16;
  103. case QRConst::MODE_KANJI : return 12;
  104. default :
  105. throw new QRCodeException('mode: '.$this->mode);
  106. }
  107. }
  108. else{
  109. throw new QRCodeException('mode: '.$this->mode);
  110. }
  111. }
  112. }