Webmozart\PathUtil\Path::hasExtension PHP Method

hasExtension() public static method

Returns whether the path has an extension.
Since: 1.1 Added method.
Since: 2.0 Method now fails if $path or $extensions have invalid types.
public static hasExtension ( string $path, string | array | null $extensions = null, boolean $ignoreCase = false ) : boolean
$path string The path string.
$extensions string | array | null If null or not provided, checks if an extension exists, otherwise checks for the specified extension or array of extensions (with or without leading dot).
$ignoreCase boolean Whether to ignore case-sensitivity (requires mbstring extension for correct multi-byte character handling in the extension).
return boolean Returns `true` if the path has an (or the specified) extension and `false` otherwise.
    public static function hasExtension($path, $extensions = null, $ignoreCase = false)
    {
        if ('' === $path) {
            return false;
        }
        $extensions = is_object($extensions) ? array($extensions) : (array) $extensions;
        Assert::allString($extensions, 'The extensions must be strings. Got: %s');
        $actualExtension = self::getExtension($path, $ignoreCase);
        // Only check if path has any extension
        if (empty($extensions)) {
            return '' !== $actualExtension;
        }
        foreach ($extensions as $key => $extension) {
            if ($ignoreCase) {
                $extension = self::toLower($extension);
            }
            // remove leading '.' in extensions array
            $extensions[$key] = ltrim($extension, '.');
        }
        return in_array($actualExtension, $extensions);
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Creates a new runner.
  *
  * @param string|null $binDir The path to Composer's "bin-dir".
  */
 public function __construct($binDir = null)
 {
     $phpFinder = new PhpExecutableFinder();
     if (!($php = $phpFinder->find())) {
         throw new RuntimeException('The "php" command could not be found.');
     }
     $puliFinder = new ExecutableFinder();
     // Search:
     // 1. in the current working directory
     // 2. in Composer's "bin-dir"
     // 3. in the system path
     $searchPath = array_merge(array(getcwd()), (array) $binDir);
     // Search "puli.phar" in the PATH and the current directory
     if (!($puli = $puliFinder->find('puli.phar', null, $searchPath))) {
         // Search "puli" in the PATH and Composer's "bin-dir"
         if (!($puli = $puliFinder->find('puli', null, $searchPath))) {
             throw new RuntimeException('The "puli"/"puli.phar" command could not be found.');
         }
     }
     if (Path::hasExtension($puli, '.bat', true)) {
         $this->puli = $puli;
     } else {
         $this->puli = $php . ' ' . $puli;
     }
 }
All Usage Examples Of Webmozart\PathUtil\Path::hasExtension