<?php

namespace Opentalent\OtCore\Utility;

class FileUtility
{
    /**
     * If $recursive is true, recursively delete a directory and all its content.
     * Else, does a simple rmdir($dir)
     *
     * @param $dir
     * @param bool $recursive
     */
    public static function rmdir($dir, $recursive=false) {
        if (!$recursive) {
            rmdir($dir);
        } else {
            if (is_dir($dir)) {
                $objects = scandir($dir);
                foreach ($objects as $object) {
                    if ($object != "." && $object != "..") {
                        if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir."/".$object))
                            self::rmdir($dir. DIRECTORY_SEPARATOR. $object, true);
                        else
                            unlink($dir. DIRECTORY_SEPARATOR. $object);
                    }
                }
                rmdir($dir);
            }
        }
    }

}
