| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Endroid\QrCode\Builder; |
| 6 |
|
| 7 |
use Endroid\QrCode\Color\ColorInterface; |
| 8 |
use Endroid\QrCode\Encoding\EncodingInterface; |
| 9 |
use Endroid\QrCode\ErrorCorrectionLevel; |
| 10 |
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface; |
| 11 |
use Endroid\QrCode\Exception\ValidationException; |
| 12 |
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface; |
| 13 |
use Endroid\QrCode\Label\Font\FontInterface; |
| 14 |
use Endroid\QrCode\Label\Label; |
| 15 |
use Endroid\QrCode\Label\LabelAlignment; |
| 16 |
use Endroid\QrCode\Label\LabelInterface; |
| 17 |
use Endroid\QrCode\Label\Margin\MarginInterface; |
| 18 |
use Endroid\QrCode\Logo\Logo; |
| 19 |
use Endroid\QrCode\Logo\LogoInterface; |
| 20 |
use Endroid\QrCode\QrCode; |
| 21 |
use Endroid\QrCode\RoundBlockSizeMode; |
| 22 |
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface; |
| 23 |
use Endroid\QrCode\Writer\PngWriter; |
| 24 |
use Endroid\QrCode\Writer\Result\ResultInterface; |
| 25 |
use Endroid\QrCode\Writer\ValidatingWriterInterface; |
| 26 |
use Endroid\QrCode\Writer\WriterInterface; |
| 27 |
|
| 28 |
class Builder implements BuilderInterface |
| 29 |
{ |
| 30 |
/** |
| 31 |
* @var array<string, mixed>{ |
| 32 |
* data: string, |
| 33 |
* writer: WriterInterface, |
| 34 |
* writerOptions: array, |
| 35 |
* qrCodeClass: class-string, |
| 36 |
* logoClass: class-string, |
| 37 |
* labelClass: class-string, |
| 38 |
* validateResult: bool, |
| 39 |
* size?: int, |
| 40 |
* encoding?: EncodingInterface, |
| 41 |
* errorCorrectionLevel?: ErrorCorrectionLevelInterface, |
| 42 |
* roundBlockSizeMode?: RoundBlockSizeModeInterface, |
| 43 |
* margin?: int, |
| 44 |
* backgroundColor?: ColorInterface, |
| 45 |
* foregroundColor?: ColorInterface, |
| 46 |
* labelText?: string, |
| 47 |
* labelFont?: FontInterface, |
| 48 |
* labelAlignment?: LabelAlignmentInterface, |
| 49 |
* labelMargin?: MarginInterface, |
| 50 |
* labelTextColor?: ColorInterface, |
| 51 |
* logoPath?: string, |
| 52 |
* logoResizeToWidth?: int, |
| 53 |
* logoResizeToHeight?: int, |
| 54 |
* logoPunchoutBackground?: bool |
| 55 |
* } |
| 56 |
*/ |
| 57 |
private array $options; |
| 58 |
|
| 59 |
public function __construct() |
| 60 |
{ |
| 61 |
$this->options = [ |
| 62 |
'data' => '', |
| 63 |
'writer' => new PngWriter(), |
| 64 |
'writerOptions' => [], |
| 65 |
'qrCodeClass' => QrCode::class, |
| 66 |
'logoClass' => Logo::class, |
| 67 |
'labelClass' => Label::class, |
| 68 |
'validateResult' => false, |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
public static function create(): BuilderInterface |
| 73 |
{ |
| 74 |
return new self(); |
| 75 |
} |
| 76 |
|
| 77 |
public function writer(WriterInterface $writer): BuilderInterface |
| 78 |
{ |
| 79 |
$this->options['writer'] = $writer; |
| 80 |
|
| 81 |
return $this; |
| 82 |
} |
| 83 |
|
| 84 |
/** @param array<string, mixed> $writerOptions */ |
| 85 |
public function writerOptions(array $writerOptions): BuilderInterface |
| 86 |
{ |
| 87 |
$this->options['writerOptions'] = $writerOptions; |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
public function data(string $data): BuilderInterface |
| 93 |
{ |
| 94 |
$this->options['data'] = $data; |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
public function encoding(EncodingInterface $encoding): BuilderInterface |
| 100 |
{ |
| 101 |
$this->options['encoding'] = $encoding; |
| 102 |
|
| 103 |
return $this; |
| 104 |
} |
| 105 |
|
| 106 |
public function errorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BuilderInterface |
| 107 |
{ |
| 108 |
$this->options['errorCorrectionLevel'] = $errorCorrectionLevel; |
| 109 |
|
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
public function size(int $size): BuilderInterface |
| 114 |
{ |
| 115 |
$this->options['size'] = $size; |
| 116 |
|
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
public function margin(int $margin): BuilderInterface |
| 121 |
{ |
| 122 |
$this->options['margin'] = $margin; |
| 123 |
|
| 124 |
return $this; |
| 125 |
} |
| 126 |
|
| 127 |
public function roundBlockSizeMode(RoundBlockSizeMode $roundBlockSizeMode): BuilderInterface |
| 128 |
{ |
| 129 |
$this->options['roundBlockSizeMode'] = $roundBlockSizeMode; |
| 130 |
|
| 131 |
return $this; |
| 132 |
} |
| 133 |
|
| 134 |
public function foregroundColor(ColorInterface $foregroundColor): BuilderInterface |
| 135 |
{ |
| 136 |
$this->options['foregroundColor'] = $foregroundColor; |
| 137 |
|
| 138 |
return $this; |
| 139 |
} |
| 140 |
|
| 141 |
public function backgroundColor(ColorInterface $backgroundColor): BuilderInterface |
| 142 |
{ |
| 143 |
$this->options['backgroundColor'] = $backgroundColor; |
| 144 |
|
| 145 |
return $this; |
| 146 |
} |
| 147 |
|
| 148 |
public function logoPath(string $logoPath): BuilderInterface |
| 149 |
{ |
| 150 |
$this->options['logoPath'] = $logoPath; |
| 151 |
|
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
public function logoResizeToWidth(int $logoResizeToWidth): BuilderInterface |
| 156 |
{ |
| 157 |
$this->options['logoResizeToWidth'] = $logoResizeToWidth; |
| 158 |
|
| 159 |
return $this; |
| 160 |
} |
| 161 |
|
| 162 |
public function logoResizeToHeight(int $logoResizeToHeight): BuilderInterface |
| 163 |
{ |
| 164 |
$this->options['logoResizeToHeight'] = $logoResizeToHeight; |
| 165 |
|
| 166 |
return $this; |
| 167 |
} |
| 168 |
|
| 169 |
public function logoPunchoutBackground(bool $logoPunchoutBackground): BuilderInterface |
| 170 |
{ |
| 171 |
$this->options['logoPunchoutBackground'] = $logoPunchoutBackground; |
| 172 |
|
| 173 |
return $this; |
| 174 |
} |
| 175 |
|
| 176 |
public function labelText(string $labelText): BuilderInterface |
| 177 |
{ |
| 178 |
$this->options['labelText'] = $labelText; |
| 179 |
|
| 180 |
return $this; |
| 181 |
} |
| 182 |
|
| 183 |
public function labelFont(FontInterface $labelFont): BuilderInterface |
| 184 |
{ |
| 185 |
$this->options['labelFont'] = $labelFont; |
| 186 |
|
| 187 |
return $this; |
| 188 |
} |
| 189 |
|
| 190 |
public function labelAlignment(LabelAlignment $labelAlignment): BuilderInterface |
| 191 |
{ |
| 192 |
$this->options['labelAlignment'] = $labelAlignment; |
| 193 |
|
| 194 |
return $this; |
| 195 |
} |
| 196 |
|
| 197 |
public function labelMargin(MarginInterface $labelMargin): BuilderInterface |
| 198 |
{ |
| 199 |
$this->options['labelMargin'] = $labelMargin; |
| 200 |
|
| 201 |
return $this; |
| 202 |
} |
| 203 |
|
| 204 |
public function labelTextColor(ColorInterface $labelTextColor): BuilderInterface |
| 205 |
{ |
| 206 |
$this->options['labelTextColor'] = $labelTextColor; |
| 207 |
|
| 208 |
return $this; |
| 209 |
} |
| 210 |
|
| 211 |
public function validateResult(bool $validateResult): BuilderInterface |
| 212 |
{ |
| 213 |
$this->options['validateResult'] = $validateResult; |
| 214 |
|
| 215 |
return $this; |
| 216 |
} |
| 217 |
|
| 218 |
public function build(): ResultInterface |
| 219 |
{ |
| 220 |
$writer = $this->options['writer']; |
| 221 |
|
| 222 |
if ($this->options['validateResult'] && !$writer instanceof ValidatingWriterInterface) { |
| 223 |
throw ValidationException::createForUnsupportedWriter(strval(get_class($writer))); |
| 224 |
} |
| 225 |
|
| 226 |
/** @var QrCode $qrCode */ |
| 227 |
$qrCode = $this->buildObject($this->options['qrCodeClass']); |
| 228 |
|
| 229 |
/** @var LogoInterface|null $logo */ |
| 230 |
$logo = $this->buildObject($this->options['logoClass'], 'logo'); |
| 231 |
|
| 232 |
/** @var LabelInterface|null $label */ |
| 233 |
$label = $this->buildObject($this->options['labelClass'], 'label'); |
| 234 |
|
| 235 |
$result = $writer->write($qrCode, $logo, $label, $this->options['writerOptions']); |
| 236 |
|
| 237 |
if ($this->options['validateResult'] && $writer instanceof ValidatingWriterInterface) { |
| 238 |
$writer->validateResult($result, $qrCode->getData()); |
| 239 |
} |
| 240 |
|
| 241 |
return $result; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @param class-string $class |
| 246 |
* |
| 247 |
* @return mixed |
| 248 |
*/ |
| 249 |
private function buildObject(string $class, string $optionsPrefix = null) |
| 250 |
{ |
| 251 |
/** @var \ReflectionClass<object> $reflectionClass */ |
| 252 |
$reflectionClass = new \ReflectionClass($class); |
| 253 |
|
| 254 |
$arguments = []; |
| 255 |
$hasBuilderOptions = false; |
| 256 |
$missingRequiredArguments = []; |
| 257 |
/** @var \ReflectionMethod $constructor */ |
| 258 |
$constructor = $reflectionClass->getConstructor(); |
| 259 |
$constructorParameters = $constructor->getParameters(); |
| 260 |
foreach ($constructorParameters as $parameter) { |
| 261 |
$optionName = null === $optionsPrefix ? $parameter->getName() : $optionsPrefix.ucfirst($parameter->getName()); |
| 262 |
if (isset($this->options[$optionName])) { |
| 263 |
$hasBuilderOptions = true; |
| 264 |
$arguments[] = $this->options[$optionName]; |
| 265 |
} elseif ($parameter->isDefaultValueAvailable()) { |
| 266 |
$arguments[] = $parameter->getDefaultValue(); |
| 267 |
} else { |
| 268 |
$missingRequiredArguments[] = $optionName; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
if (!$hasBuilderOptions) { |
| 273 |
return null; |
| 274 |
} |
| 275 |
|
| 276 |
if (count($missingRequiredArguments) > 0) { |
| 277 |
throw new \Exception(sprintf('Missing required arguments: %s', implode(', ', $missingRequiredArguments))); |
| 278 |
} |
| 279 |
|
| 280 |
return $reflectionClass->newInstanceArgs($arguments); |
| 281 |
} |
| 282 |
} |