Skip to content

Commit 005f546

Browse files
committed
ready for 4.0
1 parent e533213 commit 005f546

File tree

9 files changed

+89
-68
lines changed

9 files changed

+89
-68
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
CHANGELOG
22
=========
33

4+
4.0.0 (2024-05-26)
5+
------------------
6+
7+
- Added support for Laravel 11.0
8+
- Removed support for PHP prior to 8.2
9+
- Removed support for Laravel prior to 10.0
10+
- Removed support for `forxer/gravatar` prior to 5.0
11+
12+
413
3.0.0 (2023-03-22)
514
------------------
615

@@ -23,7 +32,7 @@ CHANGELOG
2332

2433
- Removed support for PHP prior to 8.0
2534
- Removed support for Laravel prior to 8.0
26-
- Use at least `forxer/gravatar` 4.0
35+
- Removed support for `forxer/gravatar` prior to 4.0
2736
- Renamed `forxer\LaravelGravatar\` namespace to `LaravelGravatar\`
2837

2938

README.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Index
2525
- [Installation](#installation)
2626
- [Usage](#usage)
2727
- [Retrieve instances](#retrieve-instances)
28+
- [Retrieve the Gravatar URL](#retrieve-the-gravatar-url)
2829
- [Show directly in your views](#show-directly-in-your-views)
2930
- [Mandatory parameter](#mandatory-parameter)
3031
- [Optional parameters](#optional-parameters)
@@ -41,10 +42,11 @@ Index
4142
Requirements
4243
------------
4344

44-
- PHP 8.0 or newer
45-
- Laravel 8.0 or newer
45+
- PHP 8.2 or newer
46+
- Laravel 10.0 or newer
47+
48+
If you want to use it with a version earlier than PHP 8.2 and/or a version earlier than Laravel 10, please use [version 2](https://github.com/forxer/laravel-gravatar/tree/2.x) or [version 1](https://github.com/forxer/laravel-gravatar/tree/1.x).
4649

47-
If you want to use it with a version earlier than PHP 8 and/or a version earlier than Laravel 8, please use [version 1](https://github.com/forxer/laravel-gravatar/tree/1.x).
4850

4951
Installation
5052
------------
@@ -76,7 +78,7 @@ Whatever method you use, you could use the `url()` method to retrieve it. Or dis
7678

7779
### Retrieve instances
7880

79-
With the helper
81+
With the helper:
8082

8183
```php
8284
$gravatar = gravatar();
@@ -130,8 +132,22 @@ class UserController
130132
}
131133
```
132134

135+
### Retrieve the Gravatar URL
136+
137+
Simply use the `url()` method.
138+
139+
```php
140+
$gravatar = gravatar('email@example.com');
141+
$gravatarUrl = $gravatar->url();
142+
143+
$gravatar = Gravatar::avatar('email@example.com');
144+
$gravatarUrl = $gravatar->url();
145+
```
146+
133147
### Show directly in your views
134148

149+
As classes implement the `toString()` method you can use instances directly.
150+
135151
```blade
136152
<img src="{{ gravatar('email@example.com') }}">
137153
@@ -142,6 +158,7 @@ class UserController
142158

143159
[Back to top ^](#gravatar-for-laravel)
144160

161+
145162
Mandatory parameter
146163
-------------------
147164

@@ -164,6 +181,7 @@ These previous examples are also valid for the profile.
164181

165182
[Back to top ^](#gravatar-for-laravel)
166183

184+
167185
Optional parameters
168186
-------------------
169187

@@ -347,6 +365,7 @@ $avatar = gravatar('email@example.com')
347365

348366
[Back to top ^](#gravatar-for-laravel)
349367

368+
350369
Image presets
351370
-------------
352371

@@ -420,15 +439,21 @@ $gravatarImage->preset('small');
420439

421440
[Back to top ^](#gravatar-for-laravel)
422441

442+
423443
Casts
424444
-----
425445

426446
Let's imagine that your user model has a column "gravatar" which represents the email to use. You can cast this attribute to directly obtain an instance of `LaravelGravatar\Image`:
427447

428448
```php
449+
<?php
450+
451+
namespace App\Models;
452+
453+
use Illuminate\Database\Eloquent\Model;
429454
use LaravelGravatar\Casts\GravatarImage;
430455

431-
class Post extends Model
456+
class User extends Model
432457
{
433458
protected $casts = [
434459
'gravatar' => GravatarImage::class,
@@ -472,7 +497,7 @@ You can also cast to an instance of `LaravelGravatar\Profile`:
472497
```php
473498
use LaravelGravatar\Casts\GravatarProfile;
474499

475-
class Post extends Model
500+
class User extends Model
476501
{
477502
protected $casts = [
478503
'gravatar' => GravatarProfile::class,

UPGRADE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
UPGRADE
22
=======
33

4+
From 3.x to 4.x
5+
---------------
6+
7+
This package now requires at least **PHP 8.2** and **Laravel 10**, your project must correspond to this prerequisites.
8+
9+
410
From 2.x to 3.x
511
---------------
612

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"require": {
2222
"php": "^8.2",
2323
"laravel/framework": "^10.0 || ^11.0",
24-
"forxer/gravatar": "^4.0"
24+
"forxer/gravatar": "^5.0"
2525
},
2626
"require-dev": {
2727
"laravel/pint": "^1.16.0",

rector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
88
use Rector\Config\RectorConfig;
99
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
10+
use Rector\Set\ValueObject\LevelSetList;
1011
use Rector\Set\ValueObject\SetList;
1112
use RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector;
1213
use RectorLaravel\Rector\PropertyFetch\OptionalToNullsafeOperatorRector;
@@ -62,13 +63,12 @@
6263

6364
$rectorConfig->sets([
6465
LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES,
66+
LevelSetList::UP_TO_PHP_82,
6567
SetList::PHP_82,
6668
SetList::DEAD_CODE,
6769
SetList::CODE_QUALITY,
68-
//SetList::CODING_STYLE,
69-
//SetList::NAMING,
70+
SetList::CODING_STYLE,
7071
SetList::TYPE_DECLARATION,
71-
//SetList::PRIVATIZATION,
7272
SetList::EARLY_RETURN,
7373
SetList::INSTANCEOF,
7474
]);

src/Casts/GravatarImage.php

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,24 @@
44

55
namespace LaravelGravatar\Casts;
66

7-
use Gravatar\Image;
8-
use Illuminate\Contracts\Container\BindingResolutionException;
7+
use Illuminate\Container\Container;
98
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
109
use Illuminate\Database\Eloquent\Model;
1110

1211
class GravatarImage implements CastsAttributes
1312
{
14-
protected $presetName;
15-
16-
/**
17-
* Constructor.
18-
*/
19-
public function __construct(?string $presetName = null)
20-
{
21-
$this->presetName = $presetName;
13+
public function __construct(
14+
protected ?string $presetName = null
15+
) {
2216
}
2317

24-
/**
25-
* Cast the given value.
26-
*
27-
* @return Image
28-
*
29-
* @throws BindingResolutionException
30-
*/
3118
public function get(Model $model, string $key, mixed $value, array $attributes)
3219
{
33-
return app()->make('gravatar')->image($value)->setPreset($this->presetName);
20+
return Container::getInstance()->make('gravatar')
21+
->image($value)
22+
->setPreset($this->presetName);
3423
}
3524

36-
/**
37-
* Prepare the given value for storage.
38-
*
39-
* @param mixed|null $value
40-
* @return mixed
41-
*/
4225
public function set(Model $model, string $key, mixed $value, array $attributes)
4326
{
4427
return $value;

src/Image.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ class Image extends GravatarImage
1212
{
1313
protected ?string $presetName = null;
1414

15-
/**
16-
* Construct Image instance
17-
*
18-
* @return void
19-
*/
2015
public function __construct(
2116
private array $config,
2217
?string $email = null,
@@ -33,8 +28,6 @@ public function __construct(
3328

3429
/**
3530
* Build the avatar URL based on the provided settings.
36-
*
37-
* @return string The URL to the gravatar.
3831
*/
3932
public function url(): string
4033
{
@@ -45,10 +38,8 @@ public function url(): string
4538

4639
/**
4740
* Get or set the preset name to be used.
48-
*
49-
* @param string|null $email
5041
*/
51-
public function preset(?string $presetName = null): Image|string|null
42+
public function preset(?string $presetName = null): self|string|null
5243
{
5344
if ($presetName === null) {
5445
return $this->getPreset();
@@ -59,8 +50,6 @@ public function preset(?string $presetName = null): Image|string|null
5950

6051
/**
6152
* Get the preset name to be used.
62-
*
63-
* @return int The current avatar size in use.
6453
*/
6554
public function getPreset(): ?string
6655
{
@@ -70,7 +59,7 @@ public function getPreset(): ?string
7059
/**
7160
* Set the preset name to be used.
7261
*/
73-
public function setPreset(?string $presetName): Image
62+
public function setPreset(?string $presetName): self
7463
{
7564
$this->presetName = $presetName;
7665

@@ -90,15 +79,15 @@ private function applyPreset(): Image
9079

9180
$presetValues = $this->presetValues();
9281

93-
if (empty($presetValues)) {
82+
if ($presetValues === []) {
9483
return $this;
9584
}
9685

9786
foreach ($presetValues as $k => $v) {
9887
if (! \in_array($k, $this->allowedSetterPresetKeys())) {
9988
throw new InvalidArgumentException(
100-
"Gravatar image could not find method to use \"$k\" key".
101-
'Allowed preset keys are: '.implode(',', $this->allowedSetterPresetKeys()).'.'
89+
sprintf('Gravatar image could not find method to use "%s" key', $k).
90+
sprintf('Allowed preset keys are: "%s".', implode('", "', $this->allowedSetterPresetKeys()))
10291
);
10392
}
10493

@@ -129,27 +118,38 @@ private function presetValues(): array
129118

130119
if (empty($this->config['presets']) || ! \is_array($this->config['presets'])) {
131120
throw new InvalidArgumentException('Unable to retrieve Gravatar presets array configuration.');
132-
} elseif (! isset($this->config['presets'][$this->presetName])) {
133-
throw new InvalidArgumentException("Unable to retrieve Gravatar preset values, \"{$this->presetName}\" is probably a wrong preset name.");
121+
}
122+
123+
if (! isset($this->config['presets'][$this->presetName])) {
124+
throw new InvalidArgumentException(sprintf('Unable to retrieve Gravatar preset values, "%s" is probably a wrong preset name.', $this->presetName));
134125
}
135126

136127
$presetValues = $this->config['presets'][$this->presetName];
137128

138129
if (empty($presetValues) || ! \is_array($presetValues)) {
139-
throw new InvalidArgumentException("Unable to retrieve Gravatar \"{$this->presetName}\" preset values.");
130+
throw new InvalidArgumentException(sprintf('Unable to retrieve Gravatar "%s" preset values.', $this->presetName));
140131
}
141132

142133
return $presetValues;
143134
}
144135

145-
private function allowedSetterPresetKeys()
136+
private function allowedSetterPresetKeys(): array
146137
{
147138
return [
148-
'size', 's',
149-
'default_image', 'd',
150-
'max_rating', 'r',
151-
'extension', 'e',
152-
'force_default', 'f',
139+
'size',
140+
's',
141+
142+
'default_image',
143+
'd',
144+
145+
'max_rating',
146+
'r',
147+
148+
'extension',
149+
'e',
150+
151+
'force_default',
152+
'f',
153153
];
154154
}
155155
}

src/ServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
class ServiceProvider extends BaseServiceProvider
1010
{
11-
public function register()
11+
public function register(): void
1212
{
1313
$this->mergeConfigFrom(__DIR__.'/../config/gravatar.php', 'gravatar');
1414

15-
$this->app->singleton('gravatar', fn ($app) => new Gravatar($app['config']['gravatar']));
15+
$this->app->singleton('gravatar', fn ($app): Gravatar => new Gravatar($app['config']['gravatar']));
1616
}
1717

18-
public function boot()
18+
public function boot(): void
1919
{
2020
if ($this->app->runningInConsole()) {
2121
$this->publishes([

src/helpers.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
declare(strict_types=1);
44

55
use Gravatar\Image;
6+
use Illuminate\Container\Container;
67
use LaravelGravatar\Gravatar;
78

89
if (! function_exists('gravatar')) {
9-
/**
10-
* Return a gravatar instance.
11-
*/
1210
function gravatar(?string $email = null, ?string $presetName = null): Gravatar|Image
1311
{
1412
if ($email === null) {
15-
return app()->make('gravatar');
13+
return Container::getInstance()->make('gravatar');
1614
}
1715

18-
return app()->make('gravatar')->image($email, $presetName);
16+
return Container::getInstance()->make('gravatar')->image($email, $presetName);
1917
}
2018
}

0 commit comments

Comments
 (0)