You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.6 KiB
42 lines
1.6 KiB
3 years ago
|
<?php
|
||
|
/**
|
||
|
* @license MIT
|
||
|
* Copyright 2017 Dustin Wilson, J. King, et al.
|
||
|
* See LICENSE and AUTHORS files for details
|
||
|
*/
|
||
|
|
||
|
declare(strict_types=1);
|
||
3 years ago
|
namespace MensBeam\HTML\DOM\Inner;
|
||
3 years ago
|
|
||
3 years ago
|
/**
|
||
|
* Class which uses reflection to gain access to protected constructors and
|
||
|
* properties. We are well aware of the negative conotations around using
|
||
|
* Reflection. The specification itself is written in a manner that expects
|
||
|
* C++-style friend classes which PHP lacks, so it first comes out of necessity.
|
||
|
* Any other use of it comes down to speed optimizations (such as gaining access
|
||
|
* to Node::innerNode).
|
||
|
*/
|
||
3 years ago
|
class Reflection {
|
||
3 years ago
|
public static function createFromProtectedConstructor(string $className, ...$arguments): mixed {
|
||
3 years ago
|
$reflector = new \ReflectionClass($className);
|
||
3 years ago
|
$result = $reflector->newInstanceWithoutConstructor();
|
||
|
$constructor = new \ReflectionMethod($result, '__construct');
|
||
|
$constructor->setAccessible(true);
|
||
|
$constructor->invoke($result, ...$arguments);
|
||
|
return $result;
|
||
|
}
|
||
3 years ago
|
|
||
|
public static function getProtectedProperty(mixed $instance, string $propertyName): mixed {
|
||
|
$property = new \ReflectionProperty($instance, $propertyName);
|
||
|
$property->setAccessible(true);
|
||
|
return $property->getValue($instance);
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
public static function setProtectedProperties(mixed $instance, array $properties): void {
|
||
|
foreach ($properties as $propertyName => $value) {
|
||
|
$property = new \ReflectionProperty($instance, $propertyName);
|
||
|
$property->setAccessible(true);
|
||
|
$property->setValue($instance, $value);
|
||
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|