Skip to content

Commit 896f84f

Browse files
committed
Initial commit
0 parents  commit 896f84f

File tree

8 files changed

+237
-0
lines changed

8 files changed

+237
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/composer.lock
3+
/phpunit.xml

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: php
2+
dist: precise
3+
sudo: false
4+
5+
php:
6+
- 7.1
7+
- 7.2
8+
- 7.3
9+
10+
matrix:
11+
fast_finish: true
12+
13+
cache:
14+
directories:
15+
- $HOME/.composer/cache
16+
17+
before_install:
18+
- phpenv config-rm xdebug.ini || true
19+
20+
before_script:
21+
- travis_retry composer self-update
22+
- travis_retry composer install --no-interaction --prefer-dist
23+
24+
script:
25+
- php vendor/bin/phpunit

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Dimitri Gritsajuk
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Gravatar image provider for Faker
2+
3+
[![Build Status](https://travis-ci.org/ottaviano/faker-gravatar.svg?branch=master)](https://travis-ci.org/ottaviano/faker-gravatar)
4+
5+
### Requirements
6+
7+
- PHP >= 7.1
8+
9+
### Installation
10+
11+
```bash
12+
composer require ottaviano/faker-gravatar --dev
13+
```

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "ottaviano/faker-gravatar",
3+
"license": "MIT",
4+
"description": "Faker Gravatar image provider",
5+
"type": "library",
6+
"keywords": ["faker", "fixtures", "data", "gravatar", "image", "avatar"],
7+
"authors": [
8+
{
9+
"name": "Dimitri Gritsajuk",
10+
"email": "gritsajuk.dimitri@gmail.com"
11+
}
12+
],
13+
"require": {
14+
"php": "^7.1"
15+
},
16+
"require-dev": {
17+
"fzaninotto/faker": "^1.6",
18+
"phpunit/phpunit": "^7.5",
19+
"ext-curl": "*"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Ottaviano\\Faker\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Test\\Ottaviano\\Faker\\": "tests/"
29+
}
30+
}
31+
}

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.4/phpunit.xsd"
5+
colors="true"
6+
bootstrap="vendor/autoload.php"
7+
>
8+
<testsuites>
9+
<testsuite name="Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/Gravatar.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Ottaviano\Faker;
4+
5+
use Faker\Provider\Base;
6+
7+
class Gravatar extends Base
8+
{
9+
private const MODES = [
10+
'blank',
11+
'identicon',
12+
'monsterid',
13+
'mp',
14+
'retro',
15+
'robohash',
16+
'wavatar',
17+
];
18+
19+
private const URL = 'https://www.gravatar.com/avatar/%s.jpg?d=%s&size=%d';
20+
21+
public static function gravatarUrl(string $mode = null, string $email = null, int $size = 80): string
22+
{
23+
if (!$mode || !in_array($mode, static::MODES, true)) {
24+
$mode = 'retro';
25+
}
26+
27+
$hash = $email ? md5(static::toLower($email)) : static::randomNumber(5, true);
28+
29+
return sprintf(static::URL, $hash, $mode, $size);
30+
}
31+
32+
public static function gravatar(string $dir = null, string $mode = null, string $email = null, int $size = 80, bool $fullPath = true): ?string
33+
{
34+
$dir = $dir ?? sys_get_temp_dir();
35+
36+
if (!is_dir($dir) || !is_writable($dir)) {
37+
throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir));
38+
}
39+
40+
$name = md5(uniqid($_SERVER['SERVER_ADDR'] ?? '', true));
41+
$filename = $name.'.jpg';
42+
$filepath = $dir.DIRECTORY_SEPARATOR.$filename;
43+
44+
$url = static::gravatarUrl($mode, $email, $size);
45+
46+
// save file
47+
if (function_exists('curl_exec')) {
48+
// use cURL
49+
$fp = fopen($filepath, 'w');
50+
$ch = curl_init($url);
51+
curl_setopt($ch, CURLOPT_FILE, $fp);
52+
$success = curl_exec($ch) && 200 === curl_getinfo($ch, CURLINFO_HTTP_CODE);
53+
fclose($fp);
54+
curl_close($ch);
55+
56+
if (!$success) {
57+
unlink($filepath);
58+
59+
// could not contact the distant URL or HTTP error - fail silently.
60+
return null;
61+
}
62+
} elseif (ini_get('allow_url_fopen')) {
63+
copy($url, $filepath);
64+
} else {
65+
return new \RuntimeException('The image formatter downloads an image from a remote HTTP server. Therefore, it requires that PHP can request remote hosts, either via cURL or fopen()');
66+
}
67+
68+
return $fullPath ? $filepath : $filename;
69+
}
70+
}

tests/GravatarTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Test\Ottaviano\Faker;
4+
5+
use Ottaviano\Faker\Gravatar;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class GravatarTest extends TestCase
9+
{
10+
public function testDefaultUrlValues()
11+
{
12+
$this->assertRegExp('#^https://www\.gravatar\.com/avatar/\d+\.jpg\?d=retro&size=80#', Gravatar::gravatarUrl());
13+
}
14+
15+
public function testGravatarUrlAcceptsCustomMode()
16+
{
17+
$this->assertRegExp('#^https://www\.gravatar\.com/avatar/\d+\.jpg\?d=mp&size=80#', Gravatar::gravatarUrl('mp'));
18+
}
19+
20+
public function testGravatarUrlAcceptsCustomSize()
21+
{
22+
$this->assertRegExp('#^https://www\.gravatar\.com/avatar/\d+\.jpg\?d=retro&size=200#', Gravatar::gravatarUrl(null, null, 200));
23+
}
24+
25+
public function testGravatarUrlAcceptsCustomEmail()
26+
{
27+
$email = 'TeSt@EmAiL.OK';
28+
$hash = md5(strtolower($email));
29+
30+
$this->assertRegExp("#^https://www\.gravatar\.com/avatar/${hash}\.jpg\?d=retro&size=80#", Gravatar::gravatarUrl(null, $email));
31+
}
32+
33+
public function testDownloadWithDefaults()
34+
{
35+
$curlPing = curl_init('https://www.gravatar.com');
36+
37+
curl_setopt($curlPing, CURLOPT_TIMEOUT, 5);
38+
curl_setopt($curlPing, CURLOPT_CONNECTTIMEOUT, 5);
39+
curl_setopt($curlPing, CURLOPT_RETURNTRANSFER, true);
40+
41+
curl_exec($curlPing);
42+
43+
$httpCode = curl_getinfo($curlPing, CURLINFO_HTTP_CODE);
44+
45+
curl_close($curlPing);
46+
47+
if ($httpCode < 200 | $httpCode > 300) {
48+
$this->markTestSkipped('Gravatar is offline, skipping image download');
49+
}
50+
51+
$file = Gravatar::gravatar();
52+
53+
$this->assertFileExists($file);
54+
55+
$this->assertEquals('jpg', pathinfo($file, PATHINFO_EXTENSION));
56+
57+
if (file_exists($file)) {
58+
unlink($file);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)