Generate   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 16
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A gravatar() 0 19 3
A slug() 0 10 1
1
<?php
2
3
namespace DrMVC\Helpers;
4
5
/**
6
 * Few generators of some non typical content
7
 * @package DrMVC\Helpers
8
 */
9
class Generate
10
{
11
    /**
12
     * Get either a Gravatar URL or complete image tag for a specified email address.
13
     * @source https://gravatar.com/site/implement/images/php/
14
     *
15
     * @param   string $email The email address
16
     * @param   int $s Size in pixels, defaults to 80px [ 1 - 2048 ]
17
     * @param   string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
18
     * @param   string $r Maximum rating (inclusive) [ g | pg | r | x ]
19
     * @param   bool $img True to return a complete IMG tag False for just the URL
20
     * @param   array $attrs Optional, additional key/value attributes to include in the IMG tag
21
     * @return  string containing either just a URL or a complete image tag
22
     */
23
    public static function gravatar(
24
        string $email,
25
        int $s = 80,
26
        string $d = 'mm',
27
        string $r = 'g',
28
        bool $img = false,
29
        array $attrs = []
30
    ) {
31
        $url = 'https://www.gravatar.com/avatar/';
32
        $url .= md5(strtolower(trim($email)));
33
        $url .= "?s=$s&d=$d&r=$r";
34
        if ($img) {
35
            $url = '<img src="' . $url . '"';
36
            foreach ($attrs as $key => $val) {
37
                $url .= ' ' . $key . '="' . $val . '"';
38
            }
39
            $url .= ' />';
40
        }
41
        return $url;
42
    }
43
44
    /**
45
     * Generate correct url from string (UTF-8 supported)
46
     *
47
     * @param   string $slug
48
     * @return  string
49
     */
50
    public static function slug(string $slug): string
51
    {
52
        $lettersNumbersSpacesHyphens = '/[^\-\s\pN\pL]+/u';
53
        $spacesDuplicateHypens = '/[\-\s]+/';
54
55
        $slug = preg_replace($lettersNumbersSpacesHyphens, '', $slug);
56
        $slug = preg_replace($spacesDuplicateHypens, '-', $slug);
57
        $slug = trim($slug, '-');
58
59
        return mb_strtolower($slug, 'UTF-8');
60
    }
61
62
}
63