-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new remover "element by class by index"
- Loading branch information
Showing
3 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
|
||
namespace Pkscraper\Remove; | ||
|
||
|
||
use DOMXPath; | ||
use Pkscraper\Dom\Dom; | ||
|
||
class ElementByClassByIndexRemover extends Remover | ||
{ | ||
|
||
protected $class; | ||
protected $index; | ||
|
||
/** | ||
* ElementByClassByIndexRemover constructor. | ||
* @param string $class | ||
* @param int $index | ||
*/ | ||
public function __construct($class, $index) | ||
{ | ||
$this->class = $class; | ||
$this->index = $index; | ||
} | ||
|
||
public function remove(): string | ||
{ | ||
$dom = new Dom($this->document); | ||
$xpath = new DOMXPath($dom->DOMDocument); | ||
$elementsToBeRemove = $xpath->query('//*[contains(concat(" ", normalize-space(@class), " "), " ' . $this->class . ' ")]'); | ||
$elementsToBeRemove->item($this->index)->parentNode->removeChild($elementsToBeRemove->item($this->index)); | ||
return $dom->get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
|
||
namespace Pkscraper\Tests; | ||
|
||
|
||
use PHPUnit\Framework\TestCase; | ||
use Pkscraper\Remove\ElementByClassByIndexRemover; | ||
|
||
class ElementByClassByIndexRemoverTest extends TestCase | ||
{ | ||
|
||
public function testRemove() | ||
{ | ||
$remover = new ElementByClassByIndexRemover('column', 0); | ||
$remover->setDocument(file_get_contents(__DIR__ . '/fixtures/htmlDocument.html')); | ||
$result = $remover->remove(); | ||
$this->assertStringNotContainsString('first column', $result); | ||
$this->assertStringContainsString('second column', $result); | ||
|
||
$remover = new ElementByClassByIndexRemover('column', 1); | ||
$remover->setDocument(file_get_contents(__DIR__ . '/fixtures/htmlDocument.html')); | ||
$result = $remover->remove(); | ||
$this->assertStringContainsString('first column', $result); | ||
$this->assertStringNotContainsString('second column', $result); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters