@@ -3,6 +3,9 @@
 | 
			
		||||
namespace App\Http\Controllers;
 | 
			
		||||
 | 
			
		||||
use App\Post;
 | 
			
		||||
use App\PostsTag;
 | 
			
		||||
use App\Services\TagDetectorService;
 | 
			
		||||
use App\Tag;
 | 
			
		||||
use DateTime;
 | 
			
		||||
use Illuminate\Support\Facades\Auth;
 | 
			
		||||
use Illuminate\Support\Facades\Redirect;
 | 
			
		||||
@@ -46,7 +49,7 @@ class HomeController extends Controller
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function store()
 | 
			
		||||
    public function store(TagDetectorService $tag_detector)
 | 
			
		||||
    {
 | 
			
		||||
        $today = new DateTime();
 | 
			
		||||
 | 
			
		||||
@@ -60,7 +63,23 @@ class HomeController extends Controller
 | 
			
		||||
            'content'   => $data['message'],
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        $tags = $tag_detector->detectFrom($data['content']);
 | 
			
		||||
 | 
			
		||||
        $check = Post::create($data);
 | 
			
		||||
        $all_tags = Tag::all();
 | 
			
		||||
        $all_tags_names = [];
 | 
			
		||||
        foreach ($all_tags as $tag) {
 | 
			
		||||
            $all_tags_names[$tag->id] = $tag->name;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        foreach ($tags as $tag) {
 | 
			
		||||
            if (in_array($tag, $all_tags_names)) {
 | 
			
		||||
                PostsTag::create([
 | 
			
		||||
                    'post_id' => $check->id,
 | 
			
		||||
                    'tag_id' => array_search($tag, $all_tags_names),
 | 
			
		||||
                ]);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								app/Http/Controllers/StatsController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								app/Http/Controllers/StatsController.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Http\Controllers;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Http\Request;
 | 
			
		||||
 | 
			
		||||
class StatsController extends Controller
 | 
			
		||||
{
 | 
			
		||||
    public function tag($tag = null)
 | 
			
		||||
    {
 | 
			
		||||
        // TODO
 | 
			
		||||
        exit;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								app/Providers/TagDetectorProvider.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								app/Providers/TagDetectorProvider.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Providers;
 | 
			
		||||
 | 
			
		||||
use App\Services\TagDetectorService;
 | 
			
		||||
use Illuminate\Support\ServiceProvider;
 | 
			
		||||
 | 
			
		||||
class TagDetectorProvider extends ServiceProvider
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Register services.
 | 
			
		||||
     *
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function register()
 | 
			
		||||
    {
 | 
			
		||||
        $this->app->bind(TagDetectorService::class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Bootstrap services.
 | 
			
		||||
     *
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function boot()
 | 
			
		||||
    {
 | 
			
		||||
        //
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								app/Services/TagDetectorService.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								app/Services/TagDetectorService.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Services;
 | 
			
		||||
 | 
			
		||||
class TagDetectorService
 | 
			
		||||
{
 | 
			
		||||
    public const REGEX_FIND = '/\s#([^\s]+)(?:\s|$)/m';
 | 
			
		||||
    public const REGEX_REPLACE = '/\s(#[^\s]+)(?:\s|$)/m';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param string $text
 | 
			
		||||
     *
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public function detectFrom($text)
 | 
			
		||||
    {
 | 
			
		||||
        if (preg_match_all(self::REGEX_FIND, $text, $tags) !== false) {
 | 
			
		||||
            return $tags[1];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return [];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param string $text
 | 
			
		||||
     *
 | 
			
		||||
     * @return string|string[]|null
 | 
			
		||||
     */
 | 
			
		||||
    public function linkTagFrom($text)
 | 
			
		||||
    {
 | 
			
		||||
        return preg_replace(
 | 
			
		||||
            self::REGEX_FIND, str_replace('$1', '$1', sprintf(' <a href="%s/%s">%s</a> ', url('stats'), '$1', '#$1')),
 | 
			
		||||
            $text
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -175,6 +175,8 @@ return [
 | 
			
		||||
        App\Providers\EventServiceProvider::class,
 | 
			
		||||
        App\Providers\RouteServiceProvider::class,
 | 
			
		||||
 | 
			
		||||
        \App\Providers\TagDetectorProvider::class,
 | 
			
		||||
 | 
			
		||||
    ],
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
@extends('layouts.app')
 | 
			
		||||
 | 
			
		||||
@inject('tag_detector', 'App\Services\TagDetectorService)
 | 
			
		||||
 | 
			
		||||
@section('content')
 | 
			
		||||
<div class="container">
 | 
			
		||||
    <div class="row justify-content-center">
 | 
			
		||||
@@ -39,7 +41,7 @@
 | 
			
		||||
                    @foreach($posts as $post)
 | 
			
		||||
                        <div class="post">
 | 
			
		||||
                            <p>{{ \Carbon\Carbon::instance($post->date_post)->diffForHumans() }}</p>
 | 
			
		||||
                            <p>{{ $post->content }}</p>
 | 
			
		||||
                            <p>{!! $tag_detector->linkTagFrom($post->content) !!}</p>
 | 
			
		||||
                        </div>
 | 
			
		||||
                    @endforeach
 | 
			
		||||
                </div>
 | 
			
		||||
 
 | 
			
		||||
@@ -16,3 +16,4 @@ Auth::routes();
 | 
			
		||||
Route::get('/', 'WelcomeController@index')->name('home');
 | 
			
		||||
Route::get('/home', 'HomeController@index')->name('dashboard');
 | 
			
		||||
Route::post('store', 'HomeController@store')->name('store');
 | 
			
		||||
Route::post('stats', 'StatsController@tag')->name('stats'); // TODO make a group for stats
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user