11
11
*/
12
12
class Autolink
13
13
{
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 ;
26
15
27
16
/**
28
17
* Property schemes.
@@ -64,12 +53,12 @@ class Autolink
64
53
/**
65
54
* Class init.
66
55
*
67
- * @param array $options Basic options.
68
- * @param array $schemes
56
+ * @param AutolinkOptions| array $options Basic options.
57
+ * @param array $schemes
69
58
*/
70
- public function __construct (array $ options = [], array $ schemes = [])
59
+ public function __construct (AutolinkOptions | array $ options = [], array $ schemes = [])
71
60
{
72
- $ this ->options = array_merge ( $ this -> options , ( array ) $ options );
61
+ $ this ->setOptions ( $ options );
73
62
74
63
$ this ->setSchemes (...array_merge ($ this ->schemes , $ schemes ));
75
64
}
@@ -254,22 +243,26 @@ public function shorten(string $text, int $limit): string
254
243
255
244
public function stripScheme (bool $ value = false ): static
256
245
{
257
- return $ this ->setOption ('strip_scheme ' , $ value );
246
+ $ this ->options ->stripScheme = $ value ;
247
+
248
+ return $ this ;
258
249
}
259
250
260
251
public function isStripScheme (): bool
261
252
{
262
- return ( bool ) $ this ->getOption ( ' strip_scheme ' ) ;
253
+ return $ this ->options -> stripScheme ;
263
254
}
264
255
265
256
public function autoEscape (bool $ value = true ): static
266
257
{
267
- return $ this ->setOption ('escape ' , $ value );
258
+ $ this ->options ->escape = $ value ;
259
+
260
+ return $ this ;
268
261
}
269
262
270
263
public function isAutoEscape (): bool
271
264
{
272
- return (bool ) $ this ->getOption ( ' escape ' ) ;
265
+ return (bool ) $ this ->options -> escape ;
273
266
}
274
267
275
268
/**
@@ -279,12 +272,18 @@ public function isAutoEscape(): bool
279
272
*/
280
273
public function textLimit (int |callable |null $ value = null ): static
281
274
{
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 ;
283
282
}
284
283
285
284
public function getTextLimit (): int |callable |null
286
285
{
287
- $ value = $ this ->getOption ( ' text_limit ' ) ;
286
+ $ value = $ this ->options -> textLimit ;
288
287
289
288
// Fix for B/C
290
289
if ($ value === false ) {
@@ -294,21 +293,16 @@ public function getTextLimit(): int|callable|null
294
293
return $ value ;
295
294
}
296
295
297
- /**
298
- * autoTitle
299
- *
300
- * @param mixed $value
301
- *
302
- * @return static
303
- */
304
296
public function autoTitle (bool $ value = false ): static
305
297
{
306
- return $ this ->setOption ('auto_title ' , $ value );
298
+ $ this ->options ->autoTitle = $ value ;
299
+
300
+ return $ this ;
307
301
}
308
302
309
303
public function isAutoTitle (): bool
310
304
{
311
- return ( bool ) $ this ->getOption ( ' auto_title ' ) ;
305
+ return $ this ->options -> autoTitle ;
312
306
}
313
307
314
308
/**
@@ -320,12 +314,14 @@ public function isAutoTitle(): bool
320
314
*/
321
315
public function linkNoScheme (bool |string $ value = false ): static
322
316
{
323
- return $ this ->setOption ('link_no_scheme ' , $ value );
317
+ $ this ->options ->linkNoScheme = $ value ;
318
+
319
+ return $ this ;
324
320
}
325
321
326
322
public function getLinkNoScheme (): bool |string
327
323
{
328
- return $ this ->getOption ( ' link_no_scheme ' ) ;
324
+ return $ this ->options -> linkNoScheme ;
329
325
}
330
326
331
327
/**
@@ -335,22 +331,34 @@ public function getLinkNoScheme(): bool|string
335
331
* @param mixed $value
336
332
*
337
333
* @return static
334
+ *
335
+ * @deprecated Use {@see AutolinkOptions} instead.
338
336
*/
339
337
protected function setOption (string $ name , mixed $ value = null ): static
340
338
{
341
- $ this ->options [$ name ] = $ value ;
339
+ $ name = AutolinkOptions::mapOptionKey ($ name );
340
+
341
+ $ this ->options ->$ name = $ value ;
342
342
343
343
return $ this ;
344
344
}
345
345
346
+ /**
347
+ * @param string $name
348
+ * @param mixed|null $default
349
+ *
350
+ * @return mixed
351
+ *
352
+ * @deprecated Use {@see AutolinkOptions} instead.
353
+ */
346
354
protected function getOption (string $ name , mixed $ default = null ): mixed
347
355
{
348
- return $ this ->options [$ name ] ?? $ default ;
356
+ $ name = AutolinkOptions::mapOptionKey ($ name );
357
+
358
+ return $ this ->options ->$ name ?? $ default ;
349
359
}
350
360
351
361
/**
352
- * addScheme
353
- *
354
362
* @param string ...$schemes
355
363
*
356
364
* @return static
@@ -368,8 +376,6 @@ public function addScheme(string ...$schemes): static
368
376
}
369
377
370
378
/**
371
- * removeScheme
372
- *
373
379
* @param string $scheme
374
380
*
375
381
* @return static
@@ -385,26 +391,21 @@ public function removeScheme(string $scheme): static
385
391
return $ this ;
386
392
}
387
393
388
- /**
389
- * Method to get property Options
390
- *
391
- * @return array
392
- */
393
- public function getOptions (): array
394
+ public function getOptions (): AutolinkOptions
394
395
{
395
396
return $ this ->options ;
396
397
}
397
398
398
399
/**
399
400
* Method to set property options
400
401
*
401
- * @param array $options
402
+ * @param AutolinkOptions| array $options
402
403
*
403
404
* @return static Return self to support chaining.
404
405
*/
405
- public function setOptions (array $ options ): static
406
+ public function setOptions (AutolinkOptions | array $ options ): static
406
407
{
407
- $ this ->options = $ options ;
408
+ $ this ->options = AutolinkOptions:: wrap ( $ options) ;
408
409
409
410
return $ this ;
410
411
}
0 commit comments