Overview

Namespaces

  • PHP
  • PhpOptions
    • Types

Classes

  • AType
  • CharType
  • DatetimeType
  • DateType
  • DirectoryType
  • EmailType
  • EnumType
  • FileType
  • InifileType
  • IntegerType
  • RealType
  • SeriesType
  • StringType
  • TimeType
  • Types
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * PhpOptions
  5:  * @link https://github.com/masicek/PhpOptions
  6:  * @author Viktor Mašíček <viktor@masicek.net>
  7:  * @license "New" BSD License
  8:  */
  9: 
 10: namespace PhpOptions\Types;
 11: 
 12: require_once __DIR__ . '/AType.php';
 13: 
 14: /**
 15:  * File type
 16:  *
 17:  * @author Viktor Mašíček <viktor@masicek.net>
 18:  */
 19: class FileType extends AType
 20: {
 21: 
 22:     /**
 23:      * Base path for check input path
 24:      *
 25:      * @var string
 26:      */
 27:     private $base = NULL;
 28: 
 29: 
 30:     /**
 31:      * Set object
 32:      * 'base' => base path of input value
 33:      *
 34:      * @param array $setting Array of setting of object
 35:      */
 36:     public function __construct($settings = array())
 37:     {
 38:         parent::__construct($settings);
 39:         if (isset($settings[0]))
 40:         {
 41:             $this->base = $settings[0];
 42:         }
 43:     }
 44: 
 45: 
 46:     /**
 47:      * Check type of value.
 48:      *
 49:      * @param mixed $value Checked value
 50:      *
 51:      * @return bool
 52:      */
 53:     public function check($value)
 54:     {
 55:         $isDir = FALSE;
 56:         if (is_file($value))
 57:         {
 58:             $isDir = TRUE;
 59:         }
 60:         elseif ($this->base && is_file($this->base . DIRECTORY_SEPARATOR . $value))
 61:         {
 62:             $isDir = TRUE;
 63:         }
 64:         return $isDir;
 65:     }
 66: 
 67: 
 68:     /**
 69:      * Return modified value
 70:      *
 71:      * @param mixed $value Filtered value
 72:      *
 73:      * @return mixed
 74:      */
 75:     protected function useFilter($value)
 76:     {
 77:         // base is set and value not full path
 78:         if ($this->base && !preg_match('/^([a-zA-Z]:\\\|\/)/', $value))
 79:         {
 80:             $value = $this->make($this->base, $value);
 81:         }
 82:         $value = $this->make($value);
 83: 
 84:         return $value;
 85:     }
 86: 
 87: 
 88:     /**
 89:      * Make path from list of arguments.
 90:      *
 91:      * @return string
 92:      */
 93:     private function make()
 94:     {
 95:         $pathParts = func_get_args();
 96: 
 97:         $ds = DIRECTORY_SEPARATOR;
 98:         $path = implode($ds, $pathParts);
 99: 
100:         // correct separator
101:         $path = str_replace('/', $ds, $path);
102:         $path = str_replace('\\', $ds, $path);
103: 
104:         // replace "/./" and "//"
105:         while (strpos($path, $ds . '.' . $ds) !== FALSE || strpos($path, $ds . $ds) !== FALSE)
106:         {
107:             $path = str_replace($ds . $ds, $ds, $path);
108:             $path = str_replace($ds . '.' . $ds, $ds, $path);
109:         }
110: 
111:         return $path;
112:     }
113: 
114: 
115: }
116: 
PhpOptions API documentation generated by ApiGen.
Generated using the TokenReflection library.