1111 */
1212class Autolink
1313{
14- /**
15- * Property options.
16- *
17- * @var array
18- */
19- public array $ options = [
20- 'strip_scheme ' => false ,
21- 'text_limit ' => null ,
22- 'auto_title ' => false ,
23- 'escape ' => true ,
24- 'link_no_scheme ' => false
25- ];
14+ public AutolinkOptions $ options ;
2615
2716 /**
2817 * Property schemes.
@@ -64,12 +53,12 @@ class Autolink
6453 /**
6554 * Class init.
6655 *
67- * @param array $options Basic options.
68- * @param array $schemes
56+ * @param AutolinkOptions| array $options Basic options.
57+ * @param array $schemes
6958 */
70- public function __construct (array $ options = [], array $ schemes = [])
59+ public function __construct (AutolinkOptions | array $ options = [], array $ schemes = [])
7160 {
72- $ this ->options = array_merge ( $ this -> options , ( array ) $ options );
61+ $ this ->setOptions ( $ options );
7362
7463 $ this ->setSchemes (...array_merge ($ this ->schemes , $ schemes ));
7564 }
@@ -254,22 +243,26 @@ public function shorten(string $text, int $limit): string
254243
255244 public function stripScheme (bool $ value = false ): static
256245 {
257- return $ this ->setOption ('strip_scheme ' , $ value );
246+ $ this ->options ->stripScheme = $ value ;
247+
248+ return $ this ;
258249 }
259250
260251 public function isStripScheme (): bool
261252 {
262- return ( bool ) $ this ->getOption ( ' strip_scheme ' ) ;
253+ return $ this ->options -> stripScheme ;
263254 }
264255
265256 public function autoEscape (bool $ value = true ): static
266257 {
267- return $ this ->setOption ('escape ' , $ value );
258+ $ this ->options ->escape = $ value ;
259+
260+ return $ this ;
268261 }
269262
270263 public function isAutoEscape (): bool
271264 {
272- return (bool ) $ this ->getOption ( ' escape ' ) ;
265+ return (bool ) $ this ->options -> escape ;
273266 }
274267
275268 /**
@@ -279,12 +272,18 @@ public function isAutoEscape(): bool
279272 */
280273 public function textLimit (int |callable |null $ value = null ): static
281274 {
282- return $ this ->setOption ('text_limit ' , $ value );
275+ if (is_callable ($ value )) {
276+ $ value = $ value (...);
277+ }
278+
279+ $ this ->options ->textLimit = $ value ;
280+
281+ return $ this ;
283282 }
284283
285284 public function getTextLimit (): int |callable |null
286285 {
287- $ value = $ this ->getOption ( ' text_limit ' ) ;
286+ $ value = $ this ->options -> textLimit ;
288287
289288 // Fix for B/C
290289 if ($ value === false ) {
@@ -294,21 +293,16 @@ public function getTextLimit(): int|callable|null
294293 return $ value ;
295294 }
296295
297- /**
298- * autoTitle
299- *
300- * @param mixed $value
301- *
302- * @return static
303- */
304296 public function autoTitle (bool $ value = false ): static
305297 {
306- return $ this ->setOption ('auto_title ' , $ value );
298+ $ this ->options ->autoTitle = $ value ;
299+
300+ return $ this ;
307301 }
308302
309303 public function isAutoTitle (): bool
310304 {
311- return ( bool ) $ this ->getOption ( ' auto_title ' ) ;
305+ return $ this ->options -> autoTitle ;
312306 }
313307
314308 /**
@@ -320,12 +314,14 @@ public function isAutoTitle(): bool
320314 */
321315 public function linkNoScheme (bool |string $ value = false ): static
322316 {
323- return $ this ->setOption ('link_no_scheme ' , $ value );
317+ $ this ->options ->linkNoScheme = $ value ;
318+
319+ return $ this ;
324320 }
325321
326322 public function getLinkNoScheme (): bool |string
327323 {
328- return $ this ->getOption ( ' link_no_scheme ' ) ;
324+ return $ this ->options -> linkNoScheme ;
329325 }
330326
331327 /**
@@ -335,22 +331,34 @@ public function getLinkNoScheme(): bool|string
335331 * @param mixed $value
336332 *
337333 * @return static
334+ *
335+ * @deprecated Use {@see AutolinkOptions} instead.
338336 */
339337 protected function setOption (string $ name , mixed $ value = null ): static
340338 {
341- $ this ->options [$ name ] = $ value ;
339+ $ name = AutolinkOptions::mapOptionKey ($ name );
340+
341+ $ this ->options ->$ name = $ value ;
342342
343343 return $ this ;
344344 }
345345
346+ /**
347+ * @param string $name
348+ * @param mixed|null $default
349+ *
350+ * @return mixed
351+ *
352+ * @deprecated Use {@see AutolinkOptions} instead.
353+ */
346354 protected function getOption (string $ name , mixed $ default = null ): mixed
347355 {
348- return $ this ->options [$ name ] ?? $ default ;
356+ $ name = AutolinkOptions::mapOptionKey ($ name );
357+
358+ return $ this ->options ->$ name ?? $ default ;
349359 }
350360
351361 /**
352- * addScheme
353- *
354362 * @param string ...$schemes
355363 *
356364 * @return static
@@ -368,8 +376,6 @@ public function addScheme(string ...$schemes): static
368376 }
369377
370378 /**
371- * removeScheme
372- *
373379 * @param string $scheme
374380 *
375381 * @return static
@@ -385,26 +391,21 @@ public function removeScheme(string $scheme): static
385391 return $ this ;
386392 }
387393
388- /**
389- * Method to get property Options
390- *
391- * @return array
392- */
393- public function getOptions (): array
394+ public function getOptions (): AutolinkOptions
394395 {
395396 return $ this ->options ;
396397 }
397398
398399 /**
399400 * Method to set property options
400401 *
401- * @param array $options
402+ * @param AutolinkOptions| array $options
402403 *
403404 * @return static Return self to support chaining.
404405 */
405- public function setOptions (array $ options ): static
406+ public function setOptions (AutolinkOptions | array $ options ): static
406407 {
407- $ this ->options = $ options ;
408+ $ this ->options = AutolinkOptions:: wrap ( $ options) ;
408409
409410 return $ this ;
410411 }
0 commit comments