-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
1 parent
fee2434
commit db2adc0
Showing
10 changed files
with
356 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Lift\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
final class PrimaryKey | ||
{ | ||
public function __construct( | ||
public string $type = 'int', | ||
public bool $incrementing = true, | ||
) { | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Lift\Concerns; | ||
|
||
use Illuminate\Support\Collection; | ||
use WendellAdriel\Lift\Attributes\PrimaryKey; | ||
use WendellAdriel\Lift\Support\PropertyInfo; | ||
|
||
trait CustomPrimary | ||
{ | ||
/** | ||
* @param Collection<PropertyInfo> $properties | ||
*/ | ||
private function applyPrimaryKey(Collection $properties): void | ||
{ | ||
$primaryKeyProperty = self::getPropertyForAttribute($properties, PrimaryKey::class); | ||
if (! blank($primaryKeyProperty)) { | ||
$primaryKeyAttribute = $primaryKeyProperty->attributes->first(fn ($attribute) => $attribute->getName() === PrimaryKey::class); | ||
if (! blank($primaryKeyAttribute)) { | ||
$primaryKeyAttribute = $primaryKeyAttribute->newInstance(); | ||
$this->setKeyName($primaryKeyProperty->name); | ||
$this->setKeyType($primaryKeyAttribute->type); | ||
$this->setIncrementing($primaryKeyAttribute->incrementing); | ||
|
||
if (! $this->incrementing) { | ||
$this->mergeFillable([$primaryKeyProperty->name]); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Lift\Tests\Datasets; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use WendellAdriel\Lift\Attributes\PrimaryKey; | ||
use WendellAdriel\Lift\Attributes\Rules; | ||
use WendellAdriel\Lift\Lift; | ||
|
||
class UserCustom extends Model | ||
{ | ||
use Lift; | ||
|
||
#[PrimaryKey(incrementing: false)] | ||
public int $id; | ||
|
||
#[Rules(['required', 'string'], ['required' => 'The user name cannot be empty'])] | ||
public string $name; | ||
|
||
#[Rules(['required', 'email'])] | ||
public string $email; | ||
|
||
#[Rules(['required', 'string', 'min:8'])] | ||
public string $password; | ||
|
||
protected $table = 'users'; | ||
|
||
protected $fillable = [ | ||
'name', | ||
'email', | ||
'password', | ||
]; | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Lift\Tests\Datasets; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use WendellAdriel\Lift\Attributes\PrimaryKey; | ||
use WendellAdriel\Lift\Attributes\Rules; | ||
use WendellAdriel\Lift\Lift; | ||
|
||
class UserUuid extends Model | ||
{ | ||
use Lift; | ||
|
||
#[PrimaryKey(type: 'string', incrementing: false)] | ||
public string $uuid; | ||
|
||
#[Rules(['required', 'string'], ['required' => 'The user name cannot be empty'])] | ||
public string $name; | ||
|
||
#[Rules(['required', 'email'])] | ||
public string $email; | ||
|
||
#[Rules(['required', 'string', 'min:8'])] | ||
public string $password; | ||
|
||
protected $table = 'users_uuid'; | ||
|
||
protected $fillable = [ | ||
'name', | ||
'email', | ||
'password', | ||
]; | ||
} |
Oops, something went wrong.