The purpose of this small application is to truncate a given string to a specified length with below conditions:
- The preview should never be longer than 1000 characters.
- When possible, the preview should end after a complete sentence.
It is implemented with Pure PHP.
- PHP 8.1 or higher
- An available localhost
I have implemented the single functionality of this app in a module called StringTruncator.
Some design patterns like Factory and Facade and Data Transfer Object are used inside this module.
Various OOP principles are used in this application.
StringTruncatorTransfer class is used as the DTO to handle data used inside StringTruncator module.
StringTruncatorFacade class has a truncateString method which is considered as the main API to this module.
StringTruncatorFactory class is used to instantiate the classes inside the StringTruncator module.
StringTruncatorTest class is used to test this module.
A bunch of different test scenarios are defined in the examples method.
Each scenario consists of:
- String: The original string to be truncated.
- Limit: An integer to specify the length of truncation.
- Expected: Expected string after truncation.
- Points: Points recieved after truncation.
-
Clone the project in your localhost directory
git clone https://github.com/masoudomidvar/string-truncator.git
-
Open the string-truncator directory in your localhost. By doing this, the test will be executed.
http://localhost/string-truncator