leshkens / laravel-read-time
这是一个laravel框架的扩展包,用于显示用户阅读内容的预估时间。
Requires
- php: ^7.2.5|^8.0
- ext-json: *
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
README
这是一个laravel扩展包,用于显示用户阅读内容的预估时间。
要求
- laravel版本6或更高
安装
您可以通过composer安装此包
composer require leshkens/laravel-read-time
发布配置文件
php artisan vendor:publish --provider="Leshkens\LaravelReadTime\Providers\ReadTimeServiceProvider"
配置文件 config/read-time.php
全局选项
'options' => [ // The number of words per minute, based on which the approximate // time for reading will be calculated. Default is 230 'words_per_minute' => 230, // Clear result string of html tags 'strip_tags' => false, // How many seconds does a new unit start with 'units' => [ 'second' => 0, 'minute' => 60, //'hour' => 3600 ] ],
单词计数器类
'counter' => Leshkens\LaravelReadTime\Counter::class,
您可以在单词计数中使用您自己的类和逻辑。您的类必须实现Leshkens\LaravelReadTime\Contracts\CounterInterface
接口。逻辑应在count()
方法中。
例如,这是一个标准单词计数器逻辑的示例
public function count(string $content): int { return count(preg_split('/\s+/', $content, -1, PREG_SPLIT_NO_EMPTY)); }
区域列表
用于形成字符串“阅读时间”的区域列表
'locales' => [ 'en' => Leshkens\LaravelReadTime\Locales\En::class ]
区域类必须实现Leshkens\LaravelReadTime\Contracts\LocaleInterface
接口。逻辑应在result()
方法中。
例如,让我们添加Ru区域类
namespace App\Support\ReadTimeLocales; use Leshkens\LaravelReadTime\Contracts\LocaleInterface; use function morphos\Russian\pluralize; class Ru implements LocaleInterface { protected $unitMap = [ 'second' => 'секунда', 'minute' => 'минута', 'hour' => 'час' ]; public function result(int $number, string $unit): string { return pluralize($number, $this->unitMap[$unit]); } }
在配置中
'locales' => [ 'en' => Leshkens\LaravelReadTime\Locales\En::class, 'ru' => App\Support\ReadTimeLocales\Ru::class ]
用法
对象
use Illuminate\Support\Str; use Leshkens\LaravelReadTime\Facades\ReadTime; $readTime = ReadTime::parse('Lorem ipsum dolor sit amet, consectetur adipiscing elit...'); // Or array $readTime = ReadTime::parse(['Lorem ipsum dolor sit amet', 'consectetur adipiscing elit']); $number = $readTime->number; // 3 $unit = $readTime->unit; // second $result = Str::plural($unit, $number); return "{$number} {$result} on read"; // 3 seconds on read
您可以将与全局设置相同的设置数组作为ReadTime
对象的第二个参数传递。
示例
$options = [ 'words_per_minute' => 1, 'units' => [ 'second' => 1 // leave only a seconds ] ]; $readTime = ReadTime::parse('Lorem ipsum dolor sit amet, consectetur adipiscing elit...', $options); $number = $readTime->number; // 3 $unit = $readTime->unit; // second $result = Str::plural($unit, $number); return "{$number} {$result} on read"; // 480 seconds on read
字符串
只需添加get()
方法。
该方法可以将区域(来自包配置区域列表)的字符串值作为第一个参数。如果未传递给该方法,或值为null,则采用当前应用程序的区域。
use Leshkens\LaravelReadTime\Facades\ReadTime; ReadTime::parse('Lorem ipsum dolor sit amet, consectetur adipiscing elit...') ->get('ru');
将返回3 секунды
注意:如果所需区域的对象不在包的配置文件中,则默认输出英语字符串
您还可以使用readtime()
辅助函数来渲染字符串
readtime($content, $locale, $options);
在模型中
将HasReadTime
特性和readTime()
方法与设置添加到您的模型中
use Illuminate\Database\Eloquent\Model; use Leshkens\LaravelReadTime\Traits\HasReadTime; class Article extends Model { use HasReadTime; protected function readTime(): array { return [ // Attribute for parse. You can split it with // a dot (e.g 'content.text') if the desired // attribute is inside a array or json 'source' => 'content', // No required. If this key is not present, then the current application locale is taken. 'locale' => 'en', // No required. Options array. 'options' => [ 'strip_tags' => true ] ]; } }
$article->read_time
返回阅读时间的字符串值。
如果您的属性包含包含区域数组的数组或json
// array [ 'ru' => 'Какой-то прекрасный текст', 'en' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' ] // or json { "ru": "Какой-то прекрасный текст", "en": "Lorem ipsum dolor sit amet, consectetur adipiscing elit" }
您可以将localable
设置为true
protected function readTime(): array { return [ 'source' => 'content', 'localable' => true ]; }
$article->read_time
返回数组值
[ 'ru' => '1 секунда' 'en' => '3 seconds on read' ]
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
贡献
有关详细信息,请参阅CONTRIBUTING。
安全
如果您发现任何安全相关的问题,请通过电子邮件Leshkens@gmail.com联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。