Posted By


rk on 12/04/14

Tagged


Statistics


Viewed 25 times
Favorited by 0 user(s)

ExcursionRepository


/ Published in: PHP
Save to your folder(s)

excursion repo


Copy this code and paste it in your HTML
  1. <?php
  2. namespace Repositories;
  3.  
  4. /**
  5.  * ExcursionRepository
  6.  */
  7. class ExcursionRepository extends BaseRepository
  8. {
  9.  
  10. protected $basePath;
  11. protected $excursionsPath;
  12.  
  13. public function __construct($wwwDir, $excursionsPath)
  14. {
  15. $this->basePath = $wwwDir;
  16. $this->excursionsPath = $excursionsPath;
  17. }
  18.  
  19. /**
  20. * get array of files (scandir)
  21. * filter out directiries (array_filter)
  22. * prepend a full path string (array_map)
  23. * filter out photos
  24. *
  25. * @return array
  26. */
  27. public function getAllExcursionPhotos()
  28. {
  29. $excursionsFullPath = $this->basePath . $this->excursionsPath;
  30.  
  31. if (!is_dir($excursionsFullPath)) {
  32. mkdir($excursionsFullPath, 0777, TRUE);
  33. }
  34.  
  35. $photoPaths = scandir($excursionsFullPath);
  36. $photoPaths = array_filter($photoPaths, $this->filterOutDirectories);
  37. $photoPaths = array_map($this->prependExcursionPath, $photoPaths);
  38. $photoPaths = array_filter($photoPaths, array('self', 'filterPhotos'));
  39.  
  40. return $photoPaths;
  41. }
  42.  
  43. /**
  44. * Callback
  45. *
  46. * @param $itemPath
  47. * @return bool
  48. */
  49. public function filterOutDirectories($itemPath)
  50. {
  51. $excursionsFullPath = $this->basePath . $this->excursionsPath;
  52. return !is_dir($excursionsFullPath . $itemPath);
  53. }
  54.  
  55. /**
  56. * Callback
  57. *
  58. * @param $itemPath
  59. * @return string
  60. */
  61. public function prependExcursionPath($itemPath)
  62. {
  63. return $this->excursionsPath . $itemPath;
  64. }
  65.  
  66. /**
  67. * Callback
  68. *
  69. * @param $itemPath
  70. * @return string
  71. */
  72. public function filterPhotos($itemPath)
  73. {
  74. $fullPath = $this->basePath . $itemPath;
  75. return parent::isPhoto($fullPath);
  76. }
  77. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.