Skip to content

Commit 460c935

Browse files
committed
Use object options
1 parent 9bab175 commit 460c935

File tree

3 files changed

+111
-65
lines changed

3 files changed

+111
-65
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
](https://packagist.org/packages/asika/autolink)
66
[![Packagist Downloads](https://img.shields.io/packagist/dt/asika/autolink?style=for-the-badge)](https://packagist.org/packages/asika/autolink)
77

8-
9-
108
A library to auto convert URLs to links.
119

1210
## Table of Content
@@ -23,11 +21,11 @@ A library to auto convert URLs to links.
2321
* [Convert Email](#convert-email)
2422
* [Attributes Escaping](#attributes-escaping)
2523
* [Options](#options)
26-
* [`text_limit`](#text_limit)
27-
* [`auto_title`](#auto_title)
28-
* [`strip_scheme`](#strip_scheme)
24+
* [`textLimit`](#textlimit)
25+
* [`autoTitle`](#autotitle)
26+
* [`stripScheme`](#stripscheme)
2927
* [`escape`](#escape)
30-
* [`link_no_scheme`](#link_no_scheme)
28+
* [`linkNoScheme`](#linknoscheme)
3129
* [Scheme](#scheme)
3230
* [Link Builder](#link-builder)
3331
<!-- TOC -->
@@ -74,13 +72,15 @@ $autolink = new Autolink();
7472
Create with options.
7573

7674
```php
77-
$options = [
78-
'strip_scheme' => false,
79-
'text_limit' => null,
80-
'auto_title' => false,
81-
'escape' => true,
82-
'link_no_scheme' => false
83-
];
75+
use Asika\Autolink\AutolinkOptions;
76+
77+
$options = new AutolinkOptions(
78+
stripScheme: false,
79+
textLimit: null,
80+
autoTitle: false,
81+
escape: true,
82+
linkNoScheme: false
83+
);
8484

8585
$schemes = ['http', 'https', 'skype', 'itunes'];
8686

@@ -170,7 +170,7 @@ $autolink->setEscapeHandler(fn => ...);
170170

171171
## Options
172172

173-
### `text_limit`
173+
### `textLimit`
174174

175175
We can set this option by constructor or setter:
176176

@@ -208,7 +208,7 @@ Output:
208208
http://campus.asukademy.com/....../84-find-interns......
209209
```
210210

211-
### `auto_title`
211+
### `autoTitle`
212212

213213
Use AutoTitle to force add title on anchor element.
214214

@@ -224,7 +224,7 @@ Output:
224224
<a href="http://www.google.com.tw" title="http://www.google.com.tw">http://www.google.com.tw</a>
225225
```
226226

227-
### `strip_scheme`
227+
### `stripScheme`
228228

229229
Strip Scheme on link text:
230230

@@ -261,7 +261,7 @@ Output
261261
<a href="http://www.google.com.tw?foo=bar&amp;yoo=baz" >http://www.google.com.tw?foo=bar&amp;yoo=baz</a>
262262
```
263263

264-
### `link_no_scheme`
264+
### `linkNoScheme`
265265

266266
Convert URL which no scheme. If you pass `TRUE` to this option, Autolink will use
267267
`http` as default scheme, you can also provide your own default scheme.

src/Autolink.php

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,7 @@
1111
*/
1212
class 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
}

src/AutolinkOptions.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Asika\Autolink;
6+
7+
class AutolinkOptions
8+
{
9+
public function __construct(
10+
public bool $stripScheme = false,
11+
public int|\Closure|null $textLimit = null,
12+
public bool $autoTitle = false,
13+
public bool $escape = true,
14+
public bool|string $linkNoScheme = false,
15+
)
16+
{
17+
}
18+
19+
public static function wrap(AutolinkOptions|array $options): AutolinkOptions
20+
{
21+
if ($options instanceof static) {
22+
return $options;
23+
}
24+
25+
return new static(
26+
stripScheme: $options['strip_scheme'] ?? false,
27+
textLimit: $options['text_limit'] ?? null,
28+
autoTitle: $options['auto_title'] ?? false,
29+
escape: $options['escape'] ?? true,
30+
linkNoScheme: $options['link_no_scheme'] ?? false,
31+
);
32+
}
33+
34+
public static function mapOptionKey(string $key): string
35+
{
36+
return match ($key) {
37+
'strip_scheme' => 'stripScheme',
38+
'text_limit' => 'textLimit',
39+
'auto_title' => 'autoTitle',
40+
'escape' => 'escape',
41+
'link_no_scheme' => 'linkNoScheme',
42+
default => $key,
43+
};
44+
}
45+
}

0 commit comments

Comments
 (0)