libresign/lib/Helper/MagicGetterSetterTrait.php
Vitor Mattos 45cd3c374d
chore: Add SPDX header
Signed-off-by: Vitor Mattos <vitor@php.rio>
2024-05-14 12:32:04 -03:00

26 lines
859 B
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Helper;
trait MagicGetterSetterTrait {
public function __call($name, $arguments) {
if (!preg_match('/^(?<type>get|set)(?<property>.+)/', $name, $matches)) {
throw new \LogicException(sprintf('Cannot set non existing property %s->%s = %s.', \get_class($this), $name, var_export($arguments, true)));
}
$property = lcfirst($matches['property']);
if (!property_exists($this, $property)) {
throw new \LogicException(sprintf('Cannot set non existing property %s->%s = %s.', \get_class($this), $name, var_export($arguments, true)));
}
if ($matches['type'] === 'get') {
return $this->$property;
}
$this->$property = $arguments[0] ?? null;
return $this;
}
}