diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..85ff30c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,12 @@ +*.php text eol=lf +*.phpt text eol=lf +/composer.lock export-ignore +/.github/ export-ignore +/tests export-ignore +/tools export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.php-cs-fixer.php export-ignore +/baseline.xml export-ignore +/phpstan.neon export-ignore +/phpunit.xml export-ignore \ No newline at end of file diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..07c5e96 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,33 @@ + +
HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create web pages.[1] Web browsers can read HTML files and render them into visible or audible web pages. HTML describes the structure of a website semantically along with cues for presentation, making it a markup language, rather than a programming language.
'; + +StringHumanizer::truncateHtml($text, 3); // "HyperText" +StringHumanizer::truncateHtml($text, 12, ''); // "HyperText Markup" +StringHumanizer::truncateHtml($text, 50, '', '...'); // "HyperText Markup Language, commonly referred to as..." +StringHumanizer::truncateHtml($text, 75, '', '...'); // 'HyperText Markup Language, commonly referred to as HTML, is the standard markup...' + +``` + +**Remove shortcodes** + +```php +$text = 'A text with [short]random[/short] [codes]words[/codes].'; +StringHumanizer::removeShortcodes($text); // "A text with ." +StringHumanizer::removeShortcodeTags($text); // "A text with random words." +``` + ## Number **Ordinalize** ```php -use Coduo\PHPHumanizer\Number; +use Coduo\PHPHumanizer\NumberHumanizer; -echo Number::ordinalize(0); // "0th" -echo Number::ordinalize(1); // "1st" -echo Number::ordinalize(2); // "2nd" -echo Number::ordinalize(23); // "23rd" -echo Number::ordinalize(1002); // "1002nd" -echo Number::ordinalize(-111); // "-111th" +NumberHumanizer::ordinalize(0); // "0th" +NumberHumanizer::ordinalize(1); // "1st" +NumberHumanizer::ordinalize(2); // "2nd" +NumberHumanizer::ordinalize(23); // "23rd" +NumberHumanizer::ordinalize(1002, 'nl'); // "1002e" +NumberHumanizer::ordinalize(-111); // "-111th" ``` **Ordinal** ```php -use Coduo\PHPHumanizer\Number; - -echo Number::ordinal(0); // "th" -echo Number::ordinal(1); // "st" -echo Number::ordinal(2); // "nd" -echo Number::ordinal(23); // "rd" -echo Number::ordinal(1002); // "nd" -echo Number::ordinal(-111); // "th" +use Coduo\PHPHumanizer\NumberHumanizer; + +NumberHumanizer::ordinal(0); // "th" +NumberHumanizer::ordinal(1); // "st" +NumberHumanizer::ordinal(2); // "nd" +NumberHumanizer::ordinal(23); // "rd" +NumberHumanizer::ordinal(1002); // "nd" +NumberHumanizer::ordinal(-111, 'nl'); // "e" ``` **Roman numbers** ```php -use Coduo\PHPHumanizer\Number; +use Coduo\PHPHumanizer\NumberHumanizer; -echo Number::toRoman(1); // "I" -echo Number::toRoman(5); // "V" -echo Number::toRoman(1300); // "MCCC" +NumberHumanizer::toRoman(1); // "I" +NumberHumanizer::toRoman(5); // "V" +NumberHumanizer::toRoman(1300); // "MCCC" -echo Number::fromRoman("MMMCMXCIX"); // 3999 -echo Number::fromRoman("V"); // 5 -echo Number::fromRoman("CXXV"); // 125 +NumberHumanizer::fromRoman("MMMCMXCIX"); // 3999 +NumberHumanizer::fromRoman("V"); // 5 +NumberHumanizer::fromRoman("CXXV"); // 125 ``` **Binary Suffix** -```php -use Coduo\PHPHumanizer\Number; +Convert a number of bytes in to the highest applicable data unit -echo Number::binarySuffix(0); // "0 bytes" -echo Number::binarySuffix(1); // "1 bytes" -echo Number::binarySuffix(1024); // "1 kB" -echo Number::binarySuffix(1025); // "1 kB" -echo Number::binarySuffix(1536); // "1.5 kB" -echo Number::binarySuffix(1048576 * 5); // "5 MB" -echo Number::binarySuffix(1073741824 * 2); // "2 GB" -echo Number::binarySuffix(1099511627776 * 3); // "3 TB" -echo Number::binarySuffix(1325899906842624); // "1.18 PB" +```php +use Coduo\PHPHumanizer\NumberHumanizer; + +NumberHumanizer::binarySuffix(0); // "0 bytes" +NumberHumanizer::binarySuffix(1); // "1 bytes" +NumberHumanizer::binarySuffix(1024); // "1 kB" +NumberHumanizer::binarySuffix(1025); // "1 kB" +NumberHumanizer::binarySuffix(1536); // "1.5 kB" +NumberHumanizer::binarySuffix(1048576 * 5); // "5 MB" +NumberHumanizer::binarySuffix(1073741824 * 2); // "2 GB" +NumberHumanizer::binarySuffix(1099511627776 * 3); // "3 TB" +NumberHumanizer::binarySuffix(1325899906842624); // "1.18 PB" ``` Number can be also formatted for specific locale ```php -use Coduo\PHPHumanizer\Number; +use Coduo\PHPHumanizer\NumberHumanizer; -echo Number::binarySuffix(1536, 'pl'); // "1,5 kB" +NumberHumanizer::binarySuffix(1536, 'pl'); // "1,5 kB" ``` -**Metric Suffix** +Number can also be humanized with a specific number of decimal places with `preciseBinarySuffix($number, $precision, $locale = 'en')` +The precision parameter must be between 0 and 3. ```php -use Coduo\PHPHumanizer\Number; +use Coduo\PHPHumanizer\NumberHumanizer; + +NumberHumanizer::preciseBinarySuffix(1024, 2); // "1.00 kB" +NumberHumanizer::preciseBinarySuffix(1325899906842624, 3); // "1.178 PB" +``` + +This function also supports locale + +```php +use Coduo\PHPHumanizer\NumberHumanizer; + +NumberHumanizer::preciseBinarySuffix(1325899906842624, 3, 'pl'); // "1,178 PB" +``` + +**Metric Suffix** -echo Number::metricSuffix(-1); // "-1" -echo Number::metricSuffix(0); // "0" -echo Number::metricSuffix(1); // "1" -echo Number::metricSuffix(101); // "101" -echo Number::metricSuffix(1000); // "1k" -echo Number::metricSuffix(1240); // "1.2k" -echo Number::metricSuffix(1240000); // "1.24M" -echo Number::metricSuffix(3500000); // "3.5M" +```php +use Coduo\PHPHumanizer\NumberHumanizer; + +NumberHumanizer::metricSuffix(-1); // "-1" +NumberHumanizer::metricSuffix(0); // "0" +NumberHumanizer::metricSuffix(1); // "1" +NumberHumanizer::metricSuffix(101); // "101" +NumberHumanizer::metricSuffix(1000); // "1k" +NumberHumanizer::metricSuffix(1240); // "1.2k" +NumberHumanizer::metricSuffix(1240000); // "1.24M" +NumberHumanizer::metricSuffix(3500000); // "3.5M" ``` Number can be also formatted for specific locale ```php -use Coduo\PHPHumanizer\Number; +use Coduo\PHPHumanizer\NumberHumanizer; -echo Number::metricSuffix(1240000, 'pl'); // "1,24M" +NumberHumanizer::metricSuffix(1240000, 'pl'); // "1,24M" ``` ## Collections @@ -140,11 +188,11 @@ echo Number::metricSuffix(1240000, 'pl'); // "1,24M" **Oxford** ```php -use Coduo\PHPHumanizer\Collection; +use Coduo\PHPHumanizer\CollectionHumanizer; -echo Collection::oxford(['Michal', 'Norbert', 'Lukasz', 'Pawel'], 2); // "Michal, Norbert, and 2 others" -echo Collection::oxford(['Michal', 'Norbert', 'Lukasz'], 2); // "Michal, Norbert, and 1 other" -echo Collection::oxford(['Michal', 'Norbert']); // "Michal and Norbert" +CollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz', 'Pawel'], 2); // "Michal, Norbert, and 2 others" +CollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz'], 2); // "Michal, Norbert, and 1 other" +CollectionHumanizer::oxford(['Michal', 'Norbert']); // "Michal and Norbert" ``` Oxford is using translator component, so you can use whatever string format you like. @@ -154,38 +202,55 @@ Oxford is using translator component, so you can use whatever string format you **Difference** ```php -use Coduo\PHPHumanizer\DateTime; - -echo DateTime::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:00:00")); // just now -echo DateTime::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:00:05")); // 5 seconds from now -echo DateTime::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:59:00")); // 1 minute ago -echo DateTime::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:45:00")); // 15 minutes ago -echo DateTime::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:15:00")); // 15 minutes from now -echo DateTime::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 14:00:00")); // 1 hour from now -echo DateTime::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 15:00:00")); // 2 hours from now -echo DateTime::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:00:00")); // 1 hour ago -echo DateTime::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-25")); // 1 day ago -echo DateTime::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-24")); // 2 days ago -echo DateTime::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-28")); // 2 days from now -echo DateTime::difference(new \DateTime("2014-04-01"), new \DateTime("2014-04-15")); // 2 weeks from now -echo DateTime::difference(new \DateTime("2014-04-15"), new \DateTime("2014-04-07")); // 1 week ago -echo DateTime::difference(new \DateTime("2014-01-01"), new \DateTime("2014-04-01")); // 3 months from now -echo DateTime::difference(new \DateTime("2014-05-01"), new \DateTime("2014-04-01")); // 1 month ago -echo DateTime::difference(new \DateTime("2015-05-01"), new \DateTime("2014-04-01")); // 1 year ago -echo DateTime::difference(new \DateTime("2014-05-01"), new \DateTime("2016-04-01")); // 2 years from now +use Coduo\PHPHumanizer\DateTimeHumanizer; + +DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:00:00")); // just now +DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:00:05")); // 5 seconds from now +DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:59:00")); // 1 minute ago +DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:45:00")); // 15 minutes ago +DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:15:00")); // 15 minutes from now +DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 14:00:00")); // 1 hour from now +DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 15:00:00")); // 2 hours from now +DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:00:00")); // 1 hour ago +DateTimeHumanizer::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-25")); // 1 day ago +DateTimeHumanizer::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-24")); // 2 days ago +DateTimeHumanizer::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-28")); // 2 days from now +DateTimeHumanizer::difference(new \DateTime("2014-04-01"), new \DateTime("2014-04-15")); // 2 weeks from now +DateTimeHumanizer::difference(new \DateTime("2014-04-15"), new \DateTime("2014-04-07")); // 1 week ago +DateTimeHumanizer::difference(new \DateTime("2014-01-01"), new \DateTime("2014-04-01")); // 3 months from now +DateTimeHumanizer::difference(new \DateTime("2014-05-01"), new \DateTime("2014-04-01")); // 1 month ago +DateTimeHumanizer::difference(new \DateTime("2015-05-01"), new \DateTime("2014-04-01")); // 1 year ago +DateTimeHumanizer::difference(new \DateTime("2014-05-01"), new \DateTime("2016-04-01")); // 2 years from now ``` **Precise difference** ```php -use Coduo\PHPHumanizer\DateTime; +use Coduo\PHPHumanizer\DateTimeHumanizer; -echo DateTime::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-25 11:20:00")); // 1 day, 1 hour, 40 minutes ago -echo DateTime::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2015-04-28 17:00:00")); // 1 year, 2 days, 4 hours from now -echo DateTime::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2016-04-27 13:00:00")); // 2 years, 1 day from now +DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-25 11:20:00")); // 1 day, 1 hour, 40 minutes ago +DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2015-04-28 17:00:00")); // 1 year, 2 days, 4 hours from now +DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2016-04-27 13:00:00")); // 2 years, 1 day from now +``` + +## Aeon Calendar + +[Aeon PHP](https://aeon-php.org/) is a date&time oriented set of libraries. + +```php +use Coduo\PHPHumanizer\DateTimeHumanizer; + +$timeUnit = TimeUnit::days(2) + ->add(TimeUnit::hours(3)) + ->add(TimeUnit::minutes(25)) + ->add(TimeUnit::seconds(30)) + ->add(TimeUnit::milliseconds(200)); + +DateTimeHumanizer::timeUnit($timeUnit); // 2 days, 3 hours, 25 minutes, and 30.2 seconds ``` Currently we support following languages: +* [Azerbaijani](src/Coduo/PHPHumanizer/Resources/translations/difference.az.yml) * [English](src/Coduo/PHPHumanizer/Resources/translations/difference.en.yml) * [Polish](src/Coduo/PHPHumanizer/Resources/translations/difference.pl.yml) * [German](src/Coduo/PHPHumanizer/Resources/translations/difference.de.yml) @@ -194,6 +259,46 @@ Currently we support following languages: * [Português - Brasil](src/Coduo/PHPHumanizer/Resources/translations/difference.pt_BR.yml) * [Italian](src/Coduo/PHPHumanizer/Resources/translations/difference.it.yml) * [Dutch](src/Coduo/PHPHumanizer/Resources/translations/difference.nl.yml) +* [Русский](src/Coduo/PHPHumanizer/Resources/translations/difference.ru.yml) +* [Norwegian](src/Coduo/PHPHumanizer/Resources/translations/difference.no.yml) +* [Afrikaans](src/Coduo/PHPHumanizer/Resources/translations/difference.af.yml) +* [Bulgarian](src/Coduo/PHPHumanizer/Resources/translations/difference.bg.yml) +* [Indonesian](src/Coduo/PHPHumanizer/Resources/translations/difference.id.yml) +* [Chinese Simplified](src/Coduo/PHPHumanizer/Resources/translations/difference.zh_CN.yml) +* [Chinese Taiwan](src/Coduo/PHPHumanizer/Resources/translations/difference.zh_TW.yml) +* [Spanish](src/Coduo/PHPHumanizer/Resources/translations/difference.es.yml) +* [Ukrainian](src/Coduo/PHPHumanizer/Resources/translations/difference.uk.yml) +* [Danish](src/Coduo/PHPHumanizer/Resources/translations/difference.da.yml) +* [Thai](src/Coduo/PHPHumanizer/Resources/translations/difference.th.yml) +* [Japanese](src/Coduo/PHPHumanizer/Resources/translations/difference.ja.yml) +* [Romanian](src/Coduo/PHPHumanizer/Resources/translations/difference.ro.yml) + +# Development + +After downloading library update dependencies: + +``` +composer update +``` + +In order to check lowest possible versions of dependencies add + +``` +composer update --prefer-lowest +```` + +Execute test suite: + +``` +composer run test +``` + +Run CS Fixer + +``` +composer run cs:php:fix +``` + # Credits diff --git a/UPGRADE.md b/UPGRADE.md new file mode 100644 index 0000000..ab3a500 --- /dev/null +++ b/UPGRADE.md @@ -0,0 +1,9 @@ +# Upgrade 1.0 to 2.0 + +* All classes are now marked as final in order to close extra extension points +* Renamed ``Coduo\PHPHumanizer\Collection`` into ``Coduo\PHPHumanizer\CollectionHumanizer`` +* Renamed ``Coduo\PHPHumanizer\DateTime`` into ``Coduo\PHPHumanizer\DateTimeHumanizer`` +* Renamed ``Coduo\PHPHumanizer\Number`` into ``Coduo\PHPHumanizer\NumberHumanizer`` +* Renamed ``Coduo\PHPHumanizer\String`` into ``Coduo\PHPHumanizer\StringHumanizer`` +* Replaced ``ordinalSuffix($number)`` method in ``Coduo\PHPHumanizer\Number\Ordinal\StrategyInterface`` with ``isPrefix()`` and ``ordinalIndicator($number)`` +* Dependency ``thunderer/shortcode`` was removed, now shortcode lib needs to be added to project \ No newline at end of file diff --git a/composer.json b/composer.json index ad08edc..ad191e7 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "coduo/php-humanizer", "type": "library", + "description": "Humanize values that are readable only for developers", "keywords": ["php", "humanize", "humanizer"], "license": "MIT", "authors": [ @@ -14,24 +15,49 @@ } ], "require": { - "php": ">=5.3.0", - "symfony/intl": "~2.3", - "symfony/config": "~2.3", - "symfony/translation": "~2.3", - "symfony/yaml": "~2.3" + "php": "~8.3 || ~8.4 || ~8.5", + "symfony/translation": "~5.4 || ~6.4 || ~7 || ~8" }, "require-dev": { - "phpspec/phpspec": "2.0.*" + "thunderer/shortcode": "^0.7", + "aeon-php/calendar": "^1.0" }, "config": { "bin-dir": "bin" }, "autoload": { - "psr-0": {"": "src"} + "psr-4": { + "": "src/" + } }, - "extra": { - "branch-alias": { - "dev-master": "1.1-dev" + "autoload-dev": { + "psr-4": { + "": "tests/" } + }, + "suggest": { + "ext-intl": "Required if you are going to use humanizer with locales different than en_EN" + }, + "scripts": { + "build": [ + "@static:analyze", + "@test" + ], + "test": [ + "tools/vendor/bin/phpunit" + ], + "static:analyze": [ + "tools/vendor/bin/phpstan analyze -c phpstan.neon", + "tools/vendor/bin/php-cs-fixer fix --dry-run" + ], + "cs:php:fix": "tools/vendor/bin/php-cs-fixer fix", + "tools:install": "composer install --working-dir=./tools", + "tools:update": "composer update --working-dir=./tools", + "post-install-cmd": [ + "@tools:install" + ], + "post-update-cmd": [ + "@tools:update" + ] } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..d05e1a4 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + level: max + paths: + - src + + tmpDir: var/phpstan/cache diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..9f54b7e --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,22 @@ + +