1
0

AbstractModel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php declare(strict_types=1);
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. */
  11. namespace PrivateBin\Model;
  12. use PrivateBin\Configuration;
  13. use PrivateBin\Data\AbstractData;
  14. use PrivateBin\Exception\TranslatedException;
  15. /**
  16. * AbstractModel
  17. *
  18. * Abstract model for PrivateBin objects.
  19. */
  20. abstract class AbstractModel
  21. {
  22. /**
  23. * show the same error message if the data is invalid
  24. *
  25. * @const string
  26. */
  27. const INVALID_DATA_ERROR = 'Invalid data.';
  28. /**
  29. * show the same error message if the document ID already exists
  30. *
  31. * @const string
  32. */
  33. const COLLISION_ERROR = 'You are unlucky. Try again.';
  34. /**
  35. * Instance ID.
  36. *
  37. * @access protected
  38. * @var string
  39. */
  40. protected $_id = '';
  41. /**
  42. * Instance data.
  43. *
  44. * @access protected
  45. * @var array
  46. */
  47. protected $_data = ['meta' => []];
  48. /**
  49. * Configuration.
  50. *
  51. * @access protected
  52. * @var Configuration
  53. */
  54. protected $_conf;
  55. /**
  56. * Data storage.
  57. *
  58. * @access protected
  59. * @var AbstractData
  60. */
  61. protected $_store;
  62. /**
  63. * Instance constructor.
  64. *
  65. * @access public
  66. * @param Configuration $configuration
  67. * @param AbstractData $storage
  68. */
  69. public function __construct(Configuration $configuration, AbstractData $storage)
  70. {
  71. $this->_conf = $configuration;
  72. $this->_store = $storage;
  73. }
  74. /**
  75. * Get ID.
  76. *
  77. * @access public
  78. * @return string
  79. */
  80. public function getId()
  81. {
  82. return $this->_id;
  83. }
  84. /**
  85. * Set ID.
  86. *
  87. * @access public
  88. * @param string $id
  89. * @throws TranslatedException
  90. */
  91. public function setId($id)
  92. {
  93. if (!self::isValidId($id)) {
  94. throw new TranslatedException('Invalid document ID.', 60);
  95. }
  96. $this->_id = $id;
  97. }
  98. /**
  99. * Set data and recalculate ID.
  100. *
  101. * @access public
  102. * @param array $data
  103. * @throws TranslatedException
  104. */
  105. public function setData(array &$data)
  106. {
  107. $this->_sanitize($data);
  108. $this->_validate($data);
  109. $this->_data = $data;
  110. // calculate a 64 bit checksum to avoid collisions
  111. $this->setId(hash('fnv1a64', $data['ct']));
  112. }
  113. /**
  114. * Get instance data.
  115. *
  116. * @access public
  117. * @return array
  118. */
  119. public function get()
  120. {
  121. return $this->_data;
  122. }
  123. /**
  124. * Store the instance's data.
  125. *
  126. * @access public
  127. * @throws TranslatedException
  128. */
  129. abstract public function store();
  130. /**
  131. * Test if current instance exists in store.
  132. *
  133. * @access public
  134. * @return bool
  135. */
  136. abstract public function exists();
  137. /**
  138. * Validate ID.
  139. *
  140. * @access public
  141. * @static
  142. * @param string $id
  143. * @return bool
  144. */
  145. public static function isValidId($id)
  146. {
  147. return (bool) preg_match('#\A[a-f0-9]{16}\z#', (string) $id);
  148. }
  149. /**
  150. * Sanitizes data to conform with current configuration.
  151. *
  152. * @access protected
  153. * @param array $data
  154. */
  155. abstract protected function _sanitize(array &$data);
  156. /**
  157. * Validate data.
  158. *
  159. * @access protected
  160. * @param array $data
  161. * @throws TranslatedException
  162. */
  163. protected function _validate(array &$data)
  164. {
  165. }
  166. }