Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
haruncpi committed Oct 27, 2019
0 parents commit adebee1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Laravel ID Generator
Easy way to generate custom ID from database table in Laravel framework

# Documentation
Get documentation on [laravelarticle.com](https://laravelarticle.com/laravel-custom-id-generator)
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "haruncpi/laravel-id-generator",
"description": "Easy way to generate custom ID in laravel framework",
"license": "cc-by-4.0",
"authors": [
{
"name": "Md.Harun-Ur-Rashid",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=5.6.0",
"laravel/framework": "5.*"
},
"extra": {
"laravel": {
"providers": [
"Haruncpi\\IdGenerator\\IdGeneratorServiceProvider"
]
}
}
}
56 changes: 56 additions & 0 deletions src/IdGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Haruncpi\IdGenerator;

use DB, Exception;

class IdGenerator
{

public static function generate($configArr)
{
if (!array_key_exists('table', $configArr) || $configArr['table'] == '') {
throw new Exception('Must need a table name');
}
if (!array_key_exists('length', $configArr) || $configArr['length'] == '') {
throw new Exception('Must specify the length of ID');
}
if (!array_key_exists('prefix', $configArr) || $configArr['prefix'] == '') {
throw new Exception('Must specify a prefix of your ID');
}

if (array_key_exists('where', $configArr)) {
if (is_string($configArr['where']))
throw new Exception('where clause must be an array, you provided string');
if (!count($configArr['where']))
throw new Exception('where clause must need at least an array');
}


$prefixLength = strlen($configArr['prefix']);
$idLength = $configArr['length'] - $prefixLength;
$whereString = '';

if (array_key_exists('where', $configArr)) {
$whereString .= " WHERE ";
foreach ($configArr['where'] as $row) {
$whereString .= $row[0] . "=" . $row[1] . " AND ";
}
}

$whereString = rtrim($whereString, 'AND ');


$total = DB::select("SELECT count(id) total FROM " . $configArr['table'] . $whereString . "");

if ($total[0]->total) {
$maxId = DB::select("SELECT MAX(SUBSTR(id," . ($prefixLength + 1) . "," . $idLength . ")) maxId
FROM " . $configArr['table'] . $whereString . "");
$maxId = $maxId[0]->maxId + 1;

return $configArr['prefix'] . str_pad($maxId, $idLength, '0', STR_PAD_LEFT);
} else {
return $configArr['prefix'] . str_pad(1, $idLength, '0', STR_PAD_LEFT);
}
}
}
19 changes: 19 additions & 0 deletions src/IdGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php namespace Haruncpi\IdGenerator;

use Illuminate\Support\ServiceProvider;

class IdGeneratorServiceProvider extends ServiceProvider
{

public function boot()
{

}


public function register()
{
$this->app->make('Haruncpi\IdGenerator\IdGenerator');
}

}

0 comments on commit adebee1

Please sign in to comment.