Skip to content

Commit

Permalink
Replaced cocur/slugifer with symfony/string due to syntax errors …
Browse files Browse the repository at this point in the history
…in PHP 7 builds
  • Loading branch information
caseyamcl committed Dec 8, 2024
1 parent aba3a7a commit b466c9a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 54 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"php": "^7.2|^8.0",
"ext-dom": "*",
"masterminds/html5": "^2.1",
"cocur/slugify": "^3.0|^4.0",
"symfony/string": "^5.4",
"symfony/translation-contracts": "^2.5",
"knplabs/knp-menu": "^3.2"
},
"require-dev": {
Expand Down
17 changes: 8 additions & 9 deletions src/MarkupFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use DOMElement;
use Masterminds\HTML5;
use RuntimeException;
use Cocur\Slugify\SlugifyInterface;

/**
* TOC Markup Fixer adds `id` attributes to all H1...H6 tags where they do not
Expand All @@ -40,20 +39,20 @@ class MarkupFixer
private $htmlParser;

/**
* @var SlugifyInterface
* @var SluggerInterface
*/
private $slugifier;
private $slugger;

/**
* Constructor
*
* @param HTML5|null $htmlParser
* @param SlugifyInterface|null $slugify
* @param SluggerInterface|null $slugger
*/
public function __construct(?HTML5 $htmlParser = null, ?SlugifyInterface $slugify = null)
public function __construct(?HTML5 $htmlParser = null, ?SluggerInterface $slugger = null)
{
$this->htmlParser = $htmlParser ?? new HTML5();
$this->slugifier = $slugify ?? new UniqueSlugify();
$this->slugger = $slugger ?? new UniqueSlugger();
}

/**
Expand All @@ -75,16 +74,16 @@ public function fix(string $markup, int $topLevel = 1, int $depth = 6): string
$domDocument = $this->htmlParser->loadHTML($markup);
$domDocument->preserveWhiteSpace = true; // do not clobber whitespace

// If using the default slugifier, ensure that a unique instance of the class
$slugger = $this->slugifier instanceof UniqueSlugify ? new UniqueSlugify() : $this->slugifier;
// If using the default slugger, ensure that a unique instance of the class
$slugger = $this->slugger instanceof UniqueSlugger ? new UniqueSlugger() : $this->slugger;

/** @var DOMElement $node */
foreach ($this->traverseHeaderTags($domDocument, $topLevel, $depth) as $node) {
// If no id is found, try the title attribute
$id = $node->getAttribute('id') ?: $node->getAttribute('title');

// If no title attribute, use the text content
$id = $slugger->slugify($id ?: $node->textContent);
$id = $slugger->makeSlug($id ?: $node->textContent);

// If the first character begins with a numeric, prepend 'toc-' on it.
if (ctype_digit(substr($id, 0, 1))) {
Expand Down
25 changes: 25 additions & 0 deletions src/SluggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* PHP TableOfContents Library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/toc
* @version 3
* @package caseyamcl/toc
* @author Casey McLaughlin <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
* ------------------------------------------------------------------
*/

declare(strict_types=1);

namespace TOC;

interface SluggerInterface
{
public function makeSlug(string $string): string;
}
21 changes: 10 additions & 11 deletions src/UniqueSlugify.php → src/UniqueSlugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@

namespace TOC;

use Cocur\Slugify\Slugify;
use Cocur\Slugify\SlugifyInterface;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Symfony\Component\String\Slugger\SluggerInterface as SymfonyStringSluggerInterface;

/**
* UniqueSlugify creates slugs from text without repeating the same slug twice per instance
*
* @author Casey McLaughlin <[email protected]>
*/
class UniqueSlugify implements SlugifyInterface
class UniqueSlugger implements SluggerInterface
{
/**
* @var SlugifyInterface
* @var SymfonyStringSluggerInterface
*/
private $slugify;
private $slugger;

/**
* @var array
Expand All @@ -43,24 +43,23 @@ class UniqueSlugify implements SlugifyInterface
/**
* Constructor
*
* @param SlugifyInterface|null $slugify
* @param SymfonyStringSluggerInterface|null $slugger
*/
public function __construct(?SlugifyInterface $slugify = null)
public function __construct(?SymfonyStringSluggerInterface $slugger = null)
{
$this->used = array();
$this->slugify = $slugify ?: new Slugify();
$this->slugger = $slugger ?: new AsciiSlugger();
}

/**
* Slugify
*
* @param string $string
* @param null $options
* @return string
*/
public function slugify($string, $options = null): string
public function makeSlug(string $string): string
{
$slugged = $this->slugify->slugify($string, $options);
$slugged = $this->slugger->slug($string)->lower()->toString();

$count = 1;
$orig = $slugged;
Expand Down
33 changes: 33 additions & 0 deletions tests/UniqueSluggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace TOC;

use PHPUnit\Framework\TestCase;

class UniqueSluggerTest extends TestCase
{
public function testInstantiateWithDefaults(): void
{
$slugger = new UniqueSlugger();
$this->assertInstanceOf(UniqueSlugger::class, $slugger);
}

public function testAsciiStrSlugify(): void
{
$slugger = new UniqueSlugger();
$this->assertSame('abc123456', $slugger->makeSlug('abc123456'));
}

public function testMultipleStrings(): void
{
$slugger = new UniqueSlugger();
$this->assertSame('test', $slugger->makeSlug('test'));
$this->assertSame('test-1', $slugger->makeSlug('test'));
}

public function testUnicodeSlugify(): void
{
$slugger = new UniqueSlugger();
$this->assertSame('s-c-o-u-g-i', $slugger->makeSlug('ş ç ö ü ğ ı'));
}
}
33 changes: 0 additions & 33 deletions tests/UniqueSlugifyTest.php

This file was deleted.

0 comments on commit b466c9a

Please sign in to comment.