<?php
namespace myresto\models;

use Yii;
use yii\base\Model;

use common\models\User;

/**
 * Login form
 */
class UpdateAccountForm extends Model {
    public $username;
    public $email;

    public function rules() {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['username', 'string', 'min' => 2, 'max' => 255],

            ['email', 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
        ];
    }

    public function updateAccount() {
        if ($this->validate()) {
            $user = User::findIdentity(Yii::$app->user->identity->id);
            if ($user) {
                $user->username = $this->username;
                $user->email = $this->email;
                if ($user->save())
                    return true;
                else
                    return false;
            } else {
                return false;
            }
        }
    }

}
