leshkens/laravel-read-time
Composer 安装命令:
composer require leshkens/laravel-read-time
包简介
A package for laravel framework that shows users the approximate time to read content.
README 文档
README
A package for laravel that shows users the approximate time to read content.
Requirements
- Laravel version 6 or higher
Installation
You can install the package via composer:
composer require leshkens/laravel-read-time
Publish config file
php artisan vendor:publish --provider="Leshkens\LaravelReadTime\Providers\ReadTimeServiceProvider"
Config file config/read-time.php
Global options:
'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 ] ],
Word counter class
'counter' => Leshkens\LaravelReadTime\Counter::class,
You can use your class and logic in word counting.
Your class must implement the Leshkens\LaravelReadTime\Contracts\CounterInterface interface. The logic should be in the count() method.
For example, this is what a standard word counter logic looks like:
public function count(string $content): int { return count(preg_split('/\s+/', $content, -1, PREG_SPLIT_NO_EMPTY)); }
Locale list
List of locales for forming the string "time to read":
'locales' => [ 'en' => Leshkens\LaravelReadTime\Locales\En::class ]
Locale class must implement the Leshkens\LaravelReadTime\Contracts\LocaleInterface interface. The logic should be in the result() method.
For example, let's add the Ru locale class:
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]); } }
In config:
'locales' => [ 'en' => Leshkens\LaravelReadTime\Locales\En::class, 'ru' => App\Support\ReadTimeLocales\Ru::class ]
Usage
Object
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
You can pass your array of settings (the same settings as the global ones) as the second argument of the ReadTime object.
Example:
$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
String
Just add the get() method.
The method can take a locale (from package config locale list) string value as the first argument. If nothing is passed to the method, or the value is null, the current application locale is taken.
use Leshkens\LaravelReadTime\Facades\ReadTime; ReadTime::parse('Lorem ipsum dolor sit amet, consectetur adipiscing elit...') ->get('ru');
Will return 3 секунды
Note: If the object of the desired locale is not in the config file of the package, then by default the string for English will be output
You can also use the readtime() helper to render a string:
readtime($content, $locale, $options);
In model
Add the HasReadTime trait and readTime() method with settings to your model:
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 returns the string value of the time to read.
If your attribute contains an array or json with locales:
// array [ 'ru' => 'Какой-то прекрасный текст', 'en' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' ] // or json { "ru": "Какой-то прекрасный текст", "en": "Lorem ipsum dolor sit amet, consectetur adipiscing elit" }
you can set localable to true:
protected function readTime(): array { return [ 'source' => 'content', 'localable' => true ]; }
$article->read_time returns the array value:
[ 'ru' => '1 секунда' 'en' => '3 seconds on read' ]
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email Leshkens@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
leshkens/laravel-read-time 适用场景与选型建议
leshkens/laravel-read-time 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.75k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「blog」 「time」 「content」 「laravel」 「article」 「read」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 leshkens/laravel-read-time 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 leshkens/laravel-read-time 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 leshkens/laravel-read-time 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A block that displays featured content - large image, title, description and link.
Tools to convert between .NET and PHP date formats
Adds the EDTF data type to Wikibase
Laravel package for opinionated blog implementation
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
Date component is a set of methods to help with the manipulation of dates.
统计信息
- 总下载量: 4.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-30
