Ejemplo n.º 1
0
 /**
  * @param string $dir
  * @param array $options
  * @see AbstractStrategy::generate()
  */
 public function generate(array $options = [])
 {
     $defaults = ['extenstion' => null, 'count' => null, 'prefferedName' => null];
     $options = array_merge($defaults, $options);
     $count = (int) $options['count'];
     $ext = (string) $options['extension'];
     $index = $count + 1;
     $dir = $this->getDir();
     if (!$dir) {
         throw new Exception("`dir` not initialized");
     }
     $dirPath = $this->path($index, $this->deep);
     $filter = new FilenameSafe();
     if ($options['prefferedName']) {
         $fileBasename = $filter->filter($options['prefferedName']);
     } else {
         $fileBasename = $index;
     }
     $idx = 0;
     do {
         $suffix = $idx ? '_' . $idx : '';
         $filename = $fileBasename . $suffix . ($ext ? '.' . $ext : '');
         $filePath = $dir . DIRECTORY_SEPARATOR . $dirPath . $filename;
         $idx++;
     } while (file_exists($filePath));
     return $dirPath . $filename;
 }
Ejemplo n.º 2
0
 /**
  * @param string $pattern
  * @return string
  */
 private static function normalizePattern($pattern)
 {
     $filter = new FilenameSafe();
     $result = [];
     $patternComponents = preg_split('|[\\/]+|isu', $pattern);
     foreach ($patternComponents as $component) {
         if (!in_array($component, self::$notAllowedParts)) {
             if ($component) {
                 $filtered = $filter->filter($component);
                 $result[] = $filtered;
             }
         }
     }
     return implode('/', $result);
 }