mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-02 05:59:21 +03:00
This will be better for future use of JSON.stringify() and JSON.parse() For now the difference between the PHP JSON being PascalCase and the JS object properties being camelCase is handled by AbstractModel
106 lines
1.7 KiB
PHP
106 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace RainLoop\Model;
|
|
|
|
class Template implements \JsonSerializable
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $sId;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $sName;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $sBody;
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
private $bPopulateAlways;
|
|
|
|
function __construct(string $sId = '', string $sName = '', string $sBody = '')
|
|
{
|
|
$this->sId = $sId;
|
|
$this->sName = $sName;
|
|
$this->sBody = $sBody;
|
|
$this->bPopulateAlways = false;
|
|
}
|
|
|
|
public function Id() : string
|
|
{
|
|
return $this->sId;
|
|
}
|
|
|
|
public function Name() : string
|
|
{
|
|
return $this->sName;
|
|
}
|
|
|
|
public function Body() : string
|
|
{
|
|
return $this->sBody;
|
|
}
|
|
|
|
public function SetPopulateAlways(bool $bPopulateAlways)
|
|
{
|
|
$this->bPopulateAlways = $bPopulateAlways;
|
|
}
|
|
|
|
public function FromJSON(array $aData, bool $bAjax = false) : bool
|
|
{
|
|
if (isset($aData['ID'], $aData['Name'], $aData['Body']))
|
|
{
|
|
$this->sId = $aData['ID'];
|
|
$this->sName = $aData['Name'];
|
|
$this->sBody = $aData['Body'];
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function ToSimpleJSON() : array
|
|
{
|
|
return array(
|
|
'ID' => $this->Id(),
|
|
'Name' => $this->Name(),
|
|
'Body' => $this->Body()
|
|
);
|
|
}
|
|
|
|
public function jsonSerialize()
|
|
{
|
|
$sBody = $this->Body();
|
|
$bPopulated = true;
|
|
if ($bPopulated && !$this->bPopulateAlways) {
|
|
if (1024 * 5 < \strlen($sBody) || true) {
|
|
$bPopulated = false;
|
|
$sBody = '';
|
|
}
|
|
}
|
|
return array(
|
|
'@Object' => 'Object/Template',
|
|
'id' => $this->Id(),
|
|
'name' => $this->Name(),
|
|
'body' => $sBody,
|
|
'populated' => $bPopulated
|
|
);
|
|
}
|
|
|
|
public function GenerateID() : bool
|
|
{
|
|
return $this->sId = \MailSo\Base\Utils::Md5Rand();
|
|
}
|
|
|
|
public function Validate() : bool
|
|
{
|
|
return 0 < \strlen($this->sBody);
|
|
}
|
|
}
|