1: <?php
2:
3: 4: 5: 6: 7: 8:
9:
10: namespace PhpOptions\Types;
11:
12: require_once __DIR__ . '/AType.php';
13:
14: 15: 16: 17: 18:
19: class DirectoryType extends AType
20: {
21:
22: 23: 24: 25: 26:
27: private $base = NULL;
28:
29:
30: 31: 32: 33: 34:
35: private $makeDir = FALSE;
36:
37:
38: 39: 40: 41: 42: 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: 62: 63: 64: 65: 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: 94: 95: 96: 97: 98:
99: protected function useFilter($value)
100: {
101:
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: 114: 115: 116: 117: 118:
119: private function isFullPath($path)
120: {
121: return (bool)preg_match('/^([a-zA-Z]:\\\|\/)/', $path);
122: }
123:
124:
125: 126: 127: 128: 129:
130: private function make()
131: {
132: $pathParts = func_get_args();
133:
134: $ds = DIRECTORY_SEPARATOR;
135: $path = implode($ds, $pathParts);
136:
137:
138: $path = str_replace('/', $ds, $path);
139: $path = str_replace('\\', $ds, $path);
140:
141:
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: