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