lukaszmakuch / text-generator
Converts anything to text. Works like a toString implementation, but outside the object.
Requires
Requires (Dev)
- phpdocumentor/phpdocumentor: 2.*
- phpunit/phpunit: 5.3.*
This package is not auto-updated.
Last update: 2025-01-10 21:30:18 UTC
README
TextGenerator
Converts anything to text. Works like a toString implementation, but outside the object.
Brief description
For more details, check unit tests.
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.
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.
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.
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.
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.
use lukaszmakuch\TextGenerator\NULLTextGenerator; $textGenerator = NULLTextGenerator::getInstance(); $textGenerator->getTextBasedOn($anything); //an empty string
StaticTextGenerator
Always returns the same text regardless what is the input.
use lukaszmakuch\TextGenerator\StaticTextGenerator; (new StaticTextGenerator("abc"))->getTextBasedOn("anything"); //abc
SimpleTextGeneratorProxy
Hides some actual implementation under the hood. Useful when solving circular dependencies.
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.
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.
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:
$ composer require lukaszmakuch/text-generator