Now you've installed Alphacon into Laravel either by adding it to your config/app.php
or
by letting it be auto discovered, you are ready to inject the interface and convert
strings!
This package comes with three default Interfaces you can dependency inject;
Interface | Produces |
---|---|
ITranslator | Produces the Translator class with no dictionary installed |
INatoTranslator | Produces the Translator class with the Nato dictionary installed |
IMorseCodeTranslator | Produces the Translator class with the Morse code dictionary installed |
In order for the translator to work, it requires a Dictionary. A dictionary must implement
a IDictionary
. For an example of this please look at
src/Dictionaries/NatoDictionary.php
If you inject ITranslator
into your method, you will need to set the Dictionary manually.
use \Midnite81\Alphacon\Contracts\Services;
public function example(ITranslator $translator)
{
$translator->setDictionary(new MyCustomDictionary());
}
Once a dictionary has been injected or if you have used a pre-defined translator such as Nato, you can then do the following;
use \Midnite81\Alphacon\Contracts\Services;
public function example(INatoTranslator $translator)
{
$result = $translator->of('Test'); // this is the string I want converting
$result->toTranslatedString(); // TANGO echo sierra tango
$result->toTranslatedArray(); // [0 => 'TANGO', 1 => 'echo', 2 => 'sierra', 3 => 'tango']
$result->toArray(); // [0 => [ 'character' => 'T', 'translated' => 'TANGO' ] 1 => ... ]
}
If you don't fancy dependency injecting the translator, a factory class has also been created.
$nato = Midnite81\Alphacon\Factory::nato();
$morseCode = Midnite81\Alphacon\Factory::morseCode();
$custom1 = Midnite81\Alphacon\Factory::make(new MyCustomDictionary());
$custom2 = Midnite81\Alphacon\Factory::makeByString(MyCustomDictionary::class);
// with any of the above you will then need to ->of($subject) to pass through the string
// you want translating.
// Alternatively you can pass the subject as the last argument
$nato = Midnite81\Alphacon\Factory::nato("my string");
$morseCode = Midnite81\Alphacon\Factory::morseCode("my string");
$custom1 = Midnite81\Alphacon\Factory::make(new MyCustomDictionary(), "my string");
$custom2 = Midnite81\Alphacon\Factory::makeByString(MyCustomDictionary::class, "my string");
To create a custom Dictionary you'll need to implement IDictionary.
class MyCustomDictionary implements \Midnite81\Alphacon\Contracts\Dictionaries\IDictionary
{
public function getLibrary(): array
{
return [
'a' => "Apple",
'b' => "Banana",
...
];
}
Then you can either pass it to ITranslator using the setDictionary method or alternatively you could extend the AlphaconService provider and bind as follows;
use Midnite81\Alphacon\AlphaconServiceProvider;
use Midnite81\Alphacon\Contracts\Services\IMyCustomTranslator;
use Midnite81\Alphacon\Services\MyCustomTranslator;
class ExtendedServiceProvider extends AlphaconServiceProvider
{
public function register(){
parent::register();
$this->app->bind(IMyCustomTranslator::class, function($app) {
new MyCustomTranslator(new MyCustomDictionary());
});
}
}
You will need to create the interface (IMyCustomTranslator) and extend ITranslator. Simply inherit from ITranslator and you'll have all the methods you require.
interface IMyCustomTranslator extends \Midnite81\Alphacon\Contracts\Services\ITranslator
{
}
You will also nee to create the concrete class (MyCustomTranslator) and extend Translator.
class MyCustomTranslator extends \Midnite81\Alphacon\Services\Translator
{
}