SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Model.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2021 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13. namespace Sprout\Helpers;
  14.  
  15. use karmabunny\kb\CachedHelperTrait;
  16. use karmabunny\kb\Collection;
  17. use karmabunny\kb\RulesValidatorTrait;
  18. use karmabunny\kb\Uuid;
  19. use karmabunny\kb\Validates;
  20. use karmabunny\pdb\Pdb;
  21. use karmabunny\pdb\PdbModelInterface;
  22. use karmabunny\pdb\PdbModelTrait;
  23.  
  24.  
  25. /**
  26.  * Base model class
  27.  *
  28.  * @property int $id
  29.  * @property string $uid
  30.  * @property bool $active
  31.  * @property string $date_added
  32.  * @property string $date_modified
  33.  * @property string $date_deleted
  34.  *
  35.  * @package dashboard\Base
  36.  */
  37. abstract class Model extends Collection implements PdbModelInterface, Validates
  38. {
  39. use RulesValidatorTrait;
  40. use CachedHelperTrait;
  41. use PdbModelTrait;
  42.  
  43. /** @var int */
  44. public $id = 0;
  45.  
  46.  
  47. /** @inheritdoc */
  48. public static function getConnection(): Pdb
  49. {
  50. return \Sprout\Helpers\Pdb::getInstance();
  51. }
  52.  
  53.  
  54. /** @inheritdoc */
  55. public function save($validate = true): bool
  56. {
  57. if ($validate) $this->validate();
  58.  
  59. $now = Pdb::now();
  60. $pdb = static::getConnection();
  61. $table = static::getTableName();
  62. $data = iterator_to_array($this);
  63.  
  64. if ($this->id > 0) {
  65. if (property_exists($this, 'date_modified')) $data['date_modified'] = $now;
  66.  
  67. if (property_exists($this, 'uid')) {
  68. if ($data['uid'] === Uuid::nil()) $data['uid'] = $this->getUid();
  69. $this->uid = $data['uid'];
  70. }
  71.  
  72. $pdb->update($table, $data, ['id' => $this->id]);
  73. }
  74. else {
  75. if (property_exists($this, 'date_added')) $data['date_added'] = $now;
  76. if (property_exists($this, 'date_modified')) $data['date_modified'] = $now;
  77.  
  78. $this->id = $pdb->insert($table, $data);
  79.  
  80. if (property_exists($this, 'uid')) {
  81. $this->uid = $this->getUid();
  82.  
  83. $pdb->update(
  84. $table,
  85. ['uid' => $this->uid],
  86. ['id' => $this->id]
  87. );
  88. }
  89. }
  90.  
  91. return (bool) $this->id;
  92. }
  93.  
  94.  
  95. /** @inheritdoc */
  96. public function rules(): array
  97. {
  98. return [];
  99. }
  100.  
  101.  
  102. /**
  103.   * Generate an appropriate UUID.
  104.   *
  105.   * Beware - new records are created with a UUIDv4 while the save() method
  106.   * generates a UUIDv5. Theoretically this shouldn't be externally apparent
  107.   * due to the wrapping transaction.
  108.   *
  109.   * @return string
  110.   * @throws Exception
  111.   */
  112. protected function getUid()
  113. {
  114. // Start out with a v4.
  115. if ($this->id == 0) return Uuid::uuid4();
  116.  
  117. // Upgrade it later with a v5.
  118. $pdb = static::getConnection();
  119. return $pdb->generateUid(static::getTableName(), $this->id);
  120. }
  121. }
  122.