<commit_before><?php

/**
 * This file is part of the Krystal Framework
 * 
 * Copyright (c) No Global State Lab
 * 
 * For the full copyright and license information, please view
 * the license file that was distributed with this source code.
 */

namespace Krystal\Filesystem;

use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use DirectoryIterator;
use RuntimeException;
use UnexpectedValueException;

class FileManager implements FileManagerInterface
{
    /**
     * Turns raw bytes into human-readable format
     * 
     * @param int $bytes
     * @return string
     */
    public static function humanSize($bytes)
    {
        // Make sure we can't divide by zero
        if ($bytes == 0) {
            return '0 B';
        }

        $value = floor(log($bytes, 1024));

        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
        $unit = $units[$value]; // Chosen unit

        return sprintf('%.02F', $bytes / pow(1024, $value)) * 1 . ' ' . $unit;
    }

    /**
     * Returns a directory name from a path
     * 
     * @param string $path
     * @return string
     */
    public static function getBaseName($path)
    {
        return pathinfo($path, \PATHINFO_BASENAME);
    }

    /**
     * Checks whether file has extension
     * 
     * @param string $baseName
     * @param array $extensions
     * @return boolean
     */
    public static function hasExtension($baseName, array $extensions)
    {
        // Lowercase names
        $baseName = strtolower($baseName);

        $extensions = array_map(function($key){
            return strtolower($key);
        }, $extensions);

        return in_array(self::getExtension($baseName), $extensions);
    }

    /**
     * Returns a directory name from a path
     * 
     * @param string $path
     * @return string
     */
    public static function getExtension($path)
    {
        return pathinfo($path, \PATHINFO_EXTENSION);
    }

    /**
     * Returns a directory name from a path
     * 
     * @param string $path
     * @return string
     */
    public static function getDirName($path)
    {
        return pathinfo($path, \PATHINFO_DIRNAME);
    }

    /**
     * Returns a file name from a path
     * 
     * @param string $path
     * @return string
     */
    public static function getFileName($path)
    {
        return pathinfo($path, \PATHINFO_FILENAME);
    }

    /**
     * Fetches mime type from a file
     * 
     * @param string $file
     * @return string
     */
    public static function getMimeType($file)
    {
        $mimeType = new MimeTypeGuesser();
        $extension = self::getExtension($file);

        return $mimeType->getTypeByExtension($file);
    }

    /**<commit_msg>Method sorting<commit_after><?php

/**
 * This file is part of the Krystal Framework
 * 
 * Copyright (c) No Global State Lab
 * 
 * For the full copyright and license information, please view
 * the license file that was distributed with this source code.
 */

namespace Krystal\Filesystem;

use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use DirectoryIterator;
use RuntimeException;
use UnexpectedValueException;

class FileManager implements FileManagerInterface
{
    /**
     * Returns a directory name from a path
     * 
     * @param string $path
     * @return string
     */
    public static function getBaseName($path)
    {
        return pathinfo($path, \PATHINFO_BASENAME);
    }

    /**
     * Returns a directory name from a path
     * 
     * @param string $path
     * @return string
     */
    public static function getExtension($path)
    {
        return pathinfo($path, \PATHINFO_EXTENSION);
    }

    /**
     * Returns a directory name from a path
     * 
     * @param string $path
     * @return string
     */
    public static function getDirName($path)
    {
        return pathinfo($path, \PATHINFO_DIRNAME);
    }

    /**
     * Returns a file name from a path
     * 
     * @param string $path
     * @return string
     */
    public static function getFileName($path)
    {
        return pathinfo($path, \PATHINFO_FILENAME);
    }

    /**
     * Turns raw bytes into human-readable format
     * 
     * @param int $bytes
     * @return string
     */
    public static function humanSize($bytes)
    {
        // Make sure we can't divide by zero
        if ($bytes == 0) {
            return '0 B';
        }

        $value = floor(log($bytes, 1024));

        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
        $unit = $units[$value]; // Chosen unit

        return sprintf('%.02F', $bytes / pow(1024, $value)) * 1 . ' ' . $unit;
    }

    /**
     * Checks whether file has extension
     * 
     * @param string $baseName
     * @param array $extensions
     * @return boolean
     */
    public static function hasExtension($baseName, array $extensions)
    {
        // Lowercase names
        $baseName = strtolower($baseName);

        $extensions = array_map(function($key){
            return strtolower($key);
        }, $extensions);

        return in_array(self::getExtension($baseName), $extensions);
    }

    /**
     * Fetches mime type from a file
     * 
     * @param string $file
     * @return string
     */
    public static function getMimeType($file)
    {
        $mimeType = new MimeTypeGuesser();
        $extension = self::getExtension($file);

        return $mimeType->getTypeByExtension($file);
    }

    /**
{'fraction_non_alphanumeric': 0.11564501372369625, 'fraction_numerical': 0.004757548032936871, 'mean_word_length': 2.1853146853146854, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2218727', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1547, 'n_words': 463}