2016-05-11 01:10:06 +05:30
|
|
|
<?php
|
|
|
|
namespace api\traits;
|
|
|
|
|
|
|
|
use common\models\Account;
|
|
|
|
|
|
|
|
trait AccountFinder {
|
|
|
|
|
|
|
|
private $account;
|
|
|
|
|
|
|
|
public abstract function getLogin();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
public function getAccount() {
|
|
|
|
if ($this->account === null) {
|
2016-05-30 05:14:17 +05:30
|
|
|
$className = $this->getAccountClassName();
|
|
|
|
$this->account = $className::findOne([$this->getLoginAttribute() => $this->getLogin()]);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $this->account;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLoginAttribute() {
|
|
|
|
return strpos($this->getLogin(), '@') ? 'email' : 'username';
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
/**
|
|
|
|
* @return Account|string
|
|
|
|
*/
|
|
|
|
protected function getAccountClassName() {
|
|
|
|
return Account::class;
|
|
|
|
}
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|