Skip to content

Commit a024bbc

Browse files
committed
Refactor to use model methods instead of static properties.
1 parent a23caea commit a024bbc

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

src/HasStripeId.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace Mitchdav\StripeIds;
44

55
/**
6-
* @property static $stripeIdsAlphabet
7-
* @property static $stripeIdsLength
8-
* @property static $stripeIdsSeparator
9-
* @property static $stripeIdsPrefix
6+
* @property string $stripeIdAlphabet
7+
* @property int $stripeIdLength
8+
* @property string $stripeIdSeparator
9+
* @property string $stripeIdPrefix
1010
*/
1111
trait HasStripeId
1212
{
@@ -15,12 +15,12 @@ protected static function bootHasStripeId()
1515
static::creating(function ($model) {
1616
if (!$model->getKey()) {
1717
$stripeIds = new StripeIds(
18-
self::$stripeIdsAlphabet ?? config('stripe-ids.alphabet'),
19-
self::$stripeIdsLength ?? config('stripe-ids.length'),
20-
self::$stripeIdsSeparator ?? config('stripe-ids.separator'),
18+
$model->getStripeIdAlphabet(),
19+
$model->getStripeIdLength(),
20+
$model->getStripeIdSeparator()
2121
);
2222

23-
$model->{$model->getKeyName()} = $stripeIds->id(self::$stripeIdsPrefix);
23+
$model->{$model->getKeyName()} = $stripeIds->id($model->getStripeIdPrefix());
2424
}
2525
});
2626
}
@@ -34,4 +34,24 @@ public function getKeyType()
3434
{
3535
return 'string';
3636
}
37+
38+
public function getStripeIdAlphabet()
39+
{
40+
return $this->stripeIdAlphabet ?? config('stripe-ids.alphabet');
41+
}
42+
43+
public function getStripeIdLength()
44+
{
45+
return $this->stripeIdLength ?? config('stripe-ids.length');
46+
}
47+
48+
public function getStripeIdSeparator()
49+
{
50+
return $this->stripeIdSeparator ?? config('stripe-ids.separator');
51+
}
52+
53+
public function getStripeIdPrefix()
54+
{
55+
return $this->stripeIdPrefix;
56+
}
3757
}

tests/HasStripeIdTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ 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+
1619
$this->assertNull($model->getKey());
1720

1821
$model->save();
1922

2023
$this->assertNotNull($model->getKey());
21-
$this->assertStringStartsWith(DefaultModel::$stripeIdsPrefix, $model->getKey());
24+
$this->assertStringStartsWith($prefix.$separator, $model->getKey());
2225
}
2326

2427
/** @test */
@@ -27,20 +30,22 @@ public function it_generates_an_id_using_overridden_properties()
2730
/** @var OverriddenModel $model */
2831
$model = OverriddenModel::query()->make();
2932

33+
$prefix = $model->getStripeIdPrefix();
34+
$alphabet = $model->getStripeIdAlphabet();
35+
$length = $model->getStripeIdLength();
36+
$separator = $model->getStripeIdSeparator();
37+
3038
$this->assertNull($model->getKey());
3139

3240
$model->save();
3341

3442
$this->assertNotNull($model->getKey());
35-
$this->assertStringStartsWith(OverriddenModel::$stripeIdsPrefix, $model->getKey());
43+
$this->assertStringStartsWith($prefix.$separator, $model->getKey());
3644

37-
$pattern = '/^'.OverriddenModel::$stripeIdsPrefix.OverriddenModel::$stripeIdsSeparator.'['.OverriddenModel::$stripeIdsAlphabet.']+$/';
45+
$pattern = '/^'.$prefix.$separator.'['.$alphabet.']+$/';
3846

3947
$this->assertEquals(1, preg_match($pattern, $model->getKey()));
40-
$this->assertEquals(
41-
strlen(OverriddenModel::$stripeIdsPrefix) + strlen(OverriddenModel::$stripeIdsSeparator) + OverriddenModel::$stripeIdsLength,
42-
strlen($model->getKey())
43-
);
48+
$this->assertEquals(strlen($prefix) + strlen($separator) + $length, strlen($model->getKey()));
4449
}
4550

4651
/** @test */

tests/Models/DefaultModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class DefaultModel extends Model
99
{
1010
use HasStripeId;
1111

12-
public static $stripeIdsPrefix = 'dm';
13-
1412
protected static $unguarded = true;
1513

14+
public $stripeIdPrefix = 'dm';
15+
1616
protected $table = 'test_models';
1717
}

tests/Models/OverriddenModel.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ class OverriddenModel extends Model
99
{
1010
use HasStripeId;
1111

12-
public static $stripeIdsAlphabet = 'ABCDEF123456';
12+
protected static $unguarded = true;
1313

14-
public static $stripeIdsLength = 10;
14+
public $stripeIdAlphabet = 'ABCDEF123456';
1515

16-
public static $stripeIdsSeparator = ':';
16+
public $stripeIdLength = 10;
1717

18-
public static $stripeIdsPrefix = 'om';
18+
public $stripeIdSeparator = ':';
1919

20-
protected static $unguarded = true;
20+
public $stripeIdPrefix = 'om';
2121

2222
protected $table = 'test_models';
2323
}

0 commit comments

Comments
 (0)