Skip to content

Commit

Permalink
Merge pull request #8 from lukeyouell/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
lukeyouell authored Jun 22, 2018
2 parents 893ce6c + 638bd3e commit 92a0626
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 5 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file.

## 1.3.0 - 2018-06-22

### Added
- Format the time as a `DateInterval`
- Output the total seconds, minutes and hours

### Changed
- Both the filter and function now return a `TimeModel`

## 1.2.1 - 2018-06-19

### Fixed
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,48 @@ Returns: 9 minutes, 40 seconds
Returns: 10 minutes
```

## TimeModel

Whenever you're dealing with the read time in your template, you're actually working with a TimeModel object.

### Simple Output

Outputting a TimeModel object without attaching a property or method will return the time in the form of a human time duration.

```
{{ string|readTime }}
{{ readTime(entry) }}
```

### Properties

#### `human`

The human time duration.

#### `interval(format)`

A `DateInterval` object. You'll need to set the [format](http://php.net/manual/en/dateinterval.format.php) as a parameter:

```twig
{% set time = readTime(entry) %}
{{ time.interval('%h hours, %i minutes, $s seconds') }}
```

#### `seconds`

The total number of seconds.

#### `minutes`

The total number of minutes.

#### `hours`

The total number of hours.

## Overriding Plugin Settings

If you create a [config file](https://docs.craftcms.com/v3/configuration.html) in your `config` folder called `read-time.php`, you can override the plugin’s settings in the Control Panel. Since that config file is fully [multi-environment](https://docs.craftcms.com/v3/configuration.html) aware, this is a handy way to have different settings across multiple environments.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lukeyouell/craft-readtime",
"description": "Calculate the estimated read time for content.",
"type": "craft-plugin",
"version": "1.2.1",
"version": "1.3.0",
"keywords": [
"craft",
"cms",
Expand Down
66 changes: 66 additions & 0 deletions src/models/TimeModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Read Time plugin for Craft CMS 3.x
*
* Calculate the estimated read time for content.
*
* @link https://github.com/lukeyouell
* @copyright Copyright (c) 2018 Luke Youell
*/

namespace lukeyouell\readtime\models;

use lukeyouell\readtime\ReadTime;

use Craft;
use craft\base\Model;
use craft\helpers\DateTimeHelper;

class TimeModel extends Model
{
// Public Properties
// =========================================================================

public $seconds = 0;

public $showSeconds = true;

// Public Methods
// =========================================================================

public function __toString()
{
return (string) $this->human();
}

public function human()
{
return DateTimeHelper::secondsToHumanTimeDuration($this->seconds, $this->showSeconds);
}

public function interval($format = '%h hours, %i minutes, %s seconds')
{
$currentTimeStamp = DateTimeHelper::currentTimeStamp();
$datetimeStart = DateTimeHelper::toDateTime($currentTimeStamp);
$datetimeEnd = DateTimeHelper::toDateTime(DateTimeHelper::currentTimeStamp() + $this->seconds);

$interval = $datetimeStart->diff($datetimeEnd);

return $interval->format($format);
}

public function seconds()
{
return $this->seconds;
}

public function minutes()
{
return floor($this->seconds / 60);
}

public function hours()
{
return floor(($this->seconds / 60) / 60);
}
}
16 changes: 12 additions & 4 deletions src/twigextensions/ReadTimeTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace lukeyouell\readtime\twigextensions;

use lukeyouell\readtime\ReadTime;
use lukeyouell\readtime\models\TimeModel;

use Craft;
use craft\helpers\DateTimeHelper;
Expand Down Expand Up @@ -70,17 +71,24 @@ public function readTimeFunction($element, $showSeconds = true)
}
}

$duration = DateTimeHelper::secondsToHumanTimeDuration($totalSeconds, $showSeconds);
$data = [
'seconds' => $totalSeconds,
'showSeconds' => $showSeconds,
];

return $duration;
return new TimeModel($data);
}

public function readTimeFilter($value = null, $showSeconds = true)
{
$seconds = $this->valToSeconds($value);
$duration = DateTimeHelper::secondsToHumanTimeDuration($seconds, $showSeconds);

return $duration;
$data = [
'seconds' => $seconds,
'showSeconds' => $showSeconds,
];

return new TimeModel($data);
}

// Private Methods
Expand Down

0 comments on commit 92a0626

Please sign in to comment.