Directives::partial()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Displore\Machete;
4
5
use Illuminate\Support\Facades\Blade;
6
7
class Directives
8
{
9
    /**
10
     * Format a timestamp.
11
     * From: https://laravel.com/docs/5.1/blade#extending-blade.
12
     *
13
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
14
     */
15
    public function datetime()
16
    {
17
        Blade::directive('datetime', function ($expression) {
18
            return "<?php echo with{$expression}->format('m/d/Y H:i'); ?>";
19
        });
20
    }
21
22
    /**
23
     * Shortcut to show contents to logged in users. End with `@endIf`.
24
     *
25
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
26
     */
27
    public function ifLoggedIn()
28
    {
29
        Blade::directive('ifLoggedIn', function () {
30
            return '<?php if(Auth::check()): ?>';
31
        });
32
    }
33
34
    /**
35
     * Shortcut to extend a layout.
36
     *
37
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
38
     */
39
    public function layout()
40
    {
41
        Blade::directive('layout', function ($expression) {
42
            return Blade::compileString("@extends(layouts.{$expression})");
43
        });
44
    }
45
46
    /**
47
     * Truncate the text if it is longer than a given maximum.
48
     *
49
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
50
     */
51
    public function limit()
52
    {
53
        Blade::directive('limit', function ($arguments) {
54
            list($string, $length) = explode(',', str_replace(['(', ')', ' '], '', $arguments));
55
56
            return "<?php echo str_limit({$string}, {$length}); ?>";
57
        });
58
    }
59
60
    /**
61
     * Shortcut to include a partial view.
62
     *
63
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
64
     */
65
    public function partial()
66
    {
67
        Blade::directive('partial', function ($expression) {
68
            return Blade::compileString("@include(partials.{$expression})");
69
        });
70
    }
71
72
    /**
73
     * Show the page's title.
74
     * Use `@title` in your layout, `@title('my page')` in your pages.
75
     *
76
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
77
     */
78
    public function title()
79
    {
80
        Blade::directive('title', function ($expression) {
81
82
            // If something is provided with `@title()`.
83
            if ($expression) {
84
85
                // If that something starts with `@default:` it is the default title,
86
                // Meaning it can still be overwritten by another file.
87
                if (str_contains($expression, '@default:')) {
88
                    //if (substr($title, 1, 9) == '@default:') {
89
                    $title = str_replace(['(', ')', '@default:'], '', $expression);
90
91
                    return "<title> <?php echo \$__env->yieldContent('title', {$title}); ?> </title>";
92
                }
93
94
                // Nope, no default title, show the actual one.
95
                $title = str_replace(['(', ')'], '', $expression);
96
97
                return "<?php \$__env->startSection('title', {$title}); ?>";
98
            }
99
100
            // It's just the layout placement.
101
            return "<title> <?php echo \$__env->yieldContent('title'); ?> </title>";
102
        });
103
    }
104
}
105