<?php

namespace App\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('alpha_num_space', function ($attribute, $value, $parameters, $validator) {
            return preg_match('/^[\pL\pM\pN ]+$/u', $value);
        });

        Validator::extend('alpha_num_space_plus', function ($attribute, $value, $parameters, $validator) {
            return preg_match('/^[\pL\pM\pN \+\-\'\/\&,\(\)]+$/u', $value);
        });

        Validator::extend('simple_uri', function ($attribute, $value, $parameters, $validator) {
            if ($value == '/') {
                return true;
            }
            return preg_match('/^(\/[a-zA-Z0-9-]+)+$/u', $value);
        });

        Validator::extend('simple_uri_query', function ($attribute, $value, $parameters, $validator) {
            if ($value == '/') {
                return true;
            }
            return preg_match('/^(\/[a-zA-Z0-9-_]+)+(\?(&?([\[\]a-zA-Z0-9-_]+)=([a-zA-Z0-9-_]+))+)?$/u', $value);
        });

        if($this->app->environment('production')) {
            \URL::forceScheme('https');
        }

        \Response::macro('csvattachment', function ($content, $filename) {
            $headers = [
                'Content-type'        => 'text/csv',
                'Content-Disposition' => 'attachment; filename="' . $filename . '"',
            ];

            return \Response::make($content, 200, $headers);
        });

        Blade::directive('title', function ($expression) {
            return "<?php echo co2_subscript(e($expression)); ?>";
        });
    }
}
