Skip to content

Commit 88937c0

Browse files
authored
Merge pull request #5 from soap/develop
Develop
2 parents 91780d0 + 2b20e87 commit 88937c0

13 files changed

+176
-32
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"illuminate/contracts": "^10.0||^11.0"
2222
},
2323
"require-dev": {
24+
"larastan/larastan": "^2.0",
2425
"laravel/pint": "^1.14",
2526
"nunomaduro/collision": "^8.1.1||^7.10.0",
26-
"larastan/larastan": "^2.9",
2727
"orchestra/testbench": "^9.0.0||^8.22.0",
2828
"pestphp/pest": "^2.34",
2929
"pestphp/pest-plugin-arch": "^2.7",

database/migrations/create_running-numbers_table.php.stub renamed to database/migrations/create_running_numbers_table.php.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
6-
use Soap\Laravel\RunningNumbers\RuningNumber;
6+
use Soap\Laravel\RunningNumbers\RunningNumber;
77

88
return new class extends Migration
99
{

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ parameters:
99
- database
1010
tmpDir: build/phpstan
1111
checkOctaneCompatibility: true
12-
checkModelProperties: true
12+
checkModelProperties: false
1313
checkMissingIterableValueType: false
1414

src/Commands/RunningNumberGenerateCommand.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace Soap\Laravel\RunningNumbers\Commands;
44

55
use Illuminate\Console\Command;
6-
use Soap\Laravel\RunningNumbers\RunningNumber;
6+
use Soap\Laravel\RunningNumbers\RunningNumberGenerator;
77

88
class RunningNumberGenerateCommand extends Command
99
{
1010
public $signature = 'runningnumber:generate
1111
{type : Type of running number}
1212
{prefix : Prefix before running number}
1313
{--length=4 : Length of running number padding with zero}
14+
{--format={PREFIX}-{NUMBER} : Format of running number}
1415
{--reset : Reset running number to reset-value}
1516
{--reset-value=1 : Value to reset running number to}';
1617

@@ -19,14 +20,15 @@ class RunningNumberGenerateCommand extends Command
1920
public function handle(): int
2021
{
2122
$this->comment('Generate running number for '.$this->argument('type'));
22-
$this->comment('prefix: '.$this->argument('prefix'));
23+
$this->comment('Prefix: '.$this->argument('prefix'));
2324
$this->comment('Length: '.$this->option('length'));
25+
$this->comment('Format: '.$this->option('format'));
2426

25-
$runningNumber = RunningNumber::generate(
26-
$this->argument('type'),
27-
$this->argument('prefix'),
28-
$this->option('length'),
29-
);
27+
$runningNumber = RunningNumberGenerator::make()
28+
->type($this->argument('type'))
29+
->prefix($this->argument('prefix'))
30+
->prefix($this->option('length'))
31+
->format($this->option('format'))->generate();
3032

3133
$this->comment('Running number: '.$runningNumber);
3234

src/Commands/RunningNumberResetCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Soap\Laravel\RunningNumbers\Commands;
44

55
use Illuminate\Console\Command;
6+
use Soap\Laravel\RunningNumbers\RunningNumber;
67

78
class RunningNumberResetCommand extends Command
89
{

src/Models/RunningNumberKeeper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Soap\Laravel\RunningNumbers\RunningNumber;
77

8+
/**
9+
* @property string $type
10+
* @property string $prefix
11+
* @property int $number
12+
*/
813
class RunningNumberKeeper extends Model
914
{
1015
protected $fillable = [

src/RunningNumber.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function generate(string $type, string $prefix, int $length = 3, b
4444
$runningNumber->save();
4545
}
4646

47-
return $prefix.str_pad($runningNumber->number, $length, '0', STR_PAD_LEFT);
47+
return $prefix.str_pad((string) $runningNumber->number, $length, '0', STR_PAD_LEFT);
4848
}
4949

5050
/**
@@ -74,10 +74,10 @@ public static function make(string $type, string $prefix, int $length = 3, bool
7474
}
7575
}
7676

77-
return $prefix.str_pad($runningNumber->number, $length, '0', STR_PAD_LEFT);
77+
return $prefix.str_pad((string) $runningNumber->number, $length, '0', STR_PAD_LEFT);
7878
}
7979

80-
public static function reset(string $type, string $prefix, int $value = 1)
80+
public static function reset(string $type, string $prefix, string|null|int $value = 0)
8181
{
8282
$runningNumber = RunningNumberKeeper::where('type', $type)
8383
->where('prefix', $prefix)

src/RunningNumberGenerator.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
// declare(strict_types = 1);
4+
35
namespace Soap\Laravel\RunningNumbers;
46

5-
class RunningNumberGenerator
7+
/** @phpstan-consistent-constructor */
8+
final class RunningNumberGenerator
69
{
710
protected $type = 'Default';
811

@@ -16,33 +19,39 @@ class RunningNumberGenerator
1619

1720
protected $format = '{PREFIX}-{NUMBER}';
1821

19-
public static function make()
22+
private $tokens = [
23+
'TYPE',
24+
'PREFIX',
25+
'NUMBER',
26+
];
27+
28+
public static function make(): self
2029
{
21-
return new static(self::class);
30+
return new RunningNumberGenerator;
2231
}
2332

24-
public function type($type)
33+
public function type($type): self
2534
{
2635
$this->type = $type;
2736

2837
return $this;
2938
}
3039

31-
public function prefix($prefix)
40+
public function prefix($prefix): self
3241
{
3342
$this->prefix = $prefix;
3443

3544
return $this;
3645
}
3746

38-
public function length($length)
47+
public function length($length): self
3948
{
4049
$this->length = $length;
4150

4251
return $this;
4352
}
4453

45-
public function reset($value = 0)
54+
public function reset($value = 0): self
4655
{
4756
$this->reset = true;
4857
$this->runningNumber = $value;
@@ -53,17 +62,24 @@ public function reset($value = 0)
5362
/**
5463
* @todo validate format tokens
5564
*/
56-
public function format($format)
65+
public function format($format): self
5766
{
67+
$this->validateFormat($format);
68+
5869
$this->format = $format;
5970

6071
return $this;
6172
}
6273

63-
public function generate()
74+
public function generate(): string
6475
{
76+
if (empty($this->prefix)) {
77+
$this->prefix = date('Y');
78+
}
79+
6580
if ($this->reset) {
6681
RunningNumber::reset($this->type, $this->prefix, $this->runningNumber);
82+
$this->reset = false;
6783
}
6884

6985
$this->runningNumber = RunningNumber::next($this->type, $this->prefix);
@@ -77,4 +93,19 @@ public function generate()
7793
$this->format
7894
);
7995
}
96+
97+
protected function validateFormat($format): void
98+
{
99+
$pattern = "/\{([^}]+)\}/"; // Match anything inside curly braces
100+
preg_match_all($pattern, $format, $matches);
101+
102+
$resultArray = $matches[1]; // extract the tokens from the matches
103+
104+
foreach ($resultArray as $token) {
105+
if (! in_array($token, $this->tokens)) {
106+
throw new \Exception("Invalid token: {$token}");
107+
}
108+
}
109+
110+
}
80111
}

src/RunningNumberServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function configurePackage(Package $package): void
1717
$package
1818
->name('laravel-running-numbers')
1919
->hasConfigFile()
20-
->hasMigration('create_running-numbers_table')
20+
->hasMigration('create_running_numbers_table')
2121
->hasCommands([
2222
Commands\RunningNumberInstallCommand::class,
2323
Commands\RunningNumberGenerateCommand::class,

tests/ExampleTest.php

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

0 commit comments

Comments
 (0)