Skip to content

Commit

Permalink
Merge pull request #2 from MirazMac/refactor
Browse files Browse the repository at this point in the history
Refactored and improved
  • Loading branch information
MirazMac authored Oct 19, 2021
2 parents ae09b11 + ae28449 commit c3ddcd7
Show file tree
Hide file tree
Showing 15 changed files with 1,298 additions and 394 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
.phpunit.result.cache
.phpunit.result.cache
composer.lock
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"require": {
"php": ">=5.4.0"
},
"scripts": {
"phpunit": "vendor/bin/phpunit --configuration phpunit.xml"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "4.8.36"
}
}
26 changes: 0 additions & 26 deletions example.php

This file was deleted.

2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
</exclude>
</whitelist>
</filter>
</phpunit>
</phpunit>
36 changes: 32 additions & 4 deletions src/BanglaString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace MirazMac\BanglaString;

use MirazMac\BanglaString\Translator\AvroToBijoy\Translator as AvroToBijoyTranslator;
use MirazMac\BanglaString\Translator\BijoyToAvro\Translator as BijoyToAvroTranslator;

/**
* BanglaString
*
* A wannabe all-in-all Bangla String Manupulation Library!
* This is not an ideal way to use this, but when I created this I knew very little about OOP.
* You should directly import the Translator classes and use them as such.
* However this will stay as is for backwards compatibility reasons.
*
* @author Miraz Mac <mirazmac@gmail.com>
* @link https://mirazmac.info Author Homepage
Expand All @@ -23,6 +28,13 @@ class BanglaString
*/
protected $string;

/**
* Translator instances
*
* @var array
*/
protected $translators = [];

/**
* Create a new BanglaString instance
*
Expand All @@ -42,21 +54,37 @@ public function __construct($string)
* This is identical as self::__construct(), but the difference is it allows to call the method statically
*
* @param string $string The text to be converted
* @return object
* @return self
*/
public static function translate($string)
{
return new self($string);
}

/**
* Gets the translator.
*
* @param string $name The name
*
* @return \MirazMac\BanglaString\Contracts\TranslatorContract
*/
protected function getTranslator($name)
{
if (!isset($this->translators[$name])) {
$this->translators[$name] = new $name();
}

return $this->translators[$name];
}

/**
* Translate the loaded string to Bijoy ANSI
*
* @return string
*/
public function toBijoy()
{
return Translator\AvroUnicode::getInstance()->translate($this->string);
return $this->getTranslator(AvroToBijoyTranslator::class)->translate($this->string);
}

/**
Expand All @@ -66,6 +94,6 @@ public function toBijoy()
*/
public function toAvro()
{
return Translator\BijoyAnsi::getInstance()->translate($this->string);
return $this->getTranslator(BijoyToAvroTranslator::class)->translate($this->string);
}
}
18 changes: 18 additions & 0 deletions src/Contracts/TranslatorContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace MirazMac\BanglaString\Contracts;

/**
* TranslatorContract
*/
interface TranslatorContract
{
/**
* Translates a given string
*
* @param string $string The string
*
* @return string
*/
public function translate($string);
}
Loading

0 comments on commit c3ddcd7

Please sign in to comment.