Скачиваний:
66
Добавлен:
15.03.2015
Размер:
2.75 Кб
Скачать
<?php
class Controller {
    private $error;
    private $result;

    function __construct() {
        $this->error = false;
        $this->result = false;

    }

    function processData() {
        $this->userRequest();
        if ($this->error)
            View::displayError($this->error);
        else
            if ($this->result)
                View::displayResults($this->result);
            else
                View::displayDefault();
    }

    function userRequest() {
        // данные отправлены
        if (isset($_POST['send'])) {
            $this->validate();
            if (!$this->error) {
                // основные вычисления
                $model = new Model();
                $model->calculate($_POST['passw']);
                $result = $model->getData();
                // проверка на ошибки в самой модели
                if (!is_array($result))
                    $this->error = $result;
                else
                    $this->result = $result;
            }
        }
    }

    function validate() {
        if (empty($_POST['passw']))
            $this->error = 'Не введено слово!';
        else
            if (strlen(strval($_POST['passw'])) < 3)
                $this->error = 'Слишком слабый пароль!';
    }

}

class View {
    static function displayDefault() {
        echo "<form method='POST' action=''>";
        echo "<p>Введите пароль:  ";
        echo "<input type='text' name='passw' value=''>  ";
        echo "<input type='submit' name='send' value='Отправить'>";
        echo "</form>";

    }

    static function displayError($error) {
        echo "<p><b>Ошибка:</b> {$error}";
        View::displayDefault();
    }

    static function displayResults($results) {
        echo "<p><b>Результаты:</b>";
        echo "<p>Ваш пароль <b>".$results[0]."</b> по защищенности <i>".$results[1]."</i>";
        echo "<p><a href='".$_SERVER['REQUEST_URI']."'>Проанализировать ещё пароль</a>";
    }
}

class Model {
    private $data;

    function __construct() {
        $this->data = false;

    }

    function calculate($passw) {
        $this->data[] = $passw;
        $len = strlen($passw);
        if ($len == 3)
            $this->data[] = 'Слишком слабый пароль';
        else
            if (($len > 3) && ($len < 6))
                $this->data[] = 'Достаточно защищенный пароль';
            else
                $this->data[] = 'Хорошо защищенный пароль';
    }

    function getData() {
        if ($this->data)
            return $this->data;
        else
            return 'Анализ не произведен!';
    }
}

$controller = new Controller();
$controller->processData();
?>
Соседние файлы в папке phplab4