Passed
Push — develop ( 319bd8...330c7a )
by Neill
16:41 queued 15s
created

PostStats::getProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
ccs 0
cts 4
cp 0
crap 2
1
<?php
2
/**
3
 * @link http://www.newicon.net/neon
4
 * @copyright Copyright (c) 2017 Newicon Ltd
5
 * @license http://www.newicon.net/neon/license/
6
 * @author Steve O'Brien <[email protected]> 17/04/2020
7
 * @package neon
8
 */
9
10
namespace neon\cms\form\fields;
11
12
use neon\cms\assets\CmsEditorAsset;
13
use \neon\core\form\fields\Field;
14
use neon\core\helpers\Str;
15
16
class PostStats extends Field
17
{
18
	public $ddsDataType = null;
19
20
	public $linkedField = '';
21
	/**
22
	 * @var int The average number of words an adult reads per minute
23
	 * used to calculate the time to read of the text value of the linkedField
24
	 */
25
	public $averageWordsPerMinute = 265;
26
27
	public function getProperties()
28
	{
29
		return [
30
			'name', 'linkedField'
31
		];
32
	}
33
34
	/**
35
	 * @inheritdoc
36
	 */
37
	public function registerScripts($view)
38
	{
39
		$view->registerAssetBundle(\neon\cms\assets\CmsFormAsset::class);
40
	}
41
42
	/**
43
	 * @inheritdoc
44
	 */
45
	public function getValue()
46
	{
47
		$form = $this->getParentForm();
48
		if ($form===null) return;
49
		$field = $form->getField($this->linkedField);
50
		$value = $field->getValue();
51
		if (!is_string($value))
52
			return ['word_count'=>0, 'time_to_read'=>0];
53
		// calculate post reading time
54
		$wordCount = Str::countWords(strip_tags($value));
55
		$averageWordsPerMinute = 265;
56
		$minutesToRead = round($wordCount / $averageWordsPerMinute);
57
		return [
58
			'minutes_to_read' => $minutesToRead,
59
			'word_count' => $wordCount
60
		];
61
	}
62
63
	public function getComponentDetails()
64
	{
65
		return [
66
			'label' => 'Post Stats', 'icon' => 'fa fa-flag-checkered', 'group' => 'Data', 'order'=>820
67
		];
68
	}
69
}