1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-12 05:18:21 +01:00
MadelineProto/src/SettingsAbstract.php

103 lines
2.6 KiB
PHP
Raw Normal View History

2022-12-30 21:54:44 +01:00
<?php
declare(strict_types=1);
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;
/**
* Merge legacy settings array.
*
* @param array $settings Settings array
* @internal
*/
2020-09-26 21:57:06 +02:00
public function mergeArray(array $settings): void
{
}
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;
}
/**
* Merge with other settings instance.
*
* @internal
*/
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);
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
)
)
2021-12-09 13:25:14 +01:00
&& (
!isset($this->{$name})
|| $other->{$name} !== $this->{$name}
)
) {
2020-09-26 17:11:41 +02:00
$this->{"set$uc"}($other->{$name});
$this->changed = true;
}
}
}
/**
* 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.
*
* @internal
2020-09-26 17:11:41 +02:00
*/
public function hasChanged(): bool
{
return $this->changed;
}
/**
* Apply changes.
*
* @internal
2020-09-26 17:11:41 +02:00
* @return static
*/
public function applyChanges(): self
{
$this->changed = false;
return $this;
}
}