2016-05-11 01:10:06 +05:30
|
|
|
<?php
|
|
|
|
namespace api\traits;
|
|
|
|
|
|
|
|
use common\models\Account;
|
|
|
|
|
|
|
|
trait AccountFinder {
|
|
|
|
|
|
|
|
private $account;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public abstract function getLogin(): string;
|
2016-05-11 01:10:06 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getAccount(): ?Account {
|
2016-05-11 01:10:06 +05:30
|
|
|
if ($this->account === null) {
|
2017-09-19 22:36:16 +05:30
|
|
|
$this->account = Account::findOne([$this->getLoginAttribute() => $this->getLogin()]);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $this->account;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getLoginAttribute(): string {
|
2016-05-11 01:10:06 +05:30
|
|
|
return strpos($this->getLogin(), '@') ? 'email' : 'username';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|