getTable()->emptyToNull($data); if (empty($data['alias']) && !empty($data['firstName']) && !empty($data['lastName'])) { $data['alias'] = $this->_prepareAlias($data); } if (empty($data['password'])) { unset($data['password']); } else { $data['salt'] = substr(md5(time()), 0, 8); $data['password'] = $this->_encryptPass($data['password'], $data['salt']); } $key = empty($data['id']) ? [] : ['id' => $data['id']]; if (!empty($data['email']) && !$this->getTable('User')->isUnique(['email' => $data['email'], 'bought' => 'y'], $key) ) { throw new Exception('Email is not unique', Exception::EMAIL_NOT_UNIQUE); } if (!empty($data['login']) && !$this->getTable('User')->isUnique(['login' => $data['login'], 'bought' => 'y'], $key) ) { throw new Exception('Login is not unique', Exception::LOGIN_NOT_UNIQUE); } return $this; } private function _encryptPass($password, $salt) { return md5($salt . $password); } protected function saveDependencies(array $data) { $key = ['userId' => $data['id']]; foreach (['addresses'] as $name) { $method = 'save' . ucfirst($name); if (array_key_exists($name, $data) && method_exists($this, $method)) { $this->$method($data[$name], $key); } } return $this; } public function injectDependencies(array &$data) { $key = ['userId' => $data['id']]; $data[AddressEntity::TYPE_BILLING] = $this->getBillingAddress($key); $data[AddressEntity::TYPE_SHIPPING] = $this->getMailingAddress($key); $billing = array_intersect_key($data[AddressEntity::TYPE_BILLING], $data[AddressEntity::TYPE_SHIPPING]); $data[AddressEntity::TYPE_SHIPPING]['sameAs'] = $billing == $data[AddressEntity::TYPE_MAILING] ? AddressEntity::TYPE_BILLING : ''; return $this; } public function savePrize(array $data, array $key) { $this->getTable('UserPrize')->saveBy($data, $key); return $this; } public function getPrize(array $key) { return $this->getTable('UserPrize')->searchBy($key) ?: []; } protected function _prepareAlias(array $data) { $fullName = $data['firstName'] . ' ' . $data['lastName']; $key = empty($data['id']) ? [] : ['id' => $data['id']]; do { $alias = Qs_String::aliasize($fullName); $fullName = $data['firstName'] . ' ' . $data['lastName'] . '-' . rand(1000,9999); } while (!$this->getTable('User')->isUnique(['alias' => $alias], $key)); return $alias; } }