Skip to content

Commit 5503807

Browse files
committed
Remove separator, allow more flexibility in methods, and add validation rule.
1 parent 3ab68b7 commit 5503807

11 files changed

+254
-110
lines changed

config/stripe-ids.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

config/stripe_ids.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
return [
4+
5+
'hash_alphabet' => env(
6+
'STRIPE_IDS_HASH_ALPHABET',
7+
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
8+
),
9+
10+
'hash_length' => env('STRIPE_IDS_HASH_LENGTH', 16),
11+
12+
// The 'prefixes' key is optional, and is only required if you are using the StripeIds::findByStripeId() method to
13+
// find generic models by their id.
14+
15+
'prefixes' => [
16+
17+
// 'ch_' => \App\Models\Charge::class,
18+
19+
],
20+
21+
];

src/HasStripeId.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ protected static function bootHasStripeId()
1414
{
1515
static::creating(function ($model) {
1616
if (!$model->getKey()) {
17-
$stripeIds = new StripeIds(
18-
$model->getStripeIdAlphabet(),
19-
$model->getStripeIdLength(),
20-
$model->getStripeIdSeparator()
21-
);
22-
23-
$model->{$model->getKeyName()} = $stripeIds->id($model->getStripeIdPrefix());
17+
$model->{$model->getKeyName()} = $model->getStripeId();
2418
}
2519
});
2620
}
@@ -35,23 +29,30 @@ public function getKeyType()
3529
return 'string';
3630
}
3731

38-
public function getStripeIdAlphabet()
32+
public function getStripeIdHashAlphabet()
3933
{
40-
return $this->stripeIdAlphabet ?? config('stripe-ids.alphabet');
34+
return $this->stripeIdHashAlphabet ?? null;
4135
}
4236

43-
public function getStripeIdLength()
37+
public function getStripeIdHashLength()
4438
{
45-
return $this->stripeIdLength ?? config('stripe-ids.length');
39+
return $this->stripeIdHashLength ?? null;
4640
}
4741

48-
public function getStripeIdSeparator()
42+
public function getStripeIdPrefix()
4943
{
50-
return $this->stripeIdSeparator ?? config('stripe-ids.separator');
44+
return $this->stripeIdPrefix ?? array_flip(config('stripe_ids.prefixes'))[get_class()] ?? null;
5145
}
5246

53-
public function getStripeIdPrefix()
47+
public function getStripeId()
5448
{
55-
return $this->stripeIdPrefix ?? array_flip(config('stripe-ids.separator'))[get_class()] ?? null;
49+
/** @var StripeIds $stripeIds */
50+
$stripeIds = app(StripeIds::class);
51+
52+
return $stripeIds->id(
53+
$this->getStripeIdPrefix(),
54+
$this->getStripeIdHashLength(),
55+
$this->getStripeIdHashAlphabet()
56+
);
5657
}
5758
}

src/Rules/ModelPrefix.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Mitchdav\StripeIds\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Str;
9+
use Mitchdav\StripeIds\HasStripeId;
10+
11+
class ModelPrefix implements Rule
12+
{
13+
/**
14+
* @var Collection
15+
*/
16+
private $prefixes;
17+
18+
public function __construct($models)
19+
{
20+
$this->prefixes = collect(Arr::wrap($models))
21+
->map(function ($model) {
22+
/** @var HasStripeId $instance */
23+
$instance = app($model);
24+
25+
return $instance->getStripeIdPrefix();
26+
})
27+
->sort();
28+
}
29+
30+
public function passes($attribute, $value)
31+
{
32+
return $this->prefixes->contains(function ($prefix) use ($value) {
33+
return Str::startsWith($value, $prefix);
34+
});
35+
}
36+
37+
public function message()
38+
{
39+
if ($this->prefixes->count() === 1) {
40+
return 'The :attribute must be prefixed with "'.$this->prefixes->first().'".';
41+
} else {
42+
return 'The :attribute must be prefixed with one of the following: "'.$this->prefixes->join('", "').'"';
43+
}
44+
}
45+
}

src/ServiceProvider.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
77
public function register()
88
{
99
$this->mergeConfigFrom(
10-
__DIR__.'/../config/stripe-ids.php', 'stripe-ids'
10+
__DIR__.'/../config/stripe_ids.php', 'stripe_ids'
1111
);
1212
}
1313

1414
public function boot()
1515
{
1616
$this->publishes([
17-
__DIR__.'/../config/stripe-ids.php' => config_path('stripe-ids.php'),
17+
__DIR__.'/../config/stripe_ids.php' => config_path('stripe_ids.php'),
1818
], 'config');
1919

2020
$this->app->singleton(StripeIds::class, function ($app) {
2121
return new StripeIds(
22-
$app['config']['stripe-ids']['alphabet'],
23-
$app['config']['stripe-ids']['length'],
24-
$app['config']['stripe-ids']['separator'],
25-
$app['config']['stripe-ids']['prefixes']
22+
$app['config']['stripe_ids']['hash_length'],
23+
$app['config']['stripe_ids']['hash_alphabet'],
24+
$app['config']['stripe_ids']['prefixes']
2625
);
2726
});
2827
}

src/StripeIds.php

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,52 @@
88

99
class StripeIds
1010
{
11-
/**
12-
* @var string
13-
*/
14-
private $alphabet;
15-
16-
/**
17-
* @var int
18-
*/
19-
private $alphabetLength;
20-
2111
/**
2212
* @var int
2313
*/
24-
private $length;
14+
private $hashLength;
2515

2616
/**
2717
* @var string
2818
*/
29-
private $separator;
19+
private $hashAlphabet;
3020

3121
/**
3222
* @var array
3323
*/
3424
private $prefixes;
3525

36-
public function __construct(string $alphabet, int $length, string $separator, array $prefixes = [])
26+
public function __construct(int $hashLength, string $hashAlphabet, array $prefixes = [])
3727
{
38-
$this->alphabet = $alphabet;
39-
$this->alphabetLength = strlen($alphabet);
40-
$this->length = $length;
41-
$this->separator = $separator;
28+
$this->hashLength = $hashLength;
29+
$this->hashAlphabet = $hashAlphabet;
4230
$this->prefixes = $prefixes;
4331
}
4432

45-
public function id(string $prefix)
33+
public function id(string $prefix, $hashLength = null, $hashAlphabet = null)
4634
{
47-
return $prefix.$this->separator.$this->hash();
35+
return $prefix.$this->hash($hashLength, $hashAlphabet);
4836
}
4937

50-
public function hash()
38+
public function hash($length = null, $alphabet = null)
5139
{
52-
return collect(str_split(random_bytes($this->length)))
53-
->map(function ($randomByte) {
54-
return $this->alphabet[ord($randomByte) % $this->alphabetLength];
40+
$hashLength = $length ?? $this->hashLength;
41+
$hashAlphabet = $alphabet ?? $this->hashAlphabet;
42+
$hashAlphabetLength = strlen($hashAlphabet);
43+
44+
return collect(str_split(random_bytes($hashLength)))
45+
->map(function ($randomByte) use ($hashAlphabet, $hashAlphabetLength) {
46+
return $hashAlphabet[ord($randomByte) % $hashAlphabetLength];
5547
})
5648
->join('');
5749
}
5850

59-
public function findByStripeId($id)
51+
public function findByStripeId($id, $prefixes = null)
6052
{
6153
/** @var string $model */
62-
$model = collect($this->prefixes)
54+
$model = collect($prefixes ?? $this->prefixes)
6355
->first(function ($model, $prefix) use ($id) {
64-
/** @var HasStripeId $instance */
65-
$instance = app($model);
66-
67-
return Str::startsWith($id, $prefix.$instance->getStripeIdSeparator());
56+
return Str::startsWith($id, $prefix);
6857
});
6958

7059
if ($model !== null) {

tests/HasStripeIdTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ public function it_generates_an_id_during_creating_event()
1313
/** @var DefaultModel $model */
1414
$model = DefaultModel::query()->make();
1515

16-
$prefix = $model->getStripeIdPrefix();
17-
$separator = $model->getStripeIdSeparator();
18-
1916
$this->assertNull($model->getKey());
2017

2118
$model->save();
2219

2320
$this->assertNotNull($model->getKey());
24-
$this->assertStringStartsWith($prefix.$separator, $model->getKey());
21+
$this->assertStringStartsWith($model->getStripeIdPrefix(), $model->getKey());
2522
}
2623

2724
/** @test */
@@ -31,21 +28,20 @@ public function it_generates_an_id_using_overridden_properties()
3128
$model = OverriddenModel::query()->make();
3229

3330
$prefix = $model->getStripeIdPrefix();
34-
$alphabet = $model->getStripeIdAlphabet();
35-
$length = $model->getStripeIdLength();
36-
$separator = $model->getStripeIdSeparator();
31+
$alphabet = $model->getStripeIdHashAlphabet();
32+
$length = $model->getStripeIdHashLength();
3733

3834
$this->assertNull($model->getKey());
3935

4036
$model->save();
4137

4238
$this->assertNotNull($model->getKey());
43-
$this->assertStringStartsWith($prefix.$separator, $model->getKey());
39+
$this->assertStringStartsWith($prefix, $model->getKey());
4440

45-
$pattern = '/^'.$prefix.$separator.'['.$alphabet.']+$/';
41+
$pattern = '/^'.$prefix.'['.$alphabet.']+$/';
4642

4743
$this->assertEquals(1, preg_match($pattern, $model->getKey()));
48-
$this->assertEquals(strlen($prefix) + strlen($separator) + $length, strlen($model->getKey()));
44+
$this->assertEquals(strlen($prefix) + $length, strlen($model->getKey()));
4945
}
5046

5147
/** @test */

tests/Models/DefaultModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ class DefaultModel extends Model
1111

1212
protected static $unguarded = true;
1313

14-
public $stripeIdPrefix = 'dm';
14+
public $stripeIdPrefix = 'dm_';
1515
}

tests/Models/OverriddenModel.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ class OverriddenModel extends Model
1111

1212
protected static $unguarded = true;
1313

14-
public $stripeIdAlphabet = 'ABCDEF123456';
14+
public $stripeIdHashAlphabet = 'ABCDEF123456';
1515

16-
public $stripeIdLength = 10;
16+
public $stripeIdHashLength = 10;
1717

18-
public $stripeIdSeparator = ':';
19-
20-
public $stripeIdPrefix = 'om';
18+
public $stripeIdPrefix = 'om:';
2119
}

0 commit comments

Comments
 (0)