, (*1)
TextGenerator
Converts anything to text. Works like a toString implementation, but outside the object., (*2)
Brief description
For more details, check unit tests., (*3)
TextGenerator
It's the base interface. It takes some input and returns a textual representation of it, or throws an exception if it's not supported., (*4)
use lukaszmakuch\TextGenerator\TextGenerator;
use lukaszmakuch\TextGenerator\Exception\UnableToGetText;
/* @var $textGenerator TextGenerator */
try {
echo $textGenerator->getTextBasedOn($anything);
} catch (UnableToGetText $e) {
echo $e->getMessage();
}
ClassBasedTextGenerator
Takes into account nothing but the class of a given object., (*5)
use lukaszmakuch\TextGenerator\ClassBasedTextGenerator;
/* @var $textGenerator ClassBasedTextGenerator */
$textGenerator->addTextualRepresentationOf(
\DateTime::class,
"a DateTime object"
);
echo $textGenerator->getTextBasedOn(new \DateTime()); //"a DateTime object"
ObjectToTextConverter
Template of a generator that converts an object to text., (*6)
use lukaszmakuch\TextGenerator\ObjectToTextConverter;
class DateTimeTextPresenter extends ObjectToTextConverter
{
protected function getClassOfSupportedObjects()
{
return \DateTime::class;
}
protected function getTextBasedOnObject($object)
{
/* @var $object \DateTime */
return $object->format("It's " . $object->format("Y"));
}
}
$textGenerator = new DateTimeTextPresenter();
echo $textGenerator->getTextBasedOn(new \DateTime("2016-01-01")); //"It's 2016"
TextGeneratorWithDefaultText
Decorator that returns some default text if the decorated one doesn't support the given input., (*7)
use lukaszmakuch\TextGenerator\TextGeneratorWithDefaultText;
use lukaszmakuch\TextGenerator\TextGenerator;
$textGenerator = new TextGeneratorWithDefaultText(
/* @var $actualGenerator TextGenerator */
$actualGenerator,
"default text if the input is not supported"
);
NULLTextGenerator
Returns an empty string for any given input., (*8)
use lukaszmakuch\TextGenerator\NULLTextGenerator;
$textGenerator = NULLTextGenerator::getInstance();
$textGenerator->getTextBasedOn($anything); //an empty string
StaticTextGenerator
Always returns the same text regardless what is the input., (*9)
use lukaszmakuch\TextGenerator\StaticTextGenerator;
(new StaticTextGenerator("abc"))->getTextBasedOn("anything"); //abc
SimpleTextGeneratorProxy
Hides some actual implementation under the hood. Useful when solving circular dependencies., (*10)
use lukaszmakuch\TextGenerator\SimpleTextGeneratorProxy;
use lukaszmakuch\TextGenerator\TextGenerator;
/* @var $actualGenerator TextGenerator */
$textGenerator = new SimpleTextGeneratorProxy();
$textGenerator->setActualGenerator(actualGenerator)
ClassBasedTextGeneratorProxy
Delegates work based on the class of a given object., (*11)
use lukaszmakuch\TextGenerator\SimpleTextGeneratorProxy;
use lukaszmakuch\TextGenerator\TextGenerator;
/* @var $dateTimeTextGenerator TextGenerator */
/* @var $someClassObjectsTextGenerator TextGenerator */
$textGenerator = new ClassBasedTextGeneratorProxy();
$textGenerator->registerActualGenerator(
\DateTime::class,
$dateTimeTextGenerator
);
$textGenerator->registerActualGenerator(
SomeClass::class,
$someClassObjectsTextGenerator
);
ChainOfTextGenerators
Returns the value generated by the first generator that is able to generate any output based on the given input., (*12)
use lukaszmakuch\TextGenerator\TextGenerator;
use lukaszmakuch\TextGenerator\ChainOfTextGenerators;
$chain = (new ChainOfTextGenerators())
->add($ifThisGeneratorFails)
->add($thisOneIsUsed)
->add($andIfItFailsThenThisOneIsUsed);
Installation
Use composer to get the latest version:, (*13)
$ composer require lukaszmakuch/text-generator