Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: CI

on:
push:
branches: [ main, master, develop ]
pull_request:
branches: [ main, master, develop ]

jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ['8.0', '8.1', '8.2', '8.3', '8.4']
dependency-version: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php-version }} - ${{ matrix.dependency-version }}

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, xml, curl, xdebug
coverage: xdebug

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependency-version }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependency-version }}-
${{ runner.os }}-php-${{ matrix.php-version }}-
${{ runner.os }}-php-

- name: Install dependencies
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: vendor/bin/phpunit

- name: Generate coverage report (only on PHP 8.1 stable)
if: matrix.php-version == '8.1' && matrix.dependency-version == 'prefer-stable'
run: |
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover coverage.xml

- name: Upload coverage to Codecov
if: matrix.php-version == '8.1' && matrix.dependency-version == 'prefer-stable'
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: false
74 changes: 74 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Code Coverage

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: "pages"
cancel-in-progress: false

jobs:
coverage:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
extensions: mbstring, xml, curl, xdebug
coverage: xdebug

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer run-script test

- name: Generate coverage report
run: |
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html coverage-report --coverage-clover coverage.xml --log-junit junit.xml

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella

- name: Setup Pages
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
uses: actions/configure-pages@v4

- name: Upload coverage artifact
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
uses: actions/upload-pages-artifact@v3
with:
path: './coverage-report'

- name: Deploy to GitHub Pages
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
id: deployment
uses: actions/deploy-pages@v4
46 changes: 43 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
.idea
vendor
# IDEs
.idea/
.vscode/
*.swp
*.swo

# Dependencies
vendor/
composer.lock
.phpunit.cache/test-results

# Testing
.phpunit.cache/
phpunit.xml.local
.phpunit.result.cache

# Coverage reports
coverage/
.coverage/
clover.xml

# Logs
*.log

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# PHP CS Fixer
.php-cs-fixer.cache

# PHPStan
.phpstan.cache

# Psalm
.psalm/

# Temporary files
*.tmp
*.temp
130 changes: 102 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,132 @@
Prepositioner
=============

PHP Prepositioner for replacing prepositions with   after preposition
PHP Prepositioner for replacing prepositions with ` ` after preposition

[![Code Climate](https://codeclimate.com/github/tomaj/prepositioner/badges/gpa.svg)](https://codeclimate.com/github/tomaj/prepositioner)
[![Test Coverage](https://api.codeclimate.com/v1/badges/d82eb747d9ac33571be3/test_coverage)](https://codeclimate.com/github/tomaj/prepositioner/test_coverage)
[![Latest Stable Version](https://poser.pugx.org/tomaj/prepositioner/v/stable.svg)](https://packagist.org/packages/tomaj/prepositioner)
[![License](https://poser.pugx.org/tomaj/prepositioner/license.svg)](https://packagist.org/packages/tomaj/prepositioner)

## Requirements

Instalation
-----------
- **PHP 8.0** or higher
- Uses modern PHP 8.0+ features including:
- Constructor property promotion
- Readonly properties
- Match expressions
- Attributes instead of docblock annotations
- Union types
- Spread operator in arrays

## Installation

Install package via composer:

``` bash
$ composer require tomaj/prepositioner
```bash
composer require tomaj/prepositioner
```

## Usage

### Simple usage without Factory

```php
use Tomaj\Prepositioner\Prepositioner;

$prepositioner = new Prepositioner(['one', 'two']);
$result = $prepositioner->formatText($inputText);
```

This example replaces all occurrences of *'one'* or *'two'* strings in `$inputText` as *'one '* and *'two '*.

### Using Factory with language support

```php
use Tomaj\Prepositioner\Factory;

$prepositioner = Factory::build('slovak');
$result = $prepositioner->formatText($inputText);
```

Usage
-----
### Supported Languages

Simple usage without *Factory* is very simple:
- **Slovak** - `Factory::build('slovak')`
- **Czech** - `Factory::build('czech')`
- **Romanian** - `Factory::build('romanian')`
- **Empty** - `Factory::build('empty')` - for testing or custom usage

``` php
$prepositioner = new Tomaj\Prepositioner\Prepositioner(['one', 'two']);
$prepositioner->formatText($inputText);
### Custom escape string

```php
use Tomaj\Prepositioner\Factory;

$prepositioner = Factory::build('slovak', '###CUSTOM###');
$result = $prepositioner->formatText($inputText);
```

This example replaces all occurences of *'one'* or *'two'* strings in ```$inputText``` as *'one '* and *'two '*.
## Extending

To add support for a new language, implement the `LanguageInterface`:

For using with *Factory* which contains language support try:
```php
<?php
declare(strict_types=1);

``` php
$prepositioner = Tomaj\Prepositioner\Factory::build('slovak')
$prepositioner->formatText($inputText);
namespace Tomaj\Prepositioner\Language;

final class MyLanguage implements LanguageInterface
{
private const PREPOSITIONS = ['my', 'custom', 'prepositions'];

/**
* @return array<string>
*/
public function prepositions(): array
{
return self::PREPOSITIONS;
}
}
```

Extending
---------
Then use it directly:

For new language support you need to implement new language class which implements *LanguageInterface* with prepositions. See *SlovakLanguage* for details.
```php
use Tomaj\Prepositioner\Prepositioner;
use Tomaj\Prepositioner\Language\MyLanguage;

$language = new MyLanguage();
$prepositioner = new Prepositioner($language->prepositions());
```

Upgrade
-------
Or register it for use with the Factory by placing it in the `Tomaj\Prepositioner\Language` namespace.

**From version 2 to 3**
- Minimum php version is **7.3** from now
- If you are using custom *Language* file from otside or from this repository (and don't use `Tomaj\Prepositioner\Factory`) you have to change namespace from `\Tomaj\Prepositioner\MyLanguage` to `\Tomaj\Prepositioner\Language\MyLanguage`
- *Note:* new version includes `declare(strict_types=1);` in all files
## What's New in v4.0

- **PHP 8.0+ required** - modernized codebase with latest PHP features
- **Constructor property promotion** - cleaner, more concise code
- **Readonly properties** - improved immutability and type safety
- **Match expressions** - better performance and readability in Factory
- **PHPUnit 10** - latest testing framework with attributes
- **PSR-4 autoloading** - improved autoloader efficiency
- **Final classes** - better encapsulation and performance
- **Typed arrays** - better documentation and IDE support

Known issue
-----------
## Migration from v3.x

1. each new language has to be in *Tomaj\Prepositioner\Language* namespace if you would like to use Factory
- Minimum PHP version is now **8.0**
- All functionality remains the same - no breaking changes to public API
- If extending classes, note that most classes are now `final`
- Tests now use PHPUnit 10 with attributes instead of docblock annotations

## Development

Run tests:
```bash
composer test
```

Code style:
```bash
composer cs
```
32 changes: 27 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,38 @@
}
],
"require": {
"php": ">= 7.2"
"php": "^8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0",
"squizlabs/php_codesniffer": "~3.5"
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.7"
},
"minimum-stability": "stable",
"autoload": {
"classmap": [
"src/"
"psr-4": {
"Tomaj\\Prepositioner\\": "src/Prepositioner/"
}
},
"autoload-dev": {
"psr-4": {
"Tomaj\\Prepositioner\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit",
"test-coverage": "phpunit --coverage-html coverage",
"cs-check": "phpcs --standard=PSR12 src tests",
"cs-fix": "phpcbf --standard=PSR12 src tests",
"quality": [
"@cs-check",
"@test"
]
},
"config": {
"optimize-autoloader": true,
"sort-packages": true,
"allow-plugins": {
"*": false
}
}
}
Loading
Loading