Skip to content

Commit

Permalink
Deprecate the Numeric class, ref #30
Browse files Browse the repository at this point in the history
  • Loading branch information
damienalexandre committed Mar 31, 2017
1 parent 762b773 commit 54b0d72
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
CHANGELOG
=========

### Unreleased ###
### 1.0.2 (2017-03-31) ###

* fix PHP CS Fixer configuration and version
* fix #30 deprecate the Numeric class in favor of Unit

### 1.0.1 (2015-12-13) ###

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ Trademark
Handle trade­mark symbol ``, a reg­is­tered trade­mark symbol `®`, and a copy­right symbol `©`. This fixer replace
commonly used approximations: `(r)`, `(c)` and `(TM)`. A non-breaking space is put between numbers and copyright symbol too.

Numeric
Unit (formerly Numeric)
---------

Add a non-breaking space between a numeric and it's unit. Like this: `12_h`, `42_฿` or `88_%`.
Add a non-breaking space between a numeric and it's unit. Like this: `12_h`, `42_฿` or `88_%`. It was named `Numeric` before release 1.0.2, but BC is kept for now.

**It is really easy to make your own Fixers, feel free to extend the provided ones if they do not fit your typographic rules.**

Expand All @@ -159,7 +159,7 @@ en_GB
-----

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('en_GB');
```

Expand All @@ -169,7 +169,7 @@ fr_FR
Those rules apply most of the recommendations of "Abrégé du code typographique à l'usage de la presse", ISBN: 9782351130667.

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('fr_FR');
```

Expand All @@ -179,7 +179,7 @@ fr_CA
Mostly the same as fr_FR, but the space before punctuation points is not mandatory.

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('fr_CA');
```

Expand All @@ -189,7 +189,7 @@ de_DE
Mostly the same as en_GB, according to [Typefacts](http://typefacts.com/) and [Wikipedia](http://de.wikipedia.org/wiki/Typografie_f%C3%BCr_digitale_Texte).

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('de_DE');
```

Expand Down
5 changes: 5 additions & 0 deletions src/JoliTypo/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ private function compileRules($rules)
$fixer = $rule;
$className = get_class($rule);
} else {
/* BC Layer: the Numeric class is deprecated */
if (mb_strtolower($rule) === 'numeric') {
$rule = 'Unit';
}

$className = class_exists($rule) ? $rule : (class_exists('JoliTypo\\Fixer\\'.$rule) ? 'JoliTypo\\Fixer\\'.$rule : false);
if (!$className) {
throw new BadRuleSetException(sprintf('Fixer %s not found', $rule));
Expand Down
15 changes: 6 additions & 9 deletions src/JoliTypo/Fixer/Numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@
namespace JoliTypo\Fixer;

use JoliTypo\Fixer;
use JoliTypo\FixerInterface;
use JoliTypo\StateBag;

/**
* Add nbsp between numeric and units.
* {@inheritdoc}
*
* @deprecated Numeric should not be used (reserved keyword in PHP7)
*/
class Numeric implements FixerInterface
class Numeric extends Unit
{
public function fix($content, StateBag $stateBag = null)
public function __construct()
{
// Support a wide range of currencies
$content = preg_replace('@([\dº])('.Fixer::ALL_SPACES.')+([º°%Ω฿₵¢₡$₫֏€ƒ₲₴₭£₤₺₦₨₱៛₹$₪৳₸₮₩¥\w]{1})@', '$1'.Fixer::NO_BREAK_SPACE.'$3', $content);

return $content;
trigger_error('Numeric fixer is deprecated, use Unit instead.', E_USER_NOTICE);
}
}
28 changes: 28 additions & 0 deletions src/JoliTypo/Fixer/Unit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of JoliTypo - a project by JoliCode.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace JoliTypo\Fixer;

use JoliTypo\Fixer;
use JoliTypo\FixerInterface;
use JoliTypo\StateBag;

/**
* Add nbsp between numeric and units.
*/
class Unit implements FixerInterface
{
public function fix($content, StateBag $stateBag = null)
{
// Support a wide range of currencies
$content = preg_replace('@([\dº])('.Fixer::ALL_SPACES.')+([º°%Ω฿₵¢₡$₫֏€ƒ₲₴₭£₤₺₦₨₱៛₹$₪৳₸₮₩¥\w]{1})@', '$1'.Fixer::NO_BREAK_SPACE.'$3', $content);

return $content;
}
}
2 changes: 1 addition & 1 deletion tests/JoliTypo/Tests/EnglishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class EnglishTest extends \PHPUnit_Framework_TestCase
{
private $en_fixers = array('Numeric', 'Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'CurlyQuote', 'Hyphen', 'Trademark');
private $en_fixers = array('Unit', 'Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'CurlyQuote', 'Hyphen', 'Trademark');

const TOFIX = <<<TOFIX
<!-- From https://en.wikipedia.org/wiki/Gif#Pronunciation -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

use JoliTypo\Fixer;

class NumericTest extends \PHPUnit_Framework_TestCase
class UnitTest extends \PHPUnit_Framework_TestCase
{
public function testNumericUnits()
{
$fixer = new Fixer\Numeric();
$this->assertInstanceOf('JoliTypo\Fixer\Numeric', $fixer);
$fixer = new Fixer\Unit();
$this->assertInstanceOf('JoliTypo\Fixer\Unit', $fixer);

$this->assertEquals('Test', $fixer->fix('Test'));
$this->assertEquals('1'.Fixer::NO_BREAK_SPACE.'h', $fixer->fix('1 h'));
Expand Down
2 changes: 1 addition & 1 deletion tests/JoliTypo/Tests/FrenchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class FrenchTest extends \PHPUnit_Framework_TestCase
{
private $fr_fixers = array('Numeric', 'Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'CurlyQuote', 'Hyphen', 'Trademark');
private $fr_fixers = array('Unit', 'Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'CurlyQuote', 'Hyphen', 'Trademark');

const TOFIX = <<<TOFIX
<p>Ceci est à remplacer par une fâble :p</p>
Expand Down

0 comments on commit 54b0d72

Please sign in to comment.