承接 mtownsend/read-time 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mtownsend/read-time

Composer 安装命令:

composer require mtownsend/read-time

包简介

A PHP package to show users how long it takes to read content.

README 文档

README

A PHP package to show users how long it takes to read content.

Installation

Install via composer:

composer require mtownsend/read-time

This package is designed to work with any PHP 7.0+ application but has special support for Laravel.

Registering the service provider (Laravel)

For Laravel 5.4 and lower, add the following line to your config/app.php:

/*
 * Package Service Providers...
 */
Mtownsend\ReadTime\Providers\ReadTimeServiceProvider::class,

For Laravel 5.5 and greater, the package will auto register the provider for you.

Using Lumen

To register the service provider, add the following line to app/bootstrap/app.php:

$app->register(Mtownsend\ReadTime\Providers\ReadTimeServiceProvider::class);

Publishing the config file (Laravel)

ReadTime has special configuration support for Laravel applications. You can publish a single config file and customize how you want your read time text to be displayed.

php artisan vendor:publish --provider="Mtownsend\ReadTime\Providers\ReadTimeServiceProvider" --tag="read-time-config"

These are the contents of the read-time.php config file:

return [

    /*
     * Whether or not minute/second should be abbreviated as min/sec
     */
    'abbreviate_time_measurements' => false,

    /*
     * Omit seconds from being displayed in the read time estimate
     */
    'omit_seconds' => true,

    /*
     * Whether or not only the time should be displayed
     */
    'time_only' => false,

    /*
     * The average words per minute reading time
     */
    'words_per_minute' => 230,
];

Publishing the translation files (Laravel)

ReadTime supports localization with Laravel. If you are using Laravel you'll likely want to use the premade translations.

php artisan vendor:publish --provider="Mtownsend\ReadTime\Providers\ReadTimeServiceProvider" --tag="read-time-language-files"

Quick start

Using the class

Here is an example of the most basic usage:

use Mtownsend\ReadTime\ReadTime;

$readTime = (new ReadTime($content))->get();

You may also pass several arguments to the constructor if you wish to change settings on the fly:

use Mtownsend\ReadTime\ReadTime;

$readTime = (new ReadTime($content, $omitSeconds = true, $abbreviated = false, $wordsPerMinute = 230))->get();
// or
$readTime = (new ReadTime($content))
				->omitSeconds()
				->abbreviated()
				->wpm($wordsPerMinute)
				->get();

The ReadTime class is able to accept a string of content or a flat array of multiple pieces of content. This may come in handy if you are attempting to display the total read time of body content along with sidebar content. For example:

use Mtownsend\ReadTime\ReadTime;

$readTime = (new ReadTime([$content, $moreContent, $evenMoreContent]))->get();

Methods, and arguments

Method

->abbreviated(bool $abbreviated = true)

Abbreviate the words 'minute' and 'second' to 'min' and 'sec'.

->get()

Retrieve the read time. Note: you may also invoke the class as a function or cast it as a string to retrieve the same result as get().

->getTranslation($key = null)

You may return the current translation array the class is using by omitting any argument from this method or get a specific translation key by passing it as an argument.

->ltr(bool $ltr = true)

Set the text direction of the read time result to left (default) with true, and right with false. Alternatively, you may simply call the ->rtl() method without any argument.

->omitSeconds(bool $omitSeconds = true)

Have the read time display omit seconds. Pass false to include seconds.

->rtl(bool $rtl = true)

Set the text direction of the read time result to right (default) with true, and left with false. Alternatively, you may simply call the ->ltr() method without any argument.

->setTranslation(array $translations)

Manually set the translation text for the class to use. If no key is passed it will default to the English counterpart. A complete translation array will contain the following:

[
    'reads_left_to_right' => true,
    'min' => 'min',
    'minute' => 'minute',
    'sec' => 'sec',
    'second' => 'second',
    'read' => 'read'
]

->timeOnly(bool $timeOnly = true)

Omit any words from the read time result. Pass false to include words.

->toArray()

Get the contents and settings of the class as an array.

->toJson()

Get the contents and settings of the class as a json string.

->wpm(int $wordsPerMinute)

Set the average pace of words read per minute.

Using the global helper (Laravel)

If you are using Laravel, this package provides a convenient helper function which is globally accessible.

read_time($content);

The global helper is exceptionally useful in your Laravel application. It can be used in views (remember, it outputs the read time if invoked or cast as a string, which Blade's double curly braces does):

<h1>Some blog title</h1>
<small>{{ read_time($content) }}</small>
<hr>

The global helper will also attempt to intelligently detect the information you are passing it. For example, if you pass it a non-associative array it will assume you are passing an array of content.

<h1>Some blog title</h1>
<small>{{ read_time([$content, $moreContent]) }}</small>
<hr>

But you are still free to customize the ReadTime class settings on the fly using the global helper. Simply pass an associative array of settings:

{{ read_time([
    'content' => $content,
    // or
    // 'content' => [$content, $moreContent],
    'omit_seconds' => true,
    'time_only' => false,
    'abbreviated' => true,
    'words_per_minute' => 230,
    'ltr' => true,
    'translation' => [
        'reads_left_to_right' => true,
        'min' => 'min',
        'minute' => 'minute',
        'sec' => 'sec',
        'second' => 'second',
        'read' => 'read'
    ]
]) }}

Purpose

Sites like Medium.com have popularized the concept of giving users an estimate for the amount of time it will take them to read the content. With this convenience in mind, ReadTime gives PHP developers the same tool for their readable content. It's a simple feature that will give a nice touch to your PHP application.

Contributing translations

Pull requests for translations are encouraged. Please be sure to follow the existing format.

Credits

Testing

You can run the tests with:

./vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

mtownsend/read-time 适用场景与选型建议

mtownsend/read-time 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 632.42k 次下载、GitHub Stars 达 282, 最近一次更新时间为 2019 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「blog」 「time」 「content」 「article」 「read」 「min」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mtownsend/read-time 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mtownsend/read-time 我们能提供哪些服务?
定制开发 / 二次开发

基于 mtownsend/read-time 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 632.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 283
  • 点击次数: 33
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 282
  • Watchers: 6
  • Forks: 27
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-08