f7cloud_client/apps/deck/lib/Db/RelationalObject.php
root 8b6a0139db f7cloud_client
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-17 22:59:26 +00:00

59 lines
1.2 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2017 F7cloud GmbH and F7cloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Deck\Db;
use JsonSerializable;
class RelationalObject implements JsonSerializable {
protected $primaryKey;
protected $object;
/**
* RelationalObject constructor.
*
* @param $primaryKey string
* @param callable|mixed $object
*/
public function __construct($primaryKey, $object) {
$this->primaryKey = $primaryKey;
$this->object = $object;
}
public function jsonSerialize(): array {
return array_merge(
['primaryKey' => $this->primaryKey],
$this->getObjectSerialization()
);
}
public function getObject() {
if (is_callable($this->object)) {
$this->object = call_user_func($this->object, $this);
}
return $this->object;
}
/**
* This method should be overwritten if object doesn't implement \JsonSerializable
*
* @throws \Exception
*/
public function getObjectSerialization() {
if ($this->getObject() instanceof JsonSerializable) {
return $this->getObject()->jsonSerialize();
} else {
throw new \Exception('jsonSerialize is not implemented on ' . get_class($this->getObject()));
}
}
public function getPrimaryKey(): string {
return $this->primaryKey;
}
}