好友
阅读权限20
听众
最后登录1970-1-1
|
class ZyGuessService
{
private $dict_repo;
private $surname_config; //复姓姓氏数组
public function __construct(DictRepository $dict_repo)
{
$this->dict_repo = $dict_repo;
$this->surname_config = config('surname');
}
/**
* 天格计算
* 计算规则:
* 单姓氏:取名字第一个字符计算笔画数 + 1
* 复姓氏:取名字前两个字符计算笔画数
* @Param $str 名字
* @Return int
* @AuThor joniding
* @date 2020-03-12
*/
public function DayGrid($str)
{
$two_name = mb_substr($str,0,2); //取名字前两个检测是否为复姓氏
$first_sur = mb_substr($str,0,1);
$first_info = $this->dict_repo->getFirst(['chinese' => $first_sur]);
$nums = $first_info['nums'];
//检测是否为复姓氏
if ($this->checkIsSurName($two_name)){
$second_sur = mb_substr($str,1,1);
$second_info = $this->dict_repo->getFirst(['chinese' => $second_sur]);
!empty($second_info) && $nums += $second_info['nums'];
}else{
!empty($first_info) && $nums = $first_info['nums']+1;
}
\Log::info($nums);
return $nums;
}
/**
* 地格计算
* 计算规则:
* 单姓氏:后两个字笔画总数
* 复姓氏:后两个字的笔画总数。
* 单字名:后一字笔画数 + 1
* @param $str
* @return int
*/
public function LandGrid($str)
{
$nums = 0;
$two_name = mb_substr($str,0,2); //取前两个
//检测是否为复姓氏
if ($this->checkIsSurName($two_name)){
\Log::info('进入复姓计算');
//计算第三个字的笔画数
$three_sur = mb_substr($str,2,1);
\Log::info($three_sur);
$three_info = $this->dict_repo->getFirst(['chinese' => $three_sur]);
!empty($three_info) && $nums += $three_info['nums'];
//检测用户是否为单字名
switch (mb_strlen($str)){
case 3:
$nums += 1;
break;
case 4:
$four_sur = mb_substr($str,3,1);
\Log::info($four_sur);
$four_info = $this->dict_repo->getFirst(['chinese' => $four_sur]);
$nums += $four_info['nums'];
break;
}
}else{
\Log::info('进入单姓计算');
//计算名字的第二个字
$second_sur = mb_substr($str,1,1);
\Log::info($second_sur);
$second_info = $this->dict_repo->getFirst(['chinese' => $second_sur]);
!empty($second_info) && $nums += $second_info['nums'];
switch (mb_strlen($str)){
//两字名则笔画数+1
case 2:
$nums += 1;
break;
//三字名则笔画数相加
case 3:
$three_sur = mb_substr($str,2,1);
\Log::info($three_sur);
$three_info = $this->dict_repo->getFirst(['chinese' => $three_sur]);
!empty($three_info) && $nums += $three_info['nums'];
break;
default:
break;
}
}
\Log::info($nums);
return $nums;
}
/**
* 人格计算
* 计算规则:
* 单姓氏:第一个字和第二个字的笔画数
* 复姓氏:第二个字和第三个字的笔画数
* @param $str
* @return int
*/
public function PersonGrid($str)
{
$nums = 0;
$two_name = mb_substr($str,0,2);
$second_sur = mb_substr($str,1,1);
$second_info = $this->dict_repo->getFirst(['chinese' => $second_sur]);
!empty($second_info) && $nums += $second_info['nums'];
if ($this->checkIsSurName($two_name)){
$three_sur = mb_substr($str,2,1);
$three_info = $this->dict_repo->getFirst(['chinese' => $three_sur]);
!empty($three_info) && $nums += $three_info['nums'];
}else{
$first_sur = mb_substr($str,0,1);
$first_info = $this->dict_repo->getFirst(['chinese' => $first_sur]);
!empty($first_info) && $nums += $first_info['nums'];
}
return $nums;
}
/**
* 总格计算:
* 计算规则:姓和名的总笔画之数
* @param $str
* @return int
*/
public function TotalGrid($str)
{
$nums = 0;
$length = mb_strlen($str);
for ($i = 0;$i < $length; $i++){
$sur = mb_substr($str,$i,1);
$info = $this->dict_repo->getFirst(['chinese' => $sur]);
!empty($info) && $nums += $info['nums'];
}
return $nums;
}
/**
* 外格计算:
* 计算规则:总格数减人格数
* @param $str
* @return int
*/
public function OuterGrid($str)
{
$nums = 0;
$total_nums = $this->TotalGrid($str);//总格
$person_nums = $this->PersonGrid($str); //人格
if ($total_nums < $person_nums){
\Log::error('笔画计算错误:总格数小于人格数');
return $nums;
}
$nums = $total_nums - $person_nums;
return $nums;
}
/**
* 检测姓名是否为复姓
* @param $str
* @return bool
*/
private function checkIsSurName($str)
{
if (empty($str)){
return false;
}
if (mb_strlen($str) > 2 || mb_strlen($str) <= 0){
return false;
}
if (!in_array($str,$this->surname_config)){
return false;
}
return true;
}
}
|
|