PHP code example of aklump / dom-testing-selectors
1. Go to this page and download the library: Download aklump/dom-testing-selectors library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace Vendor\DomTestingSelectors\Selectors;
final class CypressSelector extends AbstractSelector {
public function getAttributeName(): string {
return 'data-cy';
}
}
// When using the class attribute--e.g., "<div class="foo bar"/>"--you must
// merge with any existing value. The current value has to be passed as the
// second argument to __invoke() and __getAttributeValue.
$selector = new \AKlump\DomTestingSelectors\Selector\ClassSelector();
$attribute_markup = $selector('my_target_element', 'foo bar');
// $attribute_markup === 'class="foo bar t-my-target-element"'
$selector = new \AKlump\DomTestingSelectors\Selector\ClassSelector();
$attribute_value = $selector->setName('my_target_element')
->getAttributeValue('foo bar');
// $attribute_value === 'foo bar t-my-target-element'
$element = '<div></div>';
$handler = new \AKlump\DomTestingSelectors\Handler\StringHandler();
$selector = new \AKlump\DomTestingSelectors\Selector\DataTestSelector();
if ($handler->canHandle($element)) {
$handler->setTestingSelectorOnElement($element, $selector->setName('foobar'));
}
// $element === '<div data-test="foobar"></div>'
class MyArrayHandler implements \AKlump\DomTestingSelectors\Handler\HandlerInterface {
public function canHandle($element): bool {
return is_array($element);
}
public function setTestingSelectorOnElement(&$element, \AKlump\DomTestingSelectors\Selector\ElementSelectorInterface $selector): void {
$element['attributes'][$selector->getAttributeName()] = $selector->getAttributeValue();
}
}
class MyFactory extends \AKlump\DomTestingSelectors\Factory\AbstractHandlerFactory {
public function __construct() {
$this->addHandler(new \AKlump\DomTestingSelectors\Handler\StringHandler());
$this->addHandler(new MyArrayHandler());
}
}
$factory = new MyFactory();
$selector = new \AKlump\DomTestingSelectors\Selector\DataTestSelector();
$selector->setName('foobar');
$element1 = '<div></div>';
$element2 = ['tag' => 'div'];
try {
$factory->getHandler($element1)->setTestingSelectorOnElement($element1, $selector);
// $element1 === '<div data-test="foobar"></div>'
$factory->getHandler($element2)->setTestingSelectorOnElement($element2, $selector);
// $element2 === ['tag'=>'div','attributes'=>['data-test'=>'foobar']]
}
catch (\AKlump\DomTestingSelectors\Exception\NoHandlerFoundException $exception) {
// No handler found
}
class MySafeFactory extends \AKlump\DomTestingSelectors\Factory\AbstractHandlerFactory {
public function __construct() {
$this->addHandler(new \AKlump\DomTestingSelectors\Handler\StringHandler());
$this->addHandler(new \AKlump\DomTestingSelectors\Handler\PassThroughHandler());
}
}
$factory = new MySafeFactory();
$selector = new \AKlump\DomTestingSelectors\Selector\DataTestSelector();
$selector->setName('foobar');
$element1 = '<div></div>';
$element2 = 'lorem ipsum dolar';
$factory->getHandler($element1)->setTestingSelectorOnElement($element1, $selector);
// $element1 === '<div data-test="foobar"></div>'
$factory->getHandler($element2)->setTestingSelectorOnElement($element2, $selector);
// $element2 === 'lorem ipsum dolar'
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.