2022-12-30 21:54:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2020-09-22 11:48:12 +02:00
|
|
|
|
|
|
|
namespace danog\MadelineProto;
|
|
|
|
|
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionProperty;
|
|
|
|
|
|
|
|
abstract class SettingsAbstract
|
|
|
|
{
|
2020-09-26 17:11:41 +02:00
|
|
|
/**
|
|
|
|
* Whether this setting was changed.
|
|
|
|
*
|
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
protected bool $changed = true;
|
2020-09-22 11:48:12 +02:00
|
|
|
/**
|
|
|
|
* Merge legacy settings array.
|
|
|
|
*
|
|
|
|
* @param array $settings Settings array
|
2020-10-07 19:31:52 +02:00
|
|
|
* @internal
|
2020-09-22 11:48:12 +02:00
|
|
|
*/
|
2020-09-26 21:57:06 +02:00
|
|
|
public function mergeArray(array $settings): void
|
|
|
|
{
|
|
|
|
}
|
2020-09-22 11:48:12 +02:00
|
|
|
|
2023-01-22 13:25:42 +01:00
|
|
|
public function __sleep()
|
|
|
|
{
|
|
|
|
$result = [];
|
|
|
|
foreach ((new ReflectionClass($this))->getProperties(ReflectionProperty::IS_PROTECTED|ReflectionProperty::IS_PUBLIC) as $property) {
|
|
|
|
$result []= $property->getName();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2020-09-22 11:48:12 +02:00
|
|
|
/**
|
|
|
|
* Merge with other settings instance.
|
|
|
|
*
|
2020-10-07 19:31:52 +02:00
|
|
|
* @internal
|
2020-09-22 11:48:12 +02:00
|
|
|
*/
|
|
|
|
public function merge(self $other): void
|
|
|
|
{
|
|
|
|
$class = new ReflectionClass($other);
|
|
|
|
$defaults = $class->getDefaultProperties();
|
|
|
|
foreach ($class->getProperties(ReflectionProperty::IS_PROTECTED|ReflectionProperty::IS_PUBLIC) as $property) {
|
|
|
|
$name = $property->getName();
|
2020-09-26 21:57:06 +02:00
|
|
|
if ($name === 'changed') {
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-26 17:11:41 +02:00
|
|
|
$uc = \ucfirst($name);
|
2020-09-22 11:48:12 +02:00
|
|
|
if (isset($other->{$name})
|
|
|
|
&& (
|
|
|
|
!isset($defaults[$name])
|
2020-10-07 16:58:48 +02:00
|
|
|
|| (
|
|
|
|
$other->{$name} !== $defaults[$name] // Isn't equal to the default value
|
|
|
|
|| $other->{$name} !== $this->{$name} // Is equal, but current value is not the default one
|
|
|
|
)
|
2020-09-22 11:48:12 +02:00
|
|
|
)
|
2021-12-09 13:25:14 +01:00
|
|
|
&& (
|
|
|
|
!isset($this->{$name})
|
|
|
|
|| $other->{$name} !== $this->{$name}
|
|
|
|
)
|
2020-09-22 11:48:12 +02:00
|
|
|
) {
|
2020-09-26 17:11:41 +02:00
|
|
|
$this->{"set$uc"}($other->{$name});
|
|
|
|
$this->changed = true;
|
2020-09-22 11:48:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Convert array of legacy array property names to new camel case names.
|
|
|
|
*
|
|
|
|
* @param array $properties Properties
|
|
|
|
*/
|
|
|
|
protected static function toCamel(array $properties): array
|
|
|
|
{
|
|
|
|
$result = [];
|
|
|
|
foreach ($properties as $prop) {
|
|
|
|
$result['set'.\ucfirst(Tools::toCamelCase($prop))] = $prop;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2020-09-26 17:11:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get whether this setting was changed, also applies changes.
|
|
|
|
*
|
2021-05-09 22:33:59 +02:00
|
|
|
* @internal
|
2020-09-26 17:11:41 +02:00
|
|
|
*/
|
|
|
|
public function hasChanged(): bool
|
|
|
|
{
|
|
|
|
return $this->changed;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Apply changes.
|
|
|
|
*
|
2020-10-07 19:31:52 +02:00
|
|
|
* @internal
|
2020-09-26 17:11:41 +02:00
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public function applyChanges(): self
|
|
|
|
{
|
|
|
|
$this->changed = false;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-09-22 11:48:12 +02:00
|
|
|
}
|