-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
249 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Domains\Documents; | ||
|
||
use App\Helpers\EnumHelperTrait; | ||
|
||
enum StatusEnum: string | ||
{ | ||
use EnumHelperTrait; | ||
|
||
case Pending = 'pending'; | ||
case Running = 'running'; | ||
case SummaryBuilding = 'summary_building'; | ||
case Complete = 'complete'; | ||
case Failed = 'failed'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Domains\Documents; | ||
|
||
use App\Helpers\EnumHelperTrait; | ||
|
||
/** | ||
* @see settings in config/larachain.php:3 | ||
*/ | ||
enum TypesEnum: string | ||
{ | ||
use EnumHelperTrait; | ||
|
||
case WebHook = 'web_hook'; | ||
case ScrapeWebPage = 'scrape_web_page'; | ||
case PDF = 'pdf'; | ||
case CSV = 'csv'; | ||
case Txt = 'txt'; | ||
case Doc = 'doc'; | ||
case Docx = 'docx'; | ||
case Xls = 'xls'; | ||
case Xlsx = 'xlsx'; | ||
case Ppt = 'ppt'; | ||
case Pptx = 'pptx'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use ReflectionClass; | ||
|
||
trait EnumHelperTrait | ||
{ | ||
public static function enumToKeyValueArray(): array | ||
{ | ||
$enumReflection = new ReflectionClass(self::class); | ||
$cases = $enumReflection->getConstants(); | ||
|
||
$keyValueArray = []; | ||
foreach ($cases as $case) { | ||
$keyValueArray[$case->value] = str($case->name)->headline()->toString(); | ||
} | ||
|
||
return $keyValueArray; | ||
} | ||
|
||
public static function random(): string | ||
{ | ||
$enumReflection = new ReflectionClass(self::class); | ||
$cases = $enumReflection->getConstants(); | ||
$randomKey = array_rand($cases); | ||
|
||
return $cases[$randomKey]->value; | ||
} | ||
|
||
public static function selectOptions(): array | ||
{ | ||
$enumReflection = new ReflectionClass(self::class); | ||
$cases = $enumReflection->getConstants(); | ||
|
||
$keyValueArray = []; | ||
foreach ($cases as $case) { | ||
$keyValueArray[] = [ | ||
'id' => $case->value, | ||
'name' => str($case->name)->headline()->toString(), | ||
]; | ||
} | ||
|
||
return $keyValueArray; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Domains\Documents\StatusEnum; | ||
use App\Domains\Documents\TypesEnum; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
/** | ||
* Class Document | ||
* | ||
* @property int $id | ||
* @property int $collection_id | ||
* @property string|null $summary | ||
* @property string|null $file_path | ||
* @property TypesEnum $type | ||
* @property StatusEnum $status | ||
* @property Collection $collection | ||
*/ | ||
class Document extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $guarded = []; | ||
|
||
protected $cast = [ | ||
'type' => TypesEnum::class, | ||
'status' => StatusEnum::class, | ||
'status' => 'string', | ||
]; | ||
|
||
public function collection(): BelongsTo | ||
{ | ||
return $this->belongsTo(Collection::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Domains\Documents\StatusEnum; | ||
use App\Domains\Documents\TypesEnum; | ||
use App\Models\Collection; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Document> | ||
*/ | ||
class DocumentFactory extends Factory | ||
{ | ||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'type' => TypesEnum::random(), | ||
'status' => StatusEnum::random(), | ||
'summary' => $this->faker->text(), | ||
'file_path' => $this->faker->url(), | ||
'collection_id' => Collection::factory(), | ||
]; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
database/migrations/2024_03_25_020252_create_documents_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use App\Domains\Documents\StatusEnum; | ||
use App\Models\Collection; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('documents', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('type')->nullable(); | ||
$table->string('status')->default(StatusEnum::Pending); | ||
$table->longText('summary')->nullable(); | ||
$table->string('file_path')->nullable(); | ||
$table->foreignIdFor(Collection::class); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('documents'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Models; | ||
|
||
use Tests\TestCase; | ||
|
||
class DocumentTest extends TestCase | ||
{ | ||
/** | ||
* A basic feature test example. | ||
*/ | ||
public function test_factory(): void | ||
{ | ||
$model = \App\Models\Document::factory()->create(); | ||
|
||
$this->assertNotNull($model->collection_id); | ||
$this->assertNotNull($model->collection->id); | ||
$this->assertCount(1, $model->collection->documents); | ||
$this->assertNotNull($model->collection->documents()->first()->id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters