|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GinoPane\BlogTimeToRead\Components; |
|
4
|
|
|
|
|
5
|
|
|
use RainLab\Blog\Models\Post; |
|
6
|
|
|
use Cms\Classes\ComponentBase; |
|
7
|
|
|
use GinoPane\BlogTimeToRead\Plugin; |
|
8
|
|
|
use GinoPane\BlogTimeToRead\Models\Settings; |
|
9
|
|
|
use GinoPane\BlogTimeToRead\Helpers\TimeToRead as TimeToReadHelper; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class TimeToRead |
|
14
|
|
|
* |
|
15
|
|
|
* @package GinoPane\BlogTaxonomy\Components |
|
16
|
|
|
*/ |
|
17
|
|
|
class TimeToRead extends ComponentBase |
|
18
|
|
|
{ |
|
19
|
|
|
const NAME = 'timeToRead'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
private $readingSpeed; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
private $isRoundingUpEnabled; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $postSlug; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var int |
|
38
|
|
|
*/ |
|
39
|
|
|
public $minutes; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns information about this component, including name and description. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function componentDetails() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
'name' => Plugin::LOCALIZATION_KEY . 'components.timetoread.name', |
|
48
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.timetoread.description' |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Component Properties |
|
54
|
|
|
* @return array |
|
|
|
|
|
|
55
|
|
|
*/ |
|
56
|
|
|
public function defineProperties() |
|
57
|
|
|
{ |
|
58
|
|
|
return [ |
|
59
|
|
|
'postSlug' => [ |
|
60
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'components.timetoread.post_slug_title', |
|
61
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'components.timetoread.post_slug_description', |
|
62
|
|
|
'default' => '{{ :slug }}', |
|
63
|
|
|
'type' => 'string' |
|
64
|
|
|
], |
|
65
|
|
|
|
|
66
|
|
|
'readingSpeed' => [ |
|
67
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'settings.default_reading_speed', |
|
68
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'settings.default_reading_speed_comment', |
|
69
|
|
|
'default' => Settings::DEFAULT_READING_SPEED, |
|
70
|
|
|
'type' => 'string', |
|
71
|
|
|
'validationPattern' => '^(0+)?[1-9]\d*$', |
|
72
|
|
|
'showExternalParam' => false |
|
73
|
|
|
], |
|
74
|
|
|
|
|
75
|
|
|
'isRoundingUpEnabled' => [ |
|
76
|
|
|
'title' => Plugin::LOCALIZATION_KEY . 'settings.rounding_up_enabled', |
|
77
|
|
|
'description' => Plugin::LOCALIZATION_KEY . 'settings.rounding_up_enabled_comment', |
|
78
|
|
|
'type' => 'checkbox', |
|
79
|
|
|
'default' => Settings::DEFAULT_ROUNDING_UP_ENABLED, |
|
80
|
|
|
'showExternalParam' => false |
|
81
|
|
|
], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Query the tag and posts belonging to it |
|
87
|
|
|
*/ |
|
88
|
|
|
public function onRun() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->prepareVars(); |
|
91
|
|
|
|
|
92
|
|
|
$this->calculateReadingTime(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Prepare variables |
|
97
|
|
|
* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
private function prepareVars() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->postSlug = (string)$this->property('postSlug'); |
|
103
|
|
|
$this->readingSpeed = (int)$this->property('readingSpeed'); |
|
104
|
|
|
$this->isRoundingUpEnabled = (bool)$this->property('isRoundingUpEnabled'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
private function calculateReadingTime() |
|
111
|
|
|
{ |
|
112
|
|
|
$post = Post::whereSlug($this->postSlug)->first(); |
|
113
|
|
|
|
|
114
|
|
|
if (!$post) { |
|
115
|
|
|
$this->minutes = 0; |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->minutes = TimeToReadHelper::get()->calculate( |
|
120
|
|
|
$post->content, |
|
121
|
|
|
array_filter([ |
|
122
|
|
|
Settings::READING_SPEED_KEY => $this->readingSpeed, |
|
123
|
|
|
Settings::ROUNDING_UP_ENABLED_KEY => $this->isRoundingUpEnabled |
|
124
|
|
|
]) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.