From 3bcd6eb4e5375b8fc028d410f83db0d3060aa56e Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Wed, 21 Jun 2017 15:54:26 +0200 Subject: [PATCH 1/9] Improving comments Improved the comments inside the following files: `PageVisitsCounterServiceProvider.php`, `HasPageVisitsCounter.php` and `SessionHistory.php`. --- CHANGELOG.md | 1 + src/Classes/SessionHistory.php | 2 +- src/PageVisitsCounterServiceProvider.php | 38 ++++++++++++++---------- src/Traits/HasPageVisitsCounter.php | 2 +- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78965f3f..3546fca4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to `laravel-page-visits-counter` will be documented in this ### Changed - Improved `README.md`. - Updated the `CHANGELOG.md` file. +- Improved the comments inside the following files: `PageVisitsCounterServiceProvider.php`, `HasPageVisitsCounter.php` and `SessionHistory.php`. ## [0.1.4] - 17-6-2017 ### Added diff --git a/src/Classes/SessionHistory.php b/src/Classes/SessionHistory.php index 26580293..efd84c89 100644 --- a/src/Classes/SessionHistory.php +++ b/src/Classes/SessionHistory.php @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Session; /** - * Laravel wrapper for SessionHistory. + * Class SessionHistory. * * @copyright Copyright (c) 2017 Cyril de Wit (http://www.cyrildewit.nl) * @author Cyril de Wit (info@cyrildewit.nl) diff --git a/src/PageVisitsCounterServiceProvider.php b/src/PageVisitsCounterServiceProvider.php index 7f42f538..404ee8e3 100644 --- a/src/PageVisitsCounterServiceProvider.php +++ b/src/PageVisitsCounterServiceProvider.php @@ -5,30 +5,22 @@ use Illuminate\Support\ServiceProvider; use Cyrildewit\PageVisitsCounter\Contracts\PageVisit as PageVisitContract; +/** + * Class PageVisitsCounterServiceProvider. + * + * @copyright Copyright (c) 2017 Cyril de Wit (http://www.cyrildewit.nl) + * @author Cyril de Wit (info@cyrildewit.nl) + * @license https://opensource.org/licenses/MIT MIT License + */ class PageVisitsCounterServiceProvider extends ServiceProvider { /** - * Register bindings in the container. - * - * @return void - */ - public function register() - { - // Merge the config file - $this->mergeConfigFrom( - __DIR__.'/../config/page-visits-counter.php', - 'page-visits-counter' - ); - } - - /** - * Bootstrap PageVisitsCounter application services. + * Bootstrap the application services. * * @return void */ public function boot() { - // Publish config file $this->publishes([ __DIR__.'/../config/page-visits-counter.php' => $this->app->configPath('page-visits-counter.php'), ], 'config'); @@ -46,6 +38,20 @@ public function boot() $this->registerModelBindings(); } + /** + * Regiser the application services. + * + * @return void + */ + public function register() + { + // Merge the config file + $this->mergeConfigFrom( + __DIR__.'/../config/page-visits-counter.php', + 'page-visits-counter' + ); + } + /** * Register Model Bindings. * diff --git a/src/Traits/HasPageVisitsCounter.php b/src/Traits/HasPageVisitsCounter.php index 92fc86fe..6b7675e5 100644 --- a/src/Traits/HasPageVisitsCounter.php +++ b/src/Traits/HasPageVisitsCounter.php @@ -6,7 +6,7 @@ use Cyrildewit\PageVisitsCounter\Classes\SessionHistory; /** - * Trait for Laravel Models. + * Trait HasPageVisitsCounter for Eloquent models. * * @copyright Copyright (c) 2017 Cyril de Wit (http://www.cyrildewit.nl) * @author Cyril de Wit (info@cyrildewit.nl) From 2587dcaee63439a9e425a0a3e04ccc68fce31d50 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Thu, 22 Jun 2017 14:44:40 +0200 Subject: [PATCH 2/9] Updating the LICENSE file --- LICENSE => LICENSE.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename LICENSE => LICENSE.md (92%) diff --git a/LICENSE b/LICENSE.md similarity index 92% rename from LICENSE rename to LICENSE.md index 23a061a2..9d916117 100644 --- a/LICENSE +++ b/LICENSE.md @@ -1,6 +1,6 @@ -The MIT License (MIT) +# The MIT License (MIT) -Copyright (c) 2016 Nicolas Brosy +Copyright (c) 2017 Cyril de Wit (http://www.cyrildewit.nl) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 20446b4ca0976eabb0aa3375842e4b73724e501f Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Thu, 22 Jun 2017 14:53:10 +0200 Subject: [PATCH 3/9] Changing the badges --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 62118241..27544445 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # Laravel Page Visit Counter +[![Packagist](https://img.shields.io/packagist/v/cyrildewit/laravel-page-visits-counter.svg?style=flat-square)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter) +[![Travis branch](https://img.shields.io/travis/cyrildewit/laravel-page-visits-counter/master.svg?style=flat-square)](https://travis-ci.org/cyrildewit/laravel-page-visits-counter) [![StyleCI](https://styleci.io/repos/94131608/shield?style=flat-square)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter) -[![Build Status](https://travis-ci.org/cyrildewit/laravel-page-visits-counter.svg?branch=master)](https://travis-ci.org/cyrildewit/laravel-page-visits-counter) -[![Packagist](https://img.shields.io/packagist/v/cyrildewit/laravel-page-visits-counter.svg)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter) [![Total Downloads](https://img.shields.io/packagist/dt/cyrildewit/laravel-page-visits-counter.svg?style=flat-square)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter) +[![license](https://img.shields.io/github/license/cyrildewit/laravel-page-visits-counter.svg?style=flat-square)](https://github.com/cyrildewit/laravel-page-visits-counter/blob/master/LICENSE.md) This package allows you to store page visits of different models into the database. From 07d5f692f256b1a8b90c45dd1a61b535b37d6936 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Thu, 22 Jun 2017 16:54:11 +0200 Subject: [PATCH 4/9] Adding package auto discovery --- composer.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/composer.json b/composer.json index 2d53e012..195f0172 100644 --- a/composer.json +++ b/composer.json @@ -42,5 +42,12 @@ "config": { "sort-packages": true, "optimize-autoloader": true + }, + "extra": { + "laravel": { + "providers": [ + "Cyrildewit\\PageVisitsCounter\\PageVisitsCounterServiceProvider" + ] + } } } From 63f1961e57552f19126d431f7fd5a3137743feec Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 14 Jul 2017 14:21:43 +0200 Subject: [PATCH 5/9] Improving the config file --- config/page-visits-counter.php | 51 +++++++++---------- .../create_page_visits_table.php.stub | 8 +-- src/Models/PageVisit.php | 2 +- src/PageVisitsCounterServiceProvider.php | 7 ++- src/Traits/HasPageVisitsCounter.php | 4 +- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/config/page-visits-counter.php b/config/page-visits-counter.php index 13543fb6..e55712d9 100644 --- a/config/page-visits-counter.php +++ b/config/page-visits-counter.php @@ -2,32 +2,26 @@ return [ - 'models' => [ - - /* - * This model is used by default to store - * the page visits into the database. - */ - 'page-visit' => Cyrildewit\PageVisitsCounter\Models\PageVisit::class, - - ], - - 'table_names' => [ - - /* - * This table is used by creating the migrations - * files and default model. - */ - 'page-visits' => 'page-visits', - - ], - - 'sessions' => [ - - 'primary-session-key' => 'page-visits-counter.history', - - ], - + /* + * The class name of the page visit Eloquent model to be used. + */ + 'page_visit_model' => Cyrildewit\PageVisitsCounter\Models\PageVisit::class, + + /* + * The table name of the page visits database table. + * It is used by creating the migrations files and default Eloquent model. + */ + 'page_visits_table_name' => 'page-visits', + + /* + * The key thas used to store page visits into the session. This is used by + * the SessionHistory class that handles the visits with expiry dates. + */ + 'page_visits_history_session_key' => 'page-visits-counter.history', + + /* + * Number format output settings. + */ 'output-settings' => [ /* @@ -36,8 +30,9 @@ 'formatted-output-enabled' => true, /* - * The following optiosn will be used inside the - * `number_format`function. + * The configured option values will be used + * inside the official php `number_format()` function. + * * Example: 120000 ==> 120.000 * Example: 500206000 ==> 502.006.000 */ diff --git a/database/migrations/create_page_visits_table.php.stub b/database/migrations/create_page_visits_table.php.stub index 493759bc..e1039a88 100644 --- a/database/migrations/create_page_visits_table.php.stub +++ b/database/migrations/create_page_visits_table.php.stub @@ -13,9 +13,9 @@ class CreatePageVisitsTable extends Migration */ public function up() { - $tableNames = config('page-visits-counter.table_names'); + $pageVisitsTableName = config('page-visits-counter.page_visits_table_name', 'page-visits'); - Schema::create($tableNames['page-visits'], function (Blueprint $table) { + Schema::create($pageVisitsTableName, function (Blueprint $table) { $table->increments('id')->unsigned(); $table->bigInteger('visitable_id')->unsigned(); @@ -32,8 +32,8 @@ class CreatePageVisitsTable extends Migration */ public function down() { - $tableNames = config('page-visits-counter.table_names'); + $pageVisitsTableName = config('page-visits-counter.page_visits_table_name', 'page-visits'); - Schema::dropIfExists($tableNames['page-visits']); + Schema::dropIfExists(pageVisitsTableName); } } diff --git a/src/Models/PageVisit.php b/src/Models/PageVisit.php index 0a7fc771..371c94c7 100644 --- a/src/Models/PageVisit.php +++ b/src/Models/PageVisit.php @@ -24,6 +24,6 @@ public function __construct(array $attributes = []) { parent::__construct($attributes); - $this->setTable(config('page-visits-counter.table_names.page-visits', 'page-visits')); + $this->setTable(config('page-visits-counter.page_visits_table_name', 'page-visits')); } } diff --git a/src/PageVisitsCounterServiceProvider.php b/src/PageVisitsCounterServiceProvider.php index 404ee8e3..ef5d9886 100644 --- a/src/PageVisitsCounterServiceProvider.php +++ b/src/PageVisitsCounterServiceProvider.php @@ -25,7 +25,7 @@ public function boot() __DIR__.'/../config/page-visits-counter.php' => $this->app->configPath('page-visits-counter.php'), ], 'config'); - // Publish migration file only if it doesn't exist + // Publish migration file only if it doesn't exists if (! class_exists('CreatePageVisitsTable')) { $timestamp = date('Y_m_d_His', time()); @@ -34,7 +34,6 @@ public function boot() ], 'migrations'); } - // Register Model Bindings $this->registerModelBindings(); } @@ -59,8 +58,8 @@ public function register() */ protected function registerModelBindings() { - $config = $this->app->config['page-visits-counter.models']; + $config = $this->app->config['page-visits-counter']; - $this->app->bind(PageVisitContract::class, $config['page-visit']); + $this->app->bind(PageVisitContract::class, $config['page_visit_model']); } } diff --git a/src/Traits/HasPageVisitsCounter.php b/src/Traits/HasPageVisitsCounter.php index 6b7675e5..270c1dd6 100644 --- a/src/Traits/HasPageVisitsCounter.php +++ b/src/Traits/HasPageVisitsCounter.php @@ -54,7 +54,7 @@ protected function getArrayableAppends() public function visits() { return $this->morphMany( - $this->configSettings['models']['page-visit'], + $this->configSettings['page_visit_model'], 'visitable' ); } @@ -142,7 +142,7 @@ public function retrievePageVisitsCountBetween(Carbon $from_date, Carbon $end_da */ public function addVisit() { - $visitClass = $this->configSettings['models']['page-visit']; + $visitClass = $this->configSettings['page_visit_model']; $visit = new $visitClass(); $visit->visitable_id = $this->id; From 51e366c3868ee37faeaa1e25665ccb31da3a82ea Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 14 Jul 2017 14:21:55 +0200 Subject: [PATCH 6/9] Improving and updating README.md --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 27544445..ec746ecd 100644 --- a/README.md +++ b/README.md @@ -117,10 +117,12 @@ class Article extends Model Our attributes always return an object. This object contains two properties: `number` & `formatted`. As the names maybe suggests `total_visits_count->number` will return the whole number and `total_visits_count->formatted` will return a formatted string (120000 -> 120.000). ```php -$article->total_visits_count // Retrieve all counted visits -$article->last_24h_visits_count // Retrieve all counted visits from the past 24 hours -$article->last_7d_visits_count // Retrieve all counted visits from the past 7 days -$article->last_14d_visits_count // Retrieve all counted visits from the past 14 days +$article->total_visits_count->formatted // Retrieve all counted visits formatted +$article->total_visits_count->number // Retrieve all counted visits as a normal integer + +$article->last_24h_visits_count->formatted // Retrieve all counted visits from the past 24 hours (number is also available) +$article->last_7d_visits_count->formatted // Retrieve all counted visits from the past 7 days (number is also available) +$article->last_14d_visits_count->formatted // Retrieve all counted visits from the past 14 days (number is also available) // Retrieve visits from date (past 2 weeks) $article->retrievePageVisitsFrom(Carbon::now()->subWeeks(2)); From 8994a802ee6379149b0f9e0e47ed2152794bb2e4 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 14 Jul 2017 14:22:42 +0200 Subject: [PATCH 7/9] Removing unnecessary comments --- src/Classes/SessionHistory.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Classes/SessionHistory.php b/src/Classes/SessionHistory.php index efd84c89..ab0a4846 100644 --- a/src/Classes/SessionHistory.php +++ b/src/Classes/SessionHistory.php @@ -27,7 +27,6 @@ class SessionHistory */ public function __construct() { - // Set primary Session Key $this->primarySessionKey = config('page-visits-counter.sessions.primary-session-key', 'page-visits-counter.history'); } @@ -43,12 +42,10 @@ public function addToSession(Model $model, Carbon $expires_at) // Make unique key from the inserted model $uniqueKey = $this->fromCamelCaseToDashes(class_basename($model)); - // Remove expired visits froms session $this->removeExpiredVisitsFromSession($uniqueKey); // Check if the item is visited, if not add to session if (! $this->isItemVisited($uniqueKey, $model->id)) { - // Push it to the session Session::push($this->primarySessionKey.'.'.$uniqueKey, [ 'visitable_id' => $model->id, 'expires_at' => $expires_at, From 2bd54e58cfdb9ff0576fdaecdef5f62b0f72871f Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Wed, 19 Jul 2017 14:58:25 +0200 Subject: [PATCH 8/9] Improving the package --- README.md | 45 +++++++---- config/page-visits-counter.php | 5 -- src/Classes/SessionHistory.php | 6 +- src/Traits/HasPageVisitsCounter.php | 100 +++++++++++++++---------- tests/TestCases/VisitVisitableTest.php | 17 ++++- 5 files changed, 107 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index ec746ecd..20b3048b 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,11 @@ Once installed you can do stuff like this: ```php // Return total visits of the article -$article->total_visits_count->formatted +$article->page_visits +$article->page_visits_formatted // Return total visits of last 24 hours -$article->last_24h_visits_count->formatted +$article->page_visits_24h // Store new visit in the databae $article->addVisit(); @@ -51,6 +52,7 @@ In this documention you will find some helpful information about the use of this * [Making a Elqouent model visitable](#making-a-eloquent-model-visitable) * [Retrieving page visits count](#retrieving-page-visits-count) * [Storing new visits](#storing-new-visits) + * [Sorting Model items by visit count](#sorting-model-items-by-visits-count) 3. [Configuration](#configuration) * [Configuring the formatted number format](#configuring-the-formatted-number-format) @@ -114,15 +116,18 @@ class Article extends Model ### Retrieving page visits count -Our attributes always return an object. This object contains two properties: `number` & `formatted`. As the names maybe suggests `total_visits_count->number` will return the whole number and `total_visits_count->formatted` will return a formatted string (120000 -> 120.000). - ```php -$article->total_visits_count->formatted // Retrieve all counted visits formatted -$article->total_visits_count->number // Retrieve all counted visits as a normal integer +$article->page_visits +$article->page_visits_formatte + +$article->page_visits_24h +$article->page_visits_24h_formatted + +$article->page_visits_7d +$article->page_visits_7d_formatted -$article->last_24h_visits_count->formatted // Retrieve all counted visits from the past 24 hours (number is also available) -$article->last_7d_visits_count->formatted // Retrieve all counted visits from the past 7 days (number is also available) -$article->last_14d_visits_count->formatted // Retrieve all counted visits from the past 14 days (number is also available) +$article->page_visits_14d +$article->page_visits_14d_formatted // Retrieve visits from date (past 2 weeks) $article->retrievePageVisitsFrom(Carbon::now()->subWeeks(2)); @@ -142,6 +147,15 @@ $article->addVisit() $article->addVisitThatExpiresAt(Carbon::now()->addHours(2)) ``` +### Sorting Model items by visit count + +```php +// Example 1 +$articles = Article::all()->sortBy('page_visits_14d'); +$articles = Article::with('relatedModel')->get()->sortBy('page_visits_7d'); +$articles = Article::where('status', 'published')->get()->sortBy('page_visits_24h'); +``` + ## Configuration ### Configuring the formatted number format @@ -155,16 +169,15 @@ return [ // ... + /* + * Number format output settings. + */ 'output-settings' => [ /* - * Set true for aut number output. - */ - 'formatted-output-enabled' => true, - - /* - * The following optiosn will be used inside the - * `number_format`function. + * The configured option values will be used + * inside the official php `number_format()` function. + * * Example: 120000 ==> 120.000 * Example: 500206000 ==> 502.006.000 */ diff --git a/config/page-visits-counter.php b/config/page-visits-counter.php index e55712d9..0b9c9cbd 100644 --- a/config/page-visits-counter.php +++ b/config/page-visits-counter.php @@ -24,11 +24,6 @@ */ 'output-settings' => [ - /* - * Set true for formatted number output. - */ - 'formatted-output-enabled' => true, - /* * The configured option values will be used * inside the official php `number_format()` function. diff --git a/src/Classes/SessionHistory.php b/src/Classes/SessionHistory.php index ab0a4846..c276c83e 100644 --- a/src/Classes/SessionHistory.php +++ b/src/Classes/SessionHistory.php @@ -64,7 +64,7 @@ public function addToSession(Model $model, Carbon $expires_at) * @param int $visitable_id * @return true|false */ - public function isItemVisited(string $uniqueKey, int $visitable_id) + public function isItemVisited($uniqueKey, $visitable_id) { $sessionItems = Session::get($this->primarySessionKey.'.'.$uniqueKey, []); @@ -83,7 +83,7 @@ public function isItemVisited(string $uniqueKey, int $visitable_id) * @param string $uniqueKey * @return void */ - public function removeExpiredVisitsFromSession(string $uniqueKey) + public function removeExpiredVisitsFromSession($uniqueKey) { $currentTime = Carbon::now(); $sessionItems = Session::get($this->primarySessionKey.'.'.$uniqueKey, []); @@ -103,7 +103,7 @@ public function removeExpiredVisitsFromSession(string $uniqueKey) * @param string $value * @return int The converted string. */ - public function fromCamelCaseToDashes(string $value) + public function fromCamelCaseToDashes($value) { preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $value, $matches); diff --git a/src/Traits/HasPageVisitsCounter.php b/src/Traits/HasPageVisitsCounter.php index 270c1dd6..c9b39bdd 100644 --- a/src/Traits/HasPageVisitsCounter.php +++ b/src/Traits/HasPageVisitsCounter.php @@ -32,15 +32,20 @@ public function __construct() /** * Adding attributes for retrieving the pagevies of the model. * - * @var array + * @var arrayls + */ protected function getArrayableAppends() { $this->appends = array_unique(array_merge($this->appends, [ - 'total_visits_count', - 'last_24h_visits_count', - 'last_7d_visits_count', - 'last_14d_visits_count', + 'page_visits', + 'page_visits_24h', + 'page_visits_7d', + 'page_visits_14d', + 'page_visits_formatted', + 'page_visits_24h_formatted', + 'page_visits_7d_formatted', + 'page_visits_14d_formatted', ])); return parent::getArrayableAppends(); @@ -64,43 +69,81 @@ public function visits() * * @return int */ - public function getTotalVisitsCountAttribute() + public function getPageVisitsAttribute() { - $totalNumber = $this->visits()->count(); + return $this->visits()->count(); + } - return $this->convertNumber($totalNumber); + /** + * Count all page visits together of the model and format it. + * + * @return int + */ + public function getPageVisitsFormattedAttribute() + { + return $this->formatIntegerHumanReadable($this->getPageVisitsAttribute()); } /** - * Count all page visits together of the model from the past 24 hours. + * Count all page visits from the last 24 hours. * * @return int */ - public function getLast24hVisitsCountAttribute() + public function getPageVisits24hAttribute() { return $this->retrievePageVisitsCountFrom(Carbon::now()->subHours(24)); } /** - * Count all page visits together of the model from the past 7 days. + * Count all page visits from the last 24 hours and format it. + * + * @return int + */ + public function getPageVisits24hFormattedAttribute() + { + return $this->formatIntegerHumanReadable($this->getPageVisits24hAttribute()); + } + + /** + * Count all page visits from the last 7 weeks. * * @return int */ - public function getLast7dVisitsCountAttribute() + public function getPageVisits7dAttribute() { return $this->retrievePageVisitsCountFrom(Carbon::now()->subDays(7)); } /** - * Count all page visits together of the model from the past 14 days. + * Count all page visits from the last 7 weeks and format it. * * @return int */ - public function getLast14dVisitsCountAttribute() + public function getPageVisits7dFormattedAttribute() + { + return $this->formatIntegerHumanReadable($this->getPageVisits7dAttribute()); + } + + /** + * Count all page visits from the last 14 days. + * + * @return int + */ + public function getPageVisits14dAttribute() { return $this->retrievePageVisitsCountFrom(Carbon::now()->subDays(14)); } + /** + * Count all page visits from the last 14 days and format it. + * + * @return int + */ + public function getPageVisits14dFormattedAttribute() + { + return $this->formatIntegerHumanReadable($this->getPageVisits14dAttribute()); + } + /** * Count all page visits of a certain time together. * @@ -109,12 +152,10 @@ public function getLast14dVisitsCountAttribute() */ public function retrievePageVisitsCountFrom(Carbon $from_date) { - $countResult = $this + return $this ->visits() ->where('created_at', '>=', $from_date) ->count(); - - return $this->convertNumber($countResult); } /** @@ -189,27 +230,6 @@ public function addVisitThatExpiresAtAndCountAll() return $this->getTotalVisitsCountAttribute(); } - /** - * Convert the visits count based upon the config settings. - * - * @param int $number - * @return \stdClass - */ - protected function convertNumber(int $number) - { - $output = new \stdClass(); - $output->number = $number; - - if ($this->configSettings['output-settings']['formatted-output-enabled']) { - $options = $this->configSettings['output-settings']['format-options']; - - $output->number = $number; - $output->formatted = $this->formatIntegerHumanReadable($number, $options); - } - - return $output; - } - /** * Format an integer to a human readable version. * @@ -217,8 +237,10 @@ protected function convertNumber(int $number) * @param array $options * @return string */ - protected function formatIntegerHumanReadable(int $number, array $options = []) + protected function formatIntegerHumanReadable($number) { + $options = $this->configSettings['output-settings']['format-options']; + return number_format( $number, $options['decimals'], diff --git a/tests/TestCases/VisitVisitableTest.php b/tests/TestCases/VisitVisitableTest.php index f0c755c3..1007d8d5 100644 --- a/tests/TestCases/VisitVisitableTest.php +++ b/tests/TestCases/VisitVisitableTest.php @@ -18,11 +18,19 @@ public function it_can_store_new_visits_into_the_database() { // Store new visit $this->testTaskModel->addVisit(); - $hasFirstVisit = ($this->testTaskModel->total_visits_count->number === 1 ? true : false); + $hasFirstVisit = ($this->testTaskModel->page_visits === 1 ? true : false); // Store new visit $this->testTaskModel->addVisit(); - $hasSecondVisit = ($this->testTaskModel->total_visits_count->number === 2 ? true : false); + $hasSecondVisit = ($this->testTaskModel->page_visits === 2 ? true : false); + + // Store new visit + $this->testTaskModel->addVisit(); + $hasFirstVisit = ($this->testTaskModel->page_visits_formatted === "3" ? true : false); + + // Store new visit + $this->testTaskModel->addVisit(); + $hasSecondVisit = ($this->testTaskModel->page_visits_formatted === "4" ? true : false); // Check first and second visits $this->assertTrue($hasFirstVisit); @@ -38,7 +46,10 @@ public function it_can_store_new_visits_with_expiry_dates_into_the_database() // Store new visit $this->testTaskModel->addVisitThatExpiresAt(Carbon::now()->addSeconds(40)); - $hasNewVisit = ($this->testTaskModel->total_visits_count->number === 1 ? true : false); + $hasNewVisit = ($this->testTaskModel->page_visits === 1 ? true : false); + $this->assertTrue($hasNewVisit); + + $hasNewVisit = ($this->testTaskModel->page_visits_formatted === "1" ? true : false); $this->assertTrue($hasNewVisit); $hasNewVisitInSession = (new SessionHistory())->isItemVisited($uniqueKey, $visitable_id); From eb5fb977cf4399e639bfac8721f4b8d488f0bef2 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Wed, 19 Jul 2017 15:02:10 +0200 Subject: [PATCH 9/9] Changing from double quotes to single quotes --- tests/TestCases/VisitVisitableTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/TestCases/VisitVisitableTest.php b/tests/TestCases/VisitVisitableTest.php index 1007d8d5..5b86c15e 100644 --- a/tests/TestCases/VisitVisitableTest.php +++ b/tests/TestCases/VisitVisitableTest.php @@ -26,11 +26,11 @@ public function it_can_store_new_visits_into_the_database() // Store new visit $this->testTaskModel->addVisit(); - $hasFirstVisit = ($this->testTaskModel->page_visits_formatted === "3" ? true : false); + $hasFirstVisit = ($this->testTaskModel->page_visits_formatted === '3' ? true : false); // Store new visit $this->testTaskModel->addVisit(); - $hasSecondVisit = ($this->testTaskModel->page_visits_formatted === "4" ? true : false); + $hasSecondVisit = ($this->testTaskModel->page_visits_formatted === '4' ? true : false); // Check first and second visits $this->assertTrue($hasFirstVisit); @@ -49,7 +49,7 @@ public function it_can_store_new_visits_with_expiry_dates_into_the_database() $hasNewVisit = ($this->testTaskModel->page_visits === 1 ? true : false); $this->assertTrue($hasNewVisit); - $hasNewVisit = ($this->testTaskModel->page_visits_formatted === "1" ? true : false); + $hasNewVisit = ($this->testTaskModel->page_visits_formatted === '1' ? true : false); $this->assertTrue($hasNewVisit); $hasNewVisitInSession = (new SessionHistory())->isItemVisited($uniqueKey, $visitable_id);