Skip to content

Instantly share code, notes, and snippets.

View bolechen's full-sized avatar
💭
I may be slow to respond.

Bole Chen bolechen

💭
I may be slow to respond.
View GitHub Profile
@bolechen
bolechen / isWechatMacro.php
Last active January 20, 2021 02:18
Add isWecht for Laravel Request helper
<?php
// 判断是否微信内访问.
Request::macro('isWechat', function (): bool {
if (app()->environment(['local', 'testing'])) {
return true;
}
/** @var Request $request */
$request = $this;
@bolechen
bolechen / StrMask.php
Last active January 20, 2021 02:16
Add mask for Laravel Str helper
<?php
// abcde -> a*e
Str::macro('mask', function ($subject, $maxlength = 5, $mask_symbol = '*'): string {
$subject = mb_substr($subject, 0, $maxlength);
$length = mb_strlen($subject, 'utf-8');
if ($length < 2) {
return $subject;
}
@bolechen
bolechen / toRawSqlMacro.php
Last active December 24, 2020 06:21
easy way to debut sql like this: `User::where(xxx)->toRawSql();`
<?php
private function bootSupportMacros(): self
{
\Illuminate\Database\Query\Builder::macro('toRawSql', function (): string {
/** @var \Illuminate\Database\Query\Builder $builder */
$builder = $this;
return array_reduce($builder->getBindings(), static function ($sql, $binding) {
return preg_replace('/\?/', is_numeric($binding) ? $binding : "'".$binding."'", $sql, 1);
}, $builder->toSql());
@bolechen
bolechen / iPhone12.php
Last active October 27, 2020 08:04
男人袜十周年 iPhone12 抽奖算法
<?php
/**
* 男人袜十周年活动.
* 抽奖规则说明:11.25 收盘时的上证指数 × 深证成指 × 10000 = 12 位数(指数以活动页面公布链接数字为准);
* 将此 12 位数的数字倒序排列后,再除以本次活动结束时的参与人次(每个抽奖号为一个人次),得到的余数加 1 即为获奖号码。
*
* @param float $sh 上证指盘收盘价
* @param float $sz 深证成指收盘价
* @param int $totalCount 参与人次(每个抽奖号为一个人次)
@bolechen
bolechen / ExampleTest.php
Created December 14, 2019 15:06 — forked from jakzal/ExampleTest.php
Set global variables with PHPUnit test method annotations
<?php
declare(strict_types=1);
namespace Zalas\Tests;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
@bolechen
bolechen / HasPivotTrait.php
Created July 19, 2019 03:00
Check if belongsToMany relation exists
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Model;
trait HasPivotTrait
{
/**
* 检查多对多关系是否存在.
*
@bolechen
bolechen / HasUuid.php
Last active October 12, 2020 10:58
Laravel Model Use Uuid
<?php
namespace App\Traits;
use Illuminate\Support\Str;
trait HasUuid
{
/**
* Get the route key for the model.
<?php
function is_weixin() {
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
return true;
}
return false;
}