In this post, IтАЩm going to explain how you can create Laravel package locally and publish to Packagist.

To demonstrate, I will create a package to convert Hindu Arabic(Nepali) number to English and English number to Hindu Arabic (Nepali).

Creating Laravel package is not that hard.

In few simple steps you can create your own package. I will go through few steps:

Step #1: Install Laravel

I choose Laravel 9 because Laravel 9 is the latest version of Laravel. In future, you will have newer version of Laravel.

One simple command can install Laravel project in your machine. But, it has dependency on composer.

I am assuming you already have installed composer in your machine.

If you have not installed composer please install Composer

Install Laravel in your machine by running below command.

1
composer create-project --prefer-dist laravel/laravel example-app

Step #2: Folder Structure

Files and folders structure will be like below inside my example-app project.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| - app/
| - bootstrap/
| - config/
| - database/
| - lang/
| - packages/
| | - dev-scripts/
| | | - english-nepali-number-converter-php/
| | | | - src/
| | | | | - Convert.php
| | | | | - ConvertToEnglish.php
| | | | | - ConvertToNepali.php
| | | | | - EnglishNepaliNumberConverterProvider.php
| | | | - tests/
| | | | | - Unit/
| | | | | | - ConvertTest.php
| | | | | - UnitTestCase.php
| | | | - .gitignore
| | | | - composer.json
| | | | - composer.lock
| | | | - README.md
| - public/
| - resources/
| - rounts/
| - storeage/
| - tests/
| - vendor/
| - .env
| - .gitignore
| - composer.lock
| - composer.json
| - phpunit.xml
| - README.md

Step #3: Create Folder and Files inside packages folder

Go inside english-nepali-number-converter-php folder from console and run :

1
composer init

Above command creates composer.json file inside packages/dev-script/english-nepali-number-converter-php

After crating composer.json file. You can create all the folders and files inside package folder. The folder structure should look like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
| - packages/
| | - dev-scripts/
| | | - english-nepali-number-converter-php/
| | | | - src/
| | | | | - Convert.php
| | | | | - ConvertToEnglish.php
| | | | | - ConvertToNepali.php
| | | | | - EnglishNepaliNumberConverterProvider.php
| | | | - tests/
| | | | | - Unit/
| | | | | | - ConvertTest.php
| | | | | - UnitTestCase.php
| | | | - .gitignore
| | | | - composer.json
| | | | - composer.lock
| | | | - README.md

Your composer.json file will only have basic configuration. You need to update your composer.json file with additional configurations.

Updating composer.json file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{
  "name": "dev-scripts/english-nepali-number-converter-php",
  "description": "Package to convert English number to Nepali number and Nepali number to English number.",
  "keywords": [
    "Nepali Number converter",
    "English to Nepali number converter",
    "Nepali to English number converter",
    "Hindu Arabic to English number",
    "English number to Hindu Arabic",
    "Number Converter",
    "PHP package converter English number to Nepali number",
    "PHP package converter Nepali number to English number"
  ],
  "minimum-stability": "dev",
  "type": "library",
  "license": "MIT",
  "authors": [
    {
      "name": "Prakash Bhandari",
      "email": "thebhandariprakash@gmail.com"
    }
  ],
  "homepage": "https://prakashbhandari.com.np/",
  "support": {
    "issues": "https://github.com/dev-scripts/english-nepali-number-converter-php/issues"
  },
  "autoload": {
    "psr-4": {
      "DevScripts\\EnglishNepaliNumberConverter\\": "src/"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "DevScripts\\EnglishNepaliNumberConverter\\Tests\\": "tests/"
    }
  },
  "require": {
    "php": "^8.1"
  },
  "require-dev": {

  },
  "extra": {
    "laravel": {
      "providers": [
        "DevScripts\\EnglishNepaliNumberConverter\\EnglishNepaliNumberConverterProvider"
      ]
    }
  }
}

Create Convert.php, ConvertToEnglish.php, ConvertToNepali.php and EnglishNepaliNumberConverterProvider.php inside src folder.

  1. Create Convert.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

namespace DevScripts\EnglishNepaliNumberConverter;

class Convert
{
    /**
     * @param string $string
     *
     * @return string
     */
    public static function toNp(string $string): string
    {
        $convertToNepali =  new ConvertToNepali();

        return $convertToNepali->convert($string);
    }

    /**
     * @param string $string
     *
     * @return string
     */
    public static function toEn(string $string): string
    {
        $convertToEnglish =  new ConvertToEnglish();

        return $convertToEnglish->convert($string);
    }
}
  1. Create ConvertToEnglish.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

namespace DevScripts\EnglishNepaliNumberConverter;

class ConvertToEnglish
{
    /**
     * @var string[]
     */
    private  $nepaliToEnglishArray = [
        'реж' => 0,
        'рез' => 1,
        'реи' => 2,
        'рей' => 3,
        'рек' => 4,
        'рел' => 5,
        'рем' => 6,
        'рен' => 7,
        'рео' => 8,
        'реп' => 9,
        "." => ".",
    ];

    /**
     * @inheritDoc
     */
    public function convert(string $str): string
    {
        $number = "";

        foreach(mb_str_split($str) as $value) {
            if(isset($this->nepaliToEnglishArray[$value])) {
                $number .= $this->nepaliToEnglishArray[$value];
            }
        }

        return $number;
    }
}
  1. Create ConvertToNepali.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

namespace DevScripts\EnglishNepaliNumberConverter;

class ConvertToNepali
{
    /**
     * @var string[]
     */
    private  $englishToNepaliArray = [
        0 => 'реж',
        1 => 'рез',
        2 => 'реи',
        3 => 'рей',
        4 => 'рек',
        5 => 'рел',
        6 => 'рем',
        7 => 'рен',
        8 => 'рео',
        9 => 'реп',
        '.' => '.',
        ',' => ',',
    ];

    /**
     * @inheritDoc
     */
    public function convert(string $str): string
    {
        $utf = "";

        foreach(str_split($str) as $value) {
            if(isset($this->englishToNepaliArray[$value])) {
                $utf .= $this->englishToNepaliArray[$value];
            }
        }

        return $utf;
    }
}
  1. Create EnglishNepaliNumberConverterProvider.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

namespace DevScripts\EnglishNepaliNumberConverter;

use Illuminate\Support\ServiceProvider;

class EnglishNepaliNumberConverterProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->make('DevScripts\EnglishNepaliNumberConverter\Convert');
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

Write some unit test inside tests folder

  1. Create UnitTestCase.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

namespace DevScripts\EnglishNepaliNumberConverter\Tests;

use PHPUnit\Framework\TestCase;

class UnitTestCase extends TestCase
{

}
  1. Create Units/ConvertTest.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php

namespace DevScripts\EnglishNepaliNumberConverter\Tests\Unit;

use DevScripts\EnglishNepaliNumberConverter\Convert;
use DevScripts\EnglishNepaliNumberConverter\Tests\UnitTestCase;

class ConvertTest extends UnitTestCase
{
    public function test_nepali_to_english_one_digit()
    {
        $actual =  Convert::toNp(1);
        $expected = "рез";
        $this->assertEquals($expected, $actual);
    }

    public function test_nepali_to_english_small_number()
    {
       $actual =  Convert::toNp(123);
       $expected = "резреирей";
       $this->assertEquals($expected, $actual);
    }

    public function test_nepali_to_english_small_number_with_decimal()
    {
        $actual =  Convert::toNp(123.33);
        $expected = "резреирей.рейрей";
        $this->assertEquals($expected, $actual);
    }

    public function test_nepali_to_english_big_number_with_decimal()
    {
        $actual =  Convert::toNp(123456789.05);
        $expected = "резреирейрекрелремренреореп.режрел";
        $this->assertEquals($expected, $actual);
    }

    public function test_nepali_to_english_big_number_with_decimal_and_thousand_count()
    {
        $number = 123456789.05;
        $numberThousandCount = number_format($number, 2);
        $actual =  Convert::toNp($numberThousandCount);
        $expected = "резреирей,рекрелрем,ренреореп.режрел";
        $this->assertEquals($expected, $actual);
    }

    public function test_english_to_nepali_one_digit()
    {
        $actual =  Convert::toEn("рез");
        $expected = 1;
        $this->assertEquals($expected, $actual);
    }

    public function test_english_to_nepali_small_number()
    {
        $actual =  Convert::toEn("резреирей");
        $expected = 123;
        $this->assertEquals($expected, $actual);
    }

    public function test_english_to_nepali_small_number_with_decimal()
    {
        $actual =  Convert::toEn("резреирей.рейрей");
        $expected = 123.33;
        $this->assertEquals($expected, $actual);
    }

    public function test_english_to_nepali_big_number_with_decimal()
    {
        $actual =  Convert::toEn("резреирейрекрелремренреореп.режрел");
        $expected = 123456789.05;
        $this->assertEquals($expected, $actual);
    }
}

Create README.md file.

Create README.md file and update your documentation

Now your package is ready.

Step #4: Test your package locally

If you want to test locally you need to update the main composer.json file reside in the root of the project. You need to make below changes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
"require": {
"dev-scripts/english-nepali-number-converter-php": "dev-main"
},
"autoload": {
"psr-4": {
"DevScripts\\EnglishNepaliNumberConverter\\": "packages/dev-scripts/english-nepali-number-converter-php/src/"
}
},
"autoload-dev": {
"psr-4": {
"DevScripts\\EnglishNepaliNumberConverter\\Tests\\": "packages/dev-scripts/english-nepali-number-converter-php/tests/"
}
},

Run test from root folder. To run your test you should have vendor folder inside the english-nepali-number-converter-php

You already have composer.json file inside english-nepali-number-converter-php just need to run composer update this will install the phpunit package inside vendor folder.

Now, run test :

1
test packages/dev-scripts/english-nepali-number-converter-php

Also, you can test it via a web URL. You need to mack changes in web.php route file.

1
2
3
4
Route::get('/', function() {
   echo \DevScripts\EnglishNepaliNumberConverter\Convert::toNp("123454546443564364363464.100001");
    echo \DevScripts\EnglishNepaliNumberConverter\Convert::toEn("резреирейрекрелрекрелрекремрекрекрейрелремрекрейремрекрейремрейрекремрек.резрежрежрежрежрез");
});

Run app with below artisan command

1
php artisn serve

You can see result in web browser

http://localhost:8000/

Result

Step #5: Push to GitHub Public Repository

Now your package is tested and ready to publish. To publish your package you need to push it to GitHub public repository.

I have pushed this package in GitHub

Package need to be pushed form inside english-nepali-number-converter-php folder

After pushing package to the GitHub you can get the package URL

My package URL is : https://github.com/dev-scripts/english-nepali-number-converter-php

Step #6: Publish to https://packagist.org/

In https://packagist.org/ you need your account. If you don’t have account you can create one. I already have account.

So, I will go to submit page and publish my package for public use.

I aam copying the GitHub public repository URL and pest in the submit text box.

Submit package

Now, the package is publicly available to use. Anybody can use your package by running below command in their project

1
composer require dev-scripts/english-nepali-number-converter-php

Public package

In this post, we have learned how to create Laravel package, test it locally, push to GitHub public repository and finally publish to https://packagist.org/ for public use.

GitHub Repository URL : https://github.com/dev-scripts/english-nepali-number-converter-php

Packagist URL : https://packagist.org/packages/dev-scripts/english-nepali-number-converter-php