4
0

demo-functions.php 840 B

12345678910111213141516171819202122232425
  1. <?php
  2. /** @noinspection PhpUndefinedFieldInspection */
  3. trait DemoFunctions
  4. {
  5. public function demoData($file = null)
  6. {
  7. if (!$file) {
  8. $this->setResponse(422, 'Demo file was not supplied');
  9. return false;
  10. }
  11. $path = dirname(__DIR__, 1) . DIRECTORY_SEPARATOR . 'demo_data' . DIRECTORY_SEPARATOR . $file;
  12. if (file_exists($path)) {
  13. $data = file_get_contents($path);
  14. $path = (strpos($file, '/') !== false) ? explode('/', $file)[0] . '/' : '';
  15. $data = $this->userDefinedIdReplacementLink($data, ['data/cache/' => 'api/demo_data/' . $path . 'images/']);
  16. $data = json_decode($data, true);
  17. $this->setResponse(200, 'Demo data for file: ' . $file, $data['response']['data']);
  18. return $data['response']['data'];
  19. } else {
  20. $this->setResponse(404, 'Demo data was not found for file: ' . $file);
  21. return false;
  22. }
  23. }
  24. }