diff --git a/README.md b/README.md index 5e147b1..69c29dc 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ [![Latest Stable Version](https://poser.pugx.org/sanjabteam/baloot/v/stable)](https://packagist.org/packages/sanjabteam/baloot) [![Total Downloads](https://poser.pugx.org/sanjabteam/baloot/downloads)](https://packagist.org/packages/sanjabteam/baloot) -[![Build Status](https://travis-ci.org/sanjabteam/baloot.svg?branch=master)](https://travis-ci.com/sanjabteam/baloot) +[![Build Status](https://travis-ci.com/sanjabteam/baloot.svg?branch=master)](https://travis-ci.com/sanjabteam/baloot) [![Code Style](https://github.styleci.io/repos/214197383/shield)](https://github.styleci.io/repos/214197383) [![Code Style](https://codecov.io/gh/sanjabteam/baloot/branch/master/graph/badge.svg?sanitize=true)](https://codecov.io/gh/sanjabteam/baloot) [![License](https://poser.pugx.org/sanjabteam/baloot/license)](https://packagist.org/packages/sanjabteam/baloot) @@ -31,10 +31,9 @@ forkو pull request امکانات: * ذخیره و بازیابی تاریخ شمسی در الوکوئنت -* دسترسی به ای پی آی سایت آپارات مستقیما از مدل +* شرط گذاری روی کوئری با تاریخ شمسی * میدلویر برای تعمیر حروف ک و ی عربی و اعداد فارسی و عربی * تابع تهیه اسلاگ فارسی -* امکانات اضافه شده به فیکر از جمله لینک ویدیو آپارات * ولیدیتور شماره همراه و شماره تماس ثابت * دیتابیس شهر ها و استان های ایران به همراه روت بایندینگ ها * پیدا کردن بانک از روی شماره کارت @@ -43,8 +42,11 @@ forkو pull request - [کانفیگ](#%da%a9%d8%a7%d9%86%d9%81%db%8c%da%af) - [ذخیره و بازیابی تاریخ شمسی](#%d8%b0%d8%ae%db%8c%d8%b1%d9%87-%d9%88-%d8%a8%d8%a7%d8%b2%db%8c%d8%a7%d8%a8%db%8c-%d8%aa%d8%a7%d8%b1%db%8c%d8%ae-%d8%b4%d9%85%d8%b3%db%8c) - [استفاده از اتریبیوت های دلخواه](#%d8%a7%d8%b3%d8%aa%d9%81%d8%a7%d8%af%d9%87-%d8%a7%d8%b2-%d8%a7%d8%aa%d8%b1%db%8c%d8%a8%db%8c%d9%88%d8%aa-%d9%87%d8%a7%db%8c-%d8%af%d9%84%d8%ae%d9%88%d8%a7%d9%87) -- [مشخصات ویدیو آپارات](#%d9%85%d8%b4%d8%ae%d8%b5%d8%a7%d8%aa-%d9%88%db%8c%d8%af%db%8c%d9%88-%d8%a2%d9%be%d8%a7%d8%b1%d8%a7%d8%aa) - - [نکته بسیار مهم](#%d9%86%da%a9%d8%aa%d9%87-%d8%a8%d8%b3%db%8c%d8%a7%d8%b1-%d9%85%d9%87%d9%85) +- [کوئری بیلدر تاریخ شمسی](#%da%a9%d9%88%d8%a6%d8%b1%db%8c-%d8%a8%db%8c%d9%84%d8%af%d8%b1-%d8%aa%d8%a7%d8%b1%db%8c%d8%ae-%d8%b4%d9%85%d8%b3%db%8c) + - [whereJalali](#wherejalali) + - [whereDateJalali](#wheredatejalali) + - [whereInMonthJalali](#whereinmonthjalali) + - [whereInYearJalali](#whereinyearjalali) - [میدلویر](#%d9%85%db%8c%d8%af%d9%84%d9%88%db%8c%d8%b1) - [ولیدیشن](#%d9%88%d9%84%db%8c%d8%af%db%8c%d8%b4%d9%86) - [فیکر](#%d9%81%db%8c%da%a9%d8%b1) @@ -95,13 +97,6 @@ config/baloot.php قرار بدید تا جداولش ایجاد نشه. -`aparat`: -اگه نمیخاین از قابلیت آپارات استفاده کنین مقدار این قسمت رو برابر - -`false` - -قرار بدید تا جداولش ایجاد نشه. - ## ذخیره و بازیابی تاریخ شمسی این پکیج این امکان رو به شما میده تا به راحتی تاریخ های موجود در مدل لاراول رو به راحتی به کلاس [ورتا](https://github.com/hekmatinasser/verta) تبدیل کنید. برای شروع @@ -206,85 +201,64 @@ $user->birth_date_fa; // Hekmatinasser\Verta\Verta -## مشخصات ویدیو آپارات -این پکیج این امکان رو به شما میده تا به راحتی مشخصات یه فیلم رو از [ای پی آی آپارات](https://aparat.com/api) از طریق آدرس فیلم دریافت کنین. -برای شروع - -trait - -
+## کوئری بیلدر تاریخ شمسی -`Baloot\EloquentHelper` - -
- -رو به مدلی که میخاین اضافه کنین. +این دستورات کمک میکنن بر روی ستون های از نوع +date/datetime +شرط با تاریخ شمسی بذارین. +### whereJalali +شرط با یک تاریخ و زمان به خصوص شمسی
```php -use Baloot\EloquentHelper; - -class Post extends Model -{ - use EloquentHelper; // trait -} +User::whereJalali('created_at', '1399/01/15 14:00:00')->get(); +// یا +User::whereJalali('created_at', Verta::createJalali(1399,01,15, 14, 0, 0))->get(); +// شرط با عملگر +User::whereJalali('created_at', '>', '1399/01/15 14:00:00')->get(); ```
-بعد فقط کافیه که آدرس فیلم رو درون یه فیلد ذخیره کنین. بعد با پسوند - -
- -`_aparat` - -
- -به مشخصاتش دسترسی پیدا کنین. +### whereDateJalali +شرط با یک تاریخ به خصوص شمسی
```php -$post = Post::first(); -$post->video = "https://www.aparat.com/v/O4qSP"; -dd($post->video_aparat); // Baloot\Models\AparatVideo -dd($post->video_aparat->title); // ایستگاه جوانمرد راستگو -dd($post->video_aparat->username); // ding.dong -dd($post->video_aparat->sender_name); //دینگ دانگ +User::whereDateJalali('created_at', '1399/01/15')->get(); +// یا +User::whereDateJalali('created_at', Verta::createJalaliDate(1399,01,15))->get(); +// شرط با عملگر +User::whereDateJalali('created_at', '>', '1399/01/15')->get(); ```
-اگه میخاین مستقیم و بدون مدل به مشخصات دسترسی داشته باشین از این تابع استفاده کنین. - +### whereInMonthJalali +شرط یک ماه خاص شمسی در یک سال
```php -aparat_info(["https://www.aparat.com/v/O4qSP"]); +User::whereInMonthJalali('created_at', 3)->get(); // فقط کاربران ایجاد شده در خرداد ماه سال جاری + +User::whereInMonthJalali('created_at', 3, 1397)->get(); // فقط کاربران ایجاد شده در خرداد ماه سال 1397 ```
-مشخصات کامل: [AparatVideo](./src/Models/AparatVideo.php) - - -### نکته بسیار مهم -آپارات اگه طی یه زمان از حدی بیشتر بهش درخواست بفرسین یه ارور - -429 Too Many Requests - -بهتون تحویل میده و حسابی دردسر درست میکنه براتون برای همین من مشخصات فیلم ها داخل دیتابیس ذخیره میکنم بنابراین مشخصات فیلم تا زمانی که خودتون نخواید تغییری نمیکنه. یه سری چیز ها مثل تعداد بازدید و تعداد لایک خیلی سریع تغییر میکنن اما به نظرم ارزش اینو ندارن که عاقبتتون 429 بشه. -برای بروز کردن مشخصات فیلم کافیه که از این روش استفاده کنین. +### whereInYearJalali +شرط یک سال خاص شمسی
```php -$post->video_aparat->visit_cnt; // 26550 -$post->video_aparat->reload(); -$post->video_aparat->visit_cnt; // 26812 +User::whereInYearJalali('created_at')->get(); // فقط کاربران ایجاد شده در سال جاری + +User::whereInYearJalali('created_at', 1397)->get(); //فقط کاربران ایجاد شده در سال 1397 ```
@@ -423,32 +397,6 @@ $faker->iranPhone -aparatVideo: - -آدرس یه فیلم در آپارات به فراهم میکنه. - - -
- -```php -$faker->aparatVideo -``` - -
- -اگه آرایه ای از فیلم ها میخاید. - - -
- -```php -$faker->aparatVideos($faker->numberBetween(1, 3)) -``` - -
- -پارامتر تعداد فیلم هایی که لازم دارین رو ازتون دریافت میکنه. - ## استان ها و شهرها برای شروع در diff --git a/config/config.php b/config/config.php index 6510ec2..8211ffd 100644 --- a/config/config.php +++ b/config/config.php @@ -7,11 +7,4 @@ |-------------------------------------------------------------------------- */ 'geo' => true, - - /* - |-------------------------------------------------------------------------- - | فعال سازی قابلیت ارتباط با ای پی آی آپارات - |-------------------------------------------------------------------------- - */ - 'aparat' => true, ]; diff --git a/database/migrations/2014_10_11_000002_create_aparat_videos_table.php b/database/migrations/2014_10_11_000002_create_aparat_videos_table.php deleted file mode 100755 index 4cc8a78..0000000 --- a/database/migrations/2014_10_11_000002_create_aparat_videos_table.php +++ /dev/null @@ -1,56 +0,0 @@ -bigIncrements('id'); - $table->string('title'); - $table->string('username'); - $table->unsignedBigInteger('userid'); - $table->unsignedBigInteger('visit_cnt')->default(0); - $table->string('uid')->index()->unique(); - $table->string('process'); - $table->string('sender_name'); - $table->string('big_poster'); - $table->string('small_poster'); - $table->string('profilePhoto'); - $table->unsignedBigInteger('duration'); - $table->string('sdate'); - $table->string('frame'); - $table->string('official'); - $table->text('tags'); - $table->string('description')->nullable(); - $table->unsignedBigInteger('cat_id'); - $table->string('cat_name'); - $table->boolean('autoplay'); - $table->boolean('is_360d'); - $table->string('has_comment'); - $table->string('has_comment_txt'); - $table->unsignedBigInteger('size'); - $table->boolean('can_download'); - $table->unsignedBigInteger('like_cnt'); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('aparat_videos'); - } -} diff --git a/src/BalootFakerProvider.php b/src/BalootFakerProvider.php index 58c1875..52031d9 100755 --- a/src/BalootFakerProvider.php +++ b/src/BalootFakerProvider.php @@ -3,7 +3,6 @@ namespace Baloot; use Illuminate\Support\Arr; -use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\File; class BalootFakerProvider extends \Faker\Provider\Base @@ -52,61 +51,6 @@ public function iranPhone() return '0'.rand(11, 20).rand(1000000, 9999999); } - public function aparatVideo() - { - $videos = Cache::remember('sanjab_baloot_aparat_videos', now()->addHour(), function () { - $curl = curl_init(); - curl_setopt_array($curl, [ - CURLOPT_URL => 'https://www.aparat.com/etc/api/categoryVideos/cat/7/perpage/50', - CURLOPT_RETURNTRANSFER => true, - CURLOPT_TIMEOUT => 10, - ]); - $responseBody = curl_exec($curl); - if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200 && is_string($responseBody)) { - $responseBody = json_decode($responseBody, true); - if (is_array($responseBody)) { - $responseBody = $responseBody['categoryvideos']; - if (is_array($responseBody)) { - curl_close($curl); - - return array_map(function ($videoDetail) { - return $videoDetail['uid']; - }, $responseBody); - } - } - } - }); - if (is_array($videos)) { - return 'https://www.aparat.com/v/'.Arr::random($videos); - } - // @codeCoverageIgnoreStart - return 'https://www.aparat.com/v/'.Arr::random([ - 'IAN6z', 'xrAb8', 'w7NMS', '0fFhg', 'uCgQd', - 'hK5fF', 'arsHC', '43aZ8', 'syI7N', 'XaN3o', - 'YJpM1', 'TSAz1', 'sQBq4', 'Y7AZF', 'dNn3M', - 'uR7DI', 'TZ63C', '8T3hA', 'NdqEn', 'xCFnE', - 'mL21e', 'ZkGp8', 'MV9RW', 'jicTW', 'Ikan1', - 'Ylj9x', '3Qmhd', 'jWqud', '6VeK8', 'fic92', - 'fdQXx', 'UQ2jS', 'RrctN', 'EutTQ', 'evc7o', - 'CugP3', '1T08s', 'eTFxk', 'UF8xV', 'Qn8CF', - 'xtKbg', 'hTWBg', 'wWBU7', 'zt90l', 'fjyRd', - 'VPvu3', 'iaCV3', '4r9IK', 'm1gxT', 'XonZx', - 'rUC8l', '6yoBO', 'aoE2p', 'slKDV', '2Dun4', - 'L4J1F', 'dwWTy', 'a6PqK', 'bDaPX', 'pRPSt', - ]); - // @codeCoverageIgnoreEnd - } - - public function aparatVideos($count) - { - $out = []; - foreach (range(1, $count) as $c) { - $out[] = $this->aparatVideo(); - } - - return $out; - } - public function word() { return Arr::random(explode(' ', $this->generator->realText())); diff --git a/src/BalootServiceProvider.php b/src/BalootServiceProvider.php index 3fcd7e9..9ab6de1 100644 --- a/src/BalootServiceProvider.php +++ b/src/BalootServiceProvider.php @@ -4,9 +4,11 @@ use Baloot\Models\City; use Baloot\Models\Province; +use Hekmatinasser\Verta\Verta; use Illuminate\Support\Facades\Route; -use Illuminate\Support\Facades\Validator; +use Illuminate\Database\Query\Builder; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Facades\Validator; class BalootServiceProvider extends ServiceProvider { @@ -21,11 +23,6 @@ public function boot() realpath(__DIR__.'/../database/migrations/2014_10_11_000001_create_cities_table.php'), ]); } - if (config('baloot.aparat')) { - $this->loadMigrationsFrom([ - realpath(__DIR__.'/../database/migrations/2014_10_11_000002_create_aparat_videos_table.php'), - ]); - } $this->publishes([ __DIR__.'/../config/config.php' => config_path('baloot.php'), @@ -48,6 +45,8 @@ public function boot() }); } } + + $this->registerQueryBuilderMacros(); } /** @@ -58,4 +57,62 @@ public function register() $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'baloot'); app(\Faker\Generator::class)->addProvider(new BalootFakerProvider(app(\Faker\Generator::class))); } + + /** + * Register query builder macros. + */ + public function registerQueryBuilderMacros() + { + Builder::macro('whereJalali', function (string $column, $operator, $value = null, $boolean = 'and') { + /** + * @var Builder $this + */ + [$value, $operator] = $this->prepareValueAndOperator($value, $operator, func_num_args() === 2); + if (! $value instanceof Verta) { + $value = Verta::parse($value); + } + $this->where($column, $operator, $value->DateTime(), $boolean); + + return $this; + }); + + Builder::macro('whereDateJalali', function (string $column, $operator, $value = null, $boolean = 'and') { + /** + * @var Builder $this + */ + [$value, $operator] = $this->prepareValueAndOperator($value, $operator, func_num_args() === 2); + if (! $value instanceof Verta) { + $value = Verta::parse($value); + } + $this->whereDate($column, $operator, $value->DateTime(), $boolean); + + return $this; + }); + + Builder::macro('whereInMonthJalali', function (string $column, $month, $year = null) { + /** + * @var Builder $this + */ + $year = $year ? $year : verta()->year; + $this->where(function ($query) use ($column, $month, $year) { + $query->whereDate($column, '>=', Verta::createJalaliDate($year, $month, 1)->DateTime()) + ->whereDate($column, '<', Verta::createJalaliDate($year, $month, 1)->addMonth()->DateTime()); + }); + + return $this; + }); + + Builder::macro('whereInYearJalali', function (string $column, $year = null) { + /** + * @var Builder $this + */ + $year = $year ? $year : verta()->year; + $this->where(function ($query) use ($column, $year) { + $query->whereDate($column, '>=', Verta::createJalaliDate($year, 1, 1)->DateTime()) + ->whereDate($column, '<', Verta::createJalaliDate($year, 1, 1)->addYear()->DateTime()); + }); + + return $this; + }); + } } diff --git a/src/EloquentHelper.php b/src/EloquentHelper.php index e9b00ab..e8b6b4d 100644 --- a/src/EloquentHelper.php +++ b/src/EloquentHelper.php @@ -20,15 +20,8 @@ public function getAttribute($key) } elseif ($matches[2] == '_ftt') { return $attributeValue->formatJalaliDatetime(); } - } - } - if ($key && preg_match('/^([A-Za-z0-9_]+)_aparat$/', $key, $matches)) { - $attributeValue = $this->getAttribute($matches[1]); - if (is_string($attributeValue) && $info = Arr::first(aparat_info([$attributeValue]))) { - return $info; - } elseif (is_array($attributeValue) && count($info = aparat_info($attributeValue)) > 0) { - return $info; + return $attributeValue; } } diff --git a/src/Models/AparatVideo.php b/src/Models/AparatVideo.php deleted file mode 100644 index b39bb7a..0000000 --- a/src/Models/AparatVideo.php +++ /dev/null @@ -1,92 +0,0 @@ - 'array', - 'userid' => 'int', - 'visit_cnt' => 'int', - 'cat_id' => 'int', - 'autoplay' => 'bool', - 'is_360d' => 'bool', - 'size' => 'int', - 'can_download' => 'bool', - 'like_cnt' => 'int', - ]; - - /** - * بازخوانی مجدد اطلاعات از سرور آپارات. - * - * @return self - */ - public function reload() - { - $data = Arr::first(aparat_info(['https://www.aparat.com/v/'.$this->uid], false)); - if ($data) { - $this->fill($data); - $this->save(); - } - - return $this; - } -} diff --git a/src/helpers.php b/src/helpers.php index 4f9d7ac..1aefee8 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -55,58 +55,6 @@ function str_to_slug(string $string, string $separator = '-') } } -if (! function_exists('aparat_info')) { - /** - * Get information about videos from aparat api. - * - * @param array $addresses - * @param bool $cache - * @return array - */ - function aparat_info(array $addresses, bool $cache = true) - { - $videoInfos = []; - foreach ($addresses as $address) { - if (! empty($address) && preg_match_all("/https:\/\/www.aparat.com\/v\/(.+)(\/?)/", $address, $matches)) { - $aparatVideo = \Baloot\Models\AparatVideo::where('uid', $matches[1][0])->first(); - - if ($aparatVideo == null || $cache == false) { - try { - $curl = curl_init(); - curl_setopt_array($curl, [ - CURLOPT_URL => 'https://www.aparat.com/etc/api/video/videohash/'.$matches[1][0], - CURLOPT_RETURNTRANSFER => true, - ]); - $responseBody = curl_exec($curl); - if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200 && is_string($responseBody)) { - $responseBody = json_decode($responseBody, true)['video']; - if (isset($responseBody['duration'])) { - $responseBody['is_360d'] = $responseBody['360d']; - if (is_array($responseBody['tags'])) { - $responseBody['tags'] = array_map(function ($tag) { - return $tag['name']; - }, $responseBody['tags']); - } - if ($cache) { - $videoInfos[] = \Baloot\Models\AparatVideo::create($responseBody); - } else { - $videoInfos[] = $responseBody; - } - } - } - curl_close($curl); - } catch (\Exception $e) { - } - } else { - $videoInfos[] = $aparatVideo; - } - } - } - - return $videoInfos; - } -} - if (! function_exists('find_bank_by_card_number')) { /** diff --git a/tests/BasicTest.php b/tests/BasicTest.php index d6287fb..dfd349a 100644 --- a/tests/BasicTest.php +++ b/tests/BasicTest.php @@ -2,16 +2,18 @@ namespace Baloot\Tests; -use Baloot\EloquentHelper; +use Carbon\Carbon; use Baloot\Models\City; +use Baloot\EloquentHelper; use Baloot\Models\Province; -use Carbon\Carbon; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Http\Request; +use Hekmatinasser\Verta\Verta; +use Orchestra\Testbench\TestCase; +use Illuminate\Support\Facades\DB; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Validator; -use Orchestra\Testbench\TestCase; +use Illuminate\Foundation\Testing\DatabaseMigrations; class BasicTest extends TestCase { @@ -174,6 +176,46 @@ public function testRouteBindings() $this->get('/city/random-stuff')->assertStatus(404); } + public function testQueryBuilders() + { + // DateTime + $query = DB::table('test')->whereJalali('column', '1399/01/15 14:00:00'); + $this->assertEquals('select * from "test" where "column" = ?', $query->toSql()); + $this->assertEquals(1, count($query->getBindings())); + $this->assertEquals(Verta::parse('1399/01/15 14:00:00')->DateTime(), $query->getBindings()[0]); + + $query = DB::table('test')->whereJalali('column', '!=', '1399/01/15 14:00:00'); + $this->assertEquals('select * from "test" where "column" != ?', $query->toSql()); + $this->assertEquals(1, count($query->getBindings())); + $this->assertEquals(Verta::parse('1399/01/15 14:00:00')->DateTime(), $query->getBindings()[0]); + + // Date + $query = DB::table('test')->whereDateJalali('column', '1399/01/15'); + $this->assertEquals('select * from "test" where strftime(\'%Y-%m-%d\', "column") = cast(? as text)', $query->toSql()); + $this->assertEquals(1, count($query->getBindings())); + $this->assertEquals('2020-04-03', $query->getBindings()[0]); + + $query = DB::table('test')->whereDateJalali('column', '!=', '1399/01/15'); + $this->assertEquals('select * from "test" where strftime(\'%Y-%m-%d\', "column") != cast(? as text)', $query->toSql()); + $this->assertEquals(1, count($query->getBindings())); + $this->assertEquals('2020-04-03', $query->getBindings()[0]); + + // Month + $query = DB::table('test')->whereInMonthJalali('column', 1, 1399); + $this->assertEquals('select * from "test" where (strftime(\'%Y-%m-%d\', "column") >= cast(? as text) and strftime(\'%Y-%m-%d\', "column") < cast(? as text))', $query->toSql()); + $this->assertEquals(2, count($query->getBindings())); + $this->assertEquals('2020-03-20', $query->getBindings()[0]); + $this->assertEquals('2020-04-20', $query->getBindings()[1]); + + // Year + $query = DB::table('test')->whereInYearJalali('column', 1399); + $this->assertEquals('select * from "test" where (strftime(\'%Y-%m-%d\', "column") >= cast(? as text) and strftime(\'%Y-%m-%d\', "column") < cast(? as text))', $query->toSql()); + $this->assertEquals(2, count($query->getBindings())); + $this->assertEquals('2020-03-20', $query->getBindings()[0]); + $this->assertEquals('2021-03-20', $query->getBindings()[1]); + } + + protected function setUp(): void { parent::setUp();