mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-22 06:12:41 +00:00
Added editBotInfo & editChat & getVideoAttachmentDetails
This commit is contained in:
@@ -42,6 +42,65 @@ abstract readonly class AbstractModel
|
||||
return new static(...$constructorArgs); // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$reflectionClass = new ReflectionClass($this);
|
||||
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);
|
||||
$result = [];
|
||||
|
||||
foreach ($properties as $property) {
|
||||
if (!$property->isInitialized($this)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$phpPropertyName = $property->getName();
|
||||
$value = $property->getValue($this);
|
||||
|
||||
$jsonKey = self::toSnakeCase($phpPropertyName);
|
||||
|
||||
$result[$jsonKey] = $this->convertValue($value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $input
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function toSnakeCase(string $input): string
|
||||
{
|
||||
return strtolower((string)preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
protected function convertValue(mixed $value): mixed
|
||||
{
|
||||
if ($value instanceof AbstractModel) {
|
||||
return $value->toArray();
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
return array_map([$this, 'convertValue'], $value);
|
||||
}
|
||||
|
||||
if ($value instanceof BackedEnum) {
|
||||
return $value->value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param ReflectionProperty $property
|
||||
@@ -114,63 +173,4 @@ abstract readonly class AbstractModel
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $input
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function toSnakeCase(string $input): string
|
||||
{
|
||||
return strtolower((string)preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$reflectionClass = new ReflectionClass($this);
|
||||
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);
|
||||
$result = [];
|
||||
|
||||
foreach ($properties as $property) {
|
||||
if (!$property->isInitialized($this)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$phpPropertyName = $property->getName();
|
||||
$value = $property->getValue($this);
|
||||
|
||||
$jsonKey = self::toSnakeCase($phpPropertyName);
|
||||
|
||||
$result[$jsonKey] = $this->convertValue($value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function convertValue(mixed $value): mixed
|
||||
{
|
||||
if ($value instanceof AbstractModel) {
|
||||
return $value->toArray();
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
return array_map([$this, 'convertValue'], $value);
|
||||
}
|
||||
|
||||
if ($value instanceof BackedEnum) {
|
||||
return $value->value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user