Return to Snippet

Revision: 68154
at December 4, 2014 01:12 by rk


Initial Code
<?php
namespace Repositories;

/**
 * ExcursionRepository
 */
class ExcursionRepository extends BaseRepository
{

	protected $basePath;
	protected $excursionsPath;

	public function __construct($wwwDir, $excursionsPath)
	{
		$this->basePath = $wwwDir;
		$this->excursionsPath = $excursionsPath;
	}

	/**
	 * get array of files (scandir)
	 * filter out directiries (array_filter)
	 * prepend a full path string (array_map)
	 * filter out photos
	 *
	 * @return array
	 */
	public function getAllExcursionPhotos()
	{
		$excursionsFullPath = $this->basePath . $this->excursionsPath;

		if (!is_dir($excursionsFullPath)) {
			mkdir($excursionsFullPath, 0777, TRUE);
		}

		$photoPaths = scandir($excursionsFullPath);
		$photoPaths = array_filter($photoPaths, $this->filterOutDirectories);
		$photoPaths = array_map($this->prependExcursionPath, $photoPaths);
		$photoPaths = array_filter($photoPaths, array('self', 'filterPhotos'));

		return $photoPaths;
	}

	/**
	 * Callback
	 *
	 * @param $itemPath
	 * @return bool
	 */
	public function filterOutDirectories($itemPath)
	{
		$excursionsFullPath = $this->basePath . $this->excursionsPath;
		return !is_dir($excursionsFullPath . $itemPath);
	}

	/**
	 * Callback
	 *
	 * @param $itemPath
	 * @return string
	 */
	public function prependExcursionPath($itemPath)
	{
		return $this->excursionsPath . $itemPath;
	}

	/**
	 * Callback
	 *
	 * @param $itemPath
	 * @return string
	 */
	public function filterPhotos($itemPath)
	{
		$fullPath = $this->basePath . $itemPath;
		return parent::isPhoto($fullPath);
	}
}

Initial URL


Initial Description
excursion repo

Initial Title
ExcursionRepository

Initial Tags


Initial Language
PHP