AbstractModel.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Exception;
  13. use PrivateBin\Configuration;
  14. use PrivateBin\Data\AbstractData;
  15. /**
  16. * AbstractModel
  17. *
  18. * Abstract model for PrivateBin objects.
  19. */
  20. abstract class AbstractModel
  21. {
  22. /**
  23. * Instance ID.
  24. *
  25. * @access protected
  26. * @var string
  27. */
  28. protected $_id = '';
  29. /**
  30. * Instance data.
  31. *
  32. * @access protected
  33. * @var array
  34. */
  35. protected $_data = array('meta' => array());
  36. /**
  37. * Configuration.
  38. *
  39. * @access protected
  40. * @var Configuration
  41. */
  42. protected $_conf;
  43. /**
  44. * Data storage.
  45. *
  46. * @access protected
  47. * @var AbstractData
  48. */
  49. protected $_store;
  50. /**
  51. * Instance constructor.
  52. *
  53. * @access public
  54. * @param Configuration $configuration
  55. * @param AbstractData $storage
  56. */
  57. public function __construct(Configuration $configuration, AbstractData $storage)
  58. {
  59. $this->_conf = $configuration;
  60. $this->_store = $storage;
  61. }
  62. /**
  63. * Get ID.
  64. *
  65. * @access public
  66. * @return string
  67. */
  68. public function getId()
  69. {
  70. return $this->_id;
  71. }
  72. /**
  73. * Set ID.
  74. *
  75. * @access public
  76. * @param string $id
  77. * @throws Exception
  78. */
  79. public function setId($id)
  80. {
  81. if (!self::isValidId($id)) {
  82. throw new Exception('Invalid paste ID.', 60);
  83. }
  84. $this->_id = $id;
  85. }
  86. /**
  87. * Set data and recalculate ID.
  88. *
  89. * @access public
  90. * @param array $data
  91. * @throws Exception
  92. */
  93. public function setData(array $data)
  94. {
  95. $data = $this->_sanitize($data);
  96. $this->_validate($data);
  97. $this->_data = $data;
  98. // calculate a 64 bit checksum to avoid collisions
  99. $this->setId(hash('fnv1a64', $data['ct']));
  100. }
  101. /**
  102. * Get instance data.
  103. *
  104. * @access public
  105. * @return array
  106. */
  107. public function get()
  108. {
  109. return $this->_data;
  110. }
  111. /**
  112. * Store the instance's data.
  113. *
  114. * @access public
  115. * @throws Exception
  116. */
  117. abstract public function store();
  118. /**
  119. * Delete the current instance.
  120. *
  121. * @access public
  122. * @throws Exception
  123. */
  124. abstract public function delete();
  125. /**
  126. * Test if current instance exists in store.
  127. *
  128. * @access public
  129. * @return bool
  130. */
  131. abstract public function exists();
  132. /**
  133. * Validate ID.
  134. *
  135. * @access public
  136. * @static
  137. * @param string $id
  138. * @return bool
  139. */
  140. public static function isValidId($id)
  141. {
  142. return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
  143. }
  144. /**
  145. * Sanitizes data to conform with current configuration.
  146. *
  147. * @access protected
  148. * @param array $data
  149. * @return array
  150. */
  151. abstract protected function _sanitize(array $data);
  152. /**
  153. * Validate data.
  154. *
  155. * @access protected
  156. * @param array $data
  157. * @throws Exception
  158. */
  159. protected function _validate(array $data)
  160. {
  161. }
  162. }