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:  * Directory type
 16:  *
 17:  * @author Viktor Mašíček <viktor@masicek.net>
 18:  */
 19: class DirectoryType 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:      * Flag for making directory if it is not exist
 32:      *
 33:      * @var string
 34:      */
 35:     private $makeDir = FALSE;
 36: 
 37: 
 38:     /**
 39:      * Set object
 40:      * 'base' => base path of input value
 41:      *
 42:      * @param array $setting Array of setting of object
 43:      */
 44:     public function __construct($settings = array())
 45:     {
 46:         parent::__construct($settings);
 47: 
 48:         if ($this->settingsHasFlag('makeDir', $settings))
 49:         {
 50:             $this->makeDir = TRUE;
 51:         }
 52: 
 53:         if (isset($settings[0]))
 54:         {
 55:             $this->base = $settings[0];
 56:         }
 57:     }
 58: 
 59: 
 60:     /**
 61:      * Check type of value.
 62:      *
 63:      * @param mixed $value Checked value
 64:      *
 65:      * @return bool
 66:      */
 67:     public function check($value)
 68:     {
 69:         $isDir = FALSE;
 70:         if (is_dir($value))
 71:         {
 72:             $isDir = TRUE;
 73:         }
 74:         elseif ($this->base && is_dir($this->make($this->base, $value)))
 75:         {
 76:             $isDir = TRUE;
 77:         }
 78: 
 79:         if (!$isDir && $this->makeDir)
 80:         {
 81:             $value = $this->useFilter($value);
 82:             if ($this->isFullPath($value))
 83:             {
 84:                 $isDir = mkdir($value, 0700, TRUE);
 85:             }
 86:         }
 87: 
 88:         return $isDir;
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Return modified value
 94:      *
 95:      * @param mixed $value Filtered value
 96:      *
 97:      * @return mixed
 98:      */
 99:     protected function useFilter($value)
100:     {
101:         // base is set and value not full path
102:         if ($this->base && !$this->isFullPath($value))
103:         {
104:             $value = $this->make($this->base, $value);
105:         }
106:         $value = $this->make($value , '/');
107: 
108:         return $value;
109:     }
110: 
111: 
112:     /**
113:      * Detect if the path is full path
114:      *
115:      * @param string $path Detected path
116:      *
117:      * @return bool
118:      */
119:     private function isFullPath($path)
120:     {
121:         return (bool)preg_match('/^([a-zA-Z]:\\\|\/)/', $path);
122:     }
123: 
124: 
125:     /**
126:      * Make path from list of arguments.
127:      *
128:      * @return string
129:      */
130:     private function make()
131:     {
132:         $pathParts = func_get_args();
133: 
134:         $ds = DIRECTORY_SEPARATOR;
135:         $path = implode($ds, $pathParts);
136: 
137:         // correct separator
138:         $path = str_replace('/', $ds, $path);
139:         $path = str_replace('\\', $ds, $path);
140: 
141:         // replace "/./" and "//"
142:         while (strpos($path, $ds . '.' . $ds) !== FALSE || strpos($path, $ds . $ds) !== FALSE)
143:         {
144:             $path = str_replace($ds . $ds, $ds, $path);
145:             $path = str_replace($ds . '.' . $ds, $ds, $path);
146:         }
147: 
148:         return $path;
149:     }
150: 
151: 
152: }
153: 
PhpOptions API documentation generated by ApiGen.
Generated using the TokenReflection library.