Passed
Push — master ( 47fbf0...a2ba4c )
by Vicens
06:47
created

Captcha::url()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * @desc laravel 门面
4
 * @author vicens<[email protected]>
5
 */
6
7
8
namespace Vicens\Captcha\Facades;
9
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Facade;
12
use Illuminate\Support\HtmlString;
13
14
15
class Captcha extends Facade
16
{
17
18
19
    protected static function getFacadeAccessor()
20
    {
21
        return 'captcha';
22
    }
23
24
    /**
25
     * 生成验证码图片标签
26
     * @param string $name
27
     * @return string
28
     */
29
    public static function image($name = null)
30
    {
31
        return new HtmlString('<img src="' . self::src($name) . '" alt="captcha"/>');
32
    }
33
34
    /**
35
     * 获取图片验证码的URL
36
     * @param null $name
37
     * @return string
38
     */
39
    public static function src($name = null)
40
    {
41
        return self::url($name);
42
    }
43
44
    /**
45
     * 生成验证码路由
46
     * @param string $path
47
     */
48
    public static function routes($path = null)
49
    {
50
        if (!$path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
51
            $path = config('captcha.path');
52
        }
53
54
        app('router')->get($path, function (Request $request) {
55
56
            $name = $request->get('name');
57
58
            return app('captcha')->make($name)->response();
59
60
        })->name('captcha');
61
    }
62
63
    /**
64
     * 返回验证码的URL
65
     * @param string $name
66
     * @return string
67
     */
68
    public static function url($name = null)
69
    {
70
        $parameters = [];
71
72
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
73
            $parameters['name'] = $name;
74
        }
75
76
        return route('captcha', $parameters);
77
    }
78
79
    /**
80
     * 注册表单验证器
81
     */
82
    public static function validations()
83
    {
84
        // Validator extensions
85
        app('validator')->extend('captcha', function ($attribute, $value, array $parameters = []) {
86
87
            return app('captcha')->check($value, array_first($parameters));
88
        });
89
90
    }
91
92
}