QRDataModeAbstract.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Class QRDataModeAbstract
  4. *
  5. * @created 19.11.2020
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2020 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Data;
  11. use chillerlan\QRCode\Common\Mode;
  12. /**
  13. * abstract methods for the several data modes
  14. */
  15. abstract class QRDataModeAbstract implements QRDataModeInterface{
  16. /**
  17. * The data to write
  18. */
  19. protected string $data;
  20. /**
  21. * QRDataModeAbstract constructor.
  22. *
  23. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  24. */
  25. public function __construct(string $data){
  26. $data = $this::convertEncoding($data);
  27. if(!$this::validateString($data)){
  28. throw new QRCodeDataException('invalid data');
  29. }
  30. $this->data = $data;
  31. }
  32. /**
  33. * returns the character count of the $data string
  34. */
  35. protected function getCharCount():int{
  36. return strlen($this->data);
  37. }
  38. public static function convertEncoding(string $string):string{
  39. return $string;
  40. }
  41. /**
  42. * shortcut
  43. */
  44. protected static function getLengthBits(int $versionNumber):int{
  45. return Mode::getLengthBitsForVersion(static::DATAMODE, $versionNumber);
  46. }
  47. }