<?php
namespace install\library;
/**
 * Requirements class
 * @author Jérôme Brilland
 * @version 2.0
 * @copyright (c) 2019-2021, Jérôme Brilland
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
 */
class Requirements {

    private $errors = false;

    public function checkPHPVersion() {
        if ( !version_compare( PHP_VERSION, '7.3.0', '>' ) ) {
            $this->errors = true;
            return false;
        }
        return true;
    }

    public function checkPDO() {
        if ( !extension_loaded( 'pdo' ) ) {
            $this->errors = true;
            return false;
        }
        return true;
    }

    public function checkMySQL() {
        return ( in_array( 'mysql', \PDO::getAvailableDrivers() ) );
    }

    public function checkPostgreSQL() {
        return ( in_array( 'pgsql', \PDO::getAvailableDrivers() ) );
    }

    public function checkMail() {
        return function_exists( 'mail' );
    }

    public function checkGD() {
        return extension_loaded( 'gd' );
    }

    public function checkImageMagick() {
        return extension_loaded( 'imagick' );
    }

    public function checkMultibyteString() {
        if ( !extension_loaded( 'mbstring' ) ) {
            $this->errors = true;
            return false;
        }
        return true;
    }

    public function checkConfigFileWritable() {
        if ( !is_writable( CONFIG_DIR . 'config.php' ) ) {
            $this->errors = true;
            return false;
        }
        return true;
    }

    public function hasErrors() {
        if ( !$this->checkMySQL() && !$this->checkPostgreSQL() ) {
            $this->errors = true;
        }
        return $this->errors;
    }
}
