-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9b1b046
Showing
13 changed files
with
814 additions
and
0 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,61 @@ | ||
name: ci | ||
|
||
on: [ push, pull_request ] | ||
|
||
jobs: | ||
test: | ||
permissions: | ||
contents: write | ||
name: Run tests | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
php-versions: [ '8.1', '8.2', '8.3' ] | ||
steps: | ||
- name: Checkout codebase | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup PHP Action | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php-versions }} | ||
|
||
- name: Get composer cache directory | ||
id: composer-cache | ||
run: echo "::set-output name=dir::$(composer config cache-files-dir)" | ||
|
||
- name: Cache dependencies | ||
uses: actions/cache@v3 | ||
with: | ||
path: ${{ steps.composer-cache.outputs.dir }} | ||
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: ${{ runner.os }}-composer- | ||
|
||
- name: Install dependencies | ||
run: composer install --prefer-dist | ||
|
||
- name: Recreate autoload file | ||
run: composer dump-autoload | ||
|
||
- name: Run Psalm | ||
run: ./vendor/bin/psalm | ||
|
||
- name: Run Unit Tests | ||
run: XDEBUG_MODE_COVERAGE=coverage vendor/bin/phpunit | ||
|
||
- if: ${{ (matrix.php-versions == '8.3') }} | ||
name: Generate test coverage badge | ||
uses: timkrase/phpunit-coverage-badge@v1.2.1 | ||
with: | ||
coverage_badge_path: output/coverage.svg | ||
push_badge: false | ||
|
||
- if: ${{ (matrix.php-versions == '8.3') }} | ||
name: Git push to image-data branch | ||
uses: peaceiris/actions-gh-pages@v3 | ||
with: | ||
publish_dir: ./output | ||
publish_branch: image-data | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
user_name: 'github-actions[bot]' | ||
user_email: 'github-actions[bot]@users.noreply.github.com' |
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,5 @@ | ||
/vendor | ||
composer.lock | ||
/.idea | ||
/.phpunit.cache | ||
.phpunit.result.cache |
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,221 @@ | ||
<?php | ||
|
||
namespace Panakour\DOM; | ||
|
||
use DOMDocument; | ||
use DOMElement; | ||
use DOMNodeList; | ||
use InvalidArgumentException; | ||
|
||
class Dom | ||
{ | ||
/** | ||
* @var DOMDocument | ||
*/ | ||
public $DOMDocument; | ||
|
||
/** | ||
* Dom constructor. | ||
* | ||
* @param string $document | ||
*/ | ||
public function __construct(string $document) | ||
{ | ||
$this->DOMDocument = new DOMDocument(); | ||
$this->handleInvalidHtml(); | ||
$this->loadDocument($document); | ||
} | ||
|
||
public function get(): string | ||
{ | ||
return utf8_decode($this->DOMDocument->saveHTML($this->DOMDocument->documentElement)); | ||
} | ||
|
||
/** | ||
* @param string $element Html elements: 'img','p','h1' | ||
* @return DOMNodeList | ||
*/ | ||
public function getNodeList(string $element): DOMNodeList | ||
{ | ||
return $this->DOMDocument->getElementsByTagName($element); | ||
} | ||
|
||
/** | ||
* @param string $element | ||
* @param string $attribute Html attribute 'src','href','alt' | ||
* @return array | ||
*/ | ||
public function getAttributesValue(string $element, string $attribute): array | ||
{ | ||
$nodeList = $this->getNodeList($element); | ||
$attributeValues = []; | ||
foreach ($nodeList as $element) { | ||
$attributeValues[] = $element->getAttribute($attribute); | ||
} | ||
|
||
return $attributeValues; | ||
} | ||
|
||
/** | ||
* @param string $element | ||
* @param array $attributes This is the attributes values that it's going to be replaced | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function replaceAttributes(string $element, array $attributes) | ||
{ | ||
$nodeList = $this->getNodeList($element); | ||
if (!$this->isItemsEquals($nodeList, $attributes)) { | ||
throw new InvalidArgumentException( | ||
'Items are not equal. The number of original elements are: '.$nodeList->length.' but you give '.count( | ||
$attributes | ||
) | ||
); | ||
} | ||
foreach ($nodeList as $index => $element) { | ||
foreach ($attributes[$index] as $attribute => $attributeValue) { | ||
$element->setAttribute($attribute, utf8_encode($attributeValue)); | ||
} | ||
} | ||
} | ||
|
||
public function removeElementsAndItsContent(array $elements): void | ||
{ | ||
foreach ($elements as $element) { | ||
$nodeList = $this->getNodeList($element); | ||
while ($nodeList->length) { | ||
$node = $nodeList->item(0); | ||
$node->parentNode->removeChild($node); | ||
} | ||
} | ||
} | ||
|
||
public function removeElementByDomElement(DOMElement $element): void | ||
{ | ||
$element->parentNode->removeChild($element); | ||
} | ||
|
||
public function removeElementById(string $id): void | ||
{ | ||
$elementToBeDeleted = $this->DOMDocument->getElementById($id); | ||
if ($elementToBeDeleted) { | ||
$elementToBeDeleted->parentNode->removeChild($elementToBeDeleted); | ||
} | ||
} | ||
|
||
public function removeElementsByClass(string $class): void | ||
{ | ||
$xpath = new \DOMXPath($this->DOMDocument); | ||
foreach ($xpath->query("//div[contains(attribute::class, $class)]") as $e) { | ||
$e->parentNode->removeChild($e); | ||
} | ||
} | ||
|
||
public function setAttributes(string $element, array $attributes): void | ||
{ | ||
$nodeList = $this->getNodeList($element); | ||
foreach ($nodeList as $index => $element) { | ||
foreach ($attributes as $attribute => $value) { | ||
$element->setAttribute($attribute, utf8_encode($value)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param $attribute 'style', 'href', 'src' | ||
*/ | ||
public function removeAttribute(string $attribute): void | ||
{ | ||
$xpath = new \DOMXPath($this->DOMDocument); | ||
$nodes = $xpath->query("//*[@$attribute]"); | ||
foreach ($nodes as $node) { | ||
$node->removeAttribute($attribute); | ||
} | ||
} | ||
|
||
public function removeAllAttributes(): void | ||
{ | ||
$xpath = new \DOMXPath($this->DOMDocument); | ||
$nodes = $xpath->query('//@*'); | ||
foreach ($nodes as $node) { | ||
$node->parentNode->removeAttribute($node->nodeName); | ||
} | ||
} | ||
|
||
public function removeAllAttributesExcept(array $attributes): void | ||
{ | ||
$xpath = new \DOMXPath($this->DOMDocument); | ||
$nodes = $xpath->query('//@*'); | ||
foreach ($nodes as $node) { | ||
if (!in_array($node->nodeName, $attributes)) { | ||
$node->parentNode->removeAttribute($node->nodeName); | ||
} | ||
} | ||
} | ||
|
||
public function replaceElement(DOMElement $elementToBeReplaced, DOMElement $newElement): void | ||
{ | ||
$elementToBeReplaced->parentNode->replaceChild($newElement, $elementToBeReplaced); | ||
} | ||
|
||
public function isItemsEquals(DOMNodeList $elements, array $attributesValue): bool | ||
{ | ||
return $elements->length === count($attributesValue); | ||
} | ||
|
||
public function wrapDocument($wrappedElement = 'div', $wrappedClass = 'wrapper'): void | ||
{ | ||
$this->DOMDocument->documentElement->nodeValue = '<'.$wrappedElement.' class="'.$wrappedClass.'"'.'>'.$this->DOMDocument->documentElement->nodeValue.'</'.$wrappedElement.'>'; | ||
} | ||
|
||
public function getTextContent(): string | ||
{ | ||
return $this->DOMDocument->textContent; | ||
} | ||
|
||
/** | ||
* @param string $document | ||
* @throws EmptyDocumentException | ||
*/ | ||
protected function loadDocument(string $document) | ||
{ | ||
if (empty($document)) { | ||
throw new EmptyDocumentException(); | ||
} | ||
$this->DOMDocument->loadHTML($document, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); | ||
} | ||
|
||
/** | ||
* prevent errors of not valid html on load document | ||
*/ | ||
protected function handleInvalidHtml(): void | ||
{ | ||
if (libxml_use_internal_errors(true) === true) { | ||
libxml_clear_errors(); //***IMPORTANT FOR PERFORMANCE*** Clear libxml error buffer to prevent Memory Leak | ||
} | ||
} | ||
|
||
|
||
public function replaceImagesAttributes(string $name, string $storagePath, $class = 'img-responsive article-image'): void | ||
{ | ||
$numberOfImages = count($this->getAttributesValue('img', 'src')); | ||
$attributes = []; | ||
for ($i = 0; $i < $numberOfImages; $i++) { | ||
$attributes[$i]['src'] = $storagePath.$name; | ||
$attributes[$i]['alt'] = $name; | ||
$attributes[$i]['class'] = $class; | ||
} | ||
$this->replaceAttributes('img', $attributes); | ||
} | ||
|
||
public function replaceImagesSourceToAbsolute(string $srcPrefix): void | ||
{ | ||
$nodeList = $this->getNodeList('img'); | ||
foreach ($nodeList as $index => $element) { | ||
$currentAttrSrc = $element->getAttribute('src'); | ||
if (ToolBox::isRelativeUrl($currentAttrSrc)) { | ||
$element->setAttribute('src', $srcPrefix.$currentAttrSrc); | ||
} | ||
} | ||
} | ||
|
||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Panakour\DOM; | ||
|
||
class EmptyDocumentException extends \Exception | ||
{ | ||
|
||
} |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Panagiotis Koursaris | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,2 @@ | ||
[![ci](https://github.com/panakour/phpdom/actions/workflows/ci.yml/badge.svg)](https://github.com/panakour/phpdom/actions/workflows/ci.yml) | ||
![Code Coverage Badge](https://raw.githubusercontent.com/panakour/phpdom/image-data/coverage.svg) |
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,33 @@ | ||
<?php | ||
|
||
namespace Panakour\DOM; | ||
|
||
class ToolBox | ||
{ | ||
public static function getDomain(string $url) | ||
{ | ||
return parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST); | ||
} | ||
|
||
public static function isRelativeUrl(string $url): bool | ||
{ | ||
$urlParts = parse_url($url); | ||
|
||
return !isset($urlParts['host']) || ($urlParts['host'] == ''); | ||
} | ||
|
||
public static function getUrlWithoutPath($url) | ||
{ | ||
$urlParts = parse_url($url); | ||
|
||
return $urlParts['scheme'].'://'.$urlParts['host'].'/'; | ||
} | ||
|
||
public static function getUrlPathComponents($url) | ||
{ | ||
$path = parse_url($url, PHP_URL_PATH); | ||
|
||
return explode("/", trim($path, "/")); | ||
} | ||
|
||
} |
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,33 @@ | ||
{ | ||
"name": "panakour/phpdom", | ||
"description": "Some helpers around PHP DOM native classes", | ||
"keywords": [ | ||
"dom", | ||
"php", | ||
"phpdom" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Panagiots Koursaris", | ||
"email": "panakourweb@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"ext-dom": "*", | ||
"ext-libxml": "*", | ||
"php": ">=8.1" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^10.5.3", | ||
"vimeo/psalm": "^5.18.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Panakour\\DOM\\": "" | ||
}, | ||
"exclude-from-classmap": [ | ||
"/tests/" | ||
] | ||
} | ||
} |
Oops, something went wrong.