-
Notifications
You must be signed in to change notification settings - Fork 2
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
8 changed files
with
274 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ php: | |
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
|
||
services: | ||
- redis-server | ||
|
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Change Log | ||
|
||
#v0.4.6 | ||
- New feature: HasCastables trait. | ||
|
||
#v0.4.5 | ||
|
||
- New feature: Random worker terminator. | ||
|
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 |
---|---|---|
|
@@ -25,5 +25,10 @@ | |
"psr-4": { | ||
"Halaei\\Helpers\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"files": [ | ||
"tests/TestAssets.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,84 @@ | ||
<?php | ||
|
||
namespace Halaei\Helpers\Eloquent; | ||
|
||
use Halaei\Helpers\Objects\Casting; | ||
use Halaei\Helpers\Objects\DataCollection; | ||
use Halaei\Helpers\Objects\DataObject; | ||
use Halaei\Helpers\Objects\Rawable; | ||
|
||
trait HasCastables | ||
{ | ||
/** | ||
* @var DataCollection[]|DataObject[] | ||
*/ | ||
protected $castedAttributes = []; | ||
|
||
/** | ||
* Boots HasCustomAttributes Trait. By addi | ||
*/ | ||
public static function bootHasCastables() | ||
{ | ||
static::saving(function ($model) { | ||
$model->prepareSaving(); | ||
}); | ||
} | ||
|
||
protected function getCastedAttribute($key, $value, $cast) | ||
{ | ||
if (! array_key_exists($key, $this->castedAttributes)) { | ||
$this->castedAttributes[$key] = is_null($value) ? null : Casting::cast($value, $cast, $key); | ||
} | ||
return $this->castedAttributes[$key]; | ||
} | ||
|
||
/* | ||
* Overrides | ||
*/ | ||
|
||
public function attributesToArray() | ||
{ | ||
$attributes = parent::attributesToArray(); | ||
|
||
foreach ($this->castedAttributes as $key => $value) { | ||
if ($value instanceof Rawable) { | ||
$attributes[$key] = $value->toArray(); | ||
} | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
public function getAttribute($key) | ||
{ | ||
if (array_key_exists($key, static::$castables)) { | ||
return $this->getCastedAttribute($key, parent::getAttribute($key), static::$castables[$key]); | ||
} | ||
|
||
return parent::getAttribute($key); | ||
} | ||
|
||
public function setAttribute($key, $value) | ||
{ | ||
if ($value instanceof Rawable) { | ||
$this->castedAttributes[$key] = $value; | ||
return $this; | ||
} | ||
unset($this->castedAttributes[$key]); | ||
return parent::setAttribute($key, $value); | ||
} | ||
|
||
public function prepareSaving() | ||
{ | ||
foreach ($this->castedAttributes as $key => $value) { | ||
$this->attributes[$key] = $value instanceof Rawable ? $value->toRaw() : $value; | ||
} | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
unset($this->castedAttributes[$offset]); | ||
|
||
parent::offsetUnset($offset); | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
namespace HalaeiTests; | ||
|
||
use Halaei\Helpers\Objects\DataCollection; | ||
|
||
class HasCastablesTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function test_user_model() | ||
{ | ||
// assign raw value | ||
$user = new UserModel(); | ||
$this->assertNull($user->mobile); | ||
$user->mobile = '+98-9121231212'; | ||
$this->assertInstanceOf(Mobile::class, $user->mobile); | ||
$this->assertEquals('98', $user->mobile->code); | ||
$this->assertEquals('9121231212', $user->mobile->number); | ||
$this->assertEquals(['code' => '98', 'number' => '9121231212'], $user->toArray()['mobile']); | ||
|
||
// assign data object | ||
$user = new UserModel(); | ||
$user->mobile = new Mobile([ | ||
'code' => '98', | ||
'number' => '9121231212', | ||
]); | ||
$this->assertInstanceOf(Mobile::class, $user->mobile); | ||
$this->assertEquals('98', $user->mobile->code); | ||
$this->assertEquals('9121231212', $user->mobile->number); | ||
|
||
// assign in constructor | ||
$user = new UserModel([ | ||
'mobile' => '+98-9121231212', | ||
]); | ||
$this->assertInstanceOf(Mobile::class, $user->mobile); | ||
$this->assertEquals('98', $user->mobile->code); | ||
$this->assertEquals('9121231212', $user->mobile->number); | ||
|
||
} | ||
|
||
public function test_order() | ||
{ | ||
$order = new OrderModel([ | ||
'items' => [ | ||
[ | ||
'code' => '#123', | ||
'quantity' => 10, | ||
], | ||
[ | ||
'code' => '#124', | ||
'quantity' => 1, | ||
], | ||
] | ||
]); | ||
$this->assertInstanceOf(DataCollection::class, $order->items); | ||
$this->assertInstanceOf(Item::class, $order->items[0]); | ||
$this->assertEquals('#124', $order->items[1]->code); | ||
} | ||
} |
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,106 @@ | ||
<?php | ||
|
||
namespace HalaeiTests; | ||
|
||
use Halaei\Helpers\Eloquent\HasCastables; | ||
use Halaei\Helpers\Objects\DataObject; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* @property int $id | ||
* @property string $name | ||
* @property Mobile $mobile | ||
* @property Order[] $orders | ||
* @property | ||
*/ | ||
class User extends DataObject | ||
{ | ||
public static function relations() | ||
{ | ||
return [ | ||
'mobile' => [Mobile::class, 'decode'], | ||
'orders' => [Order::class], | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
* @property string $code | ||
* @property string $number | ||
*/ | ||
class Mobile extends DataObject | ||
{ | ||
public static function decode($str) | ||
{ | ||
if (preg_match('/^\+(\d+)-(\d+)$/', $str, $parts)) { | ||
return new self(['code' => $parts[1], 'number' => $parts[2]]); | ||
} | ||
} | ||
|
||
public function toRaw() | ||
{ | ||
return '+'.$this->code.'-'.$this->number; | ||
} | ||
} | ||
|
||
/** | ||
* @property string $id | ||
* @property Item[] $items | ||
*/ | ||
class Order extends DataObject | ||
{ | ||
public static function relations() | ||
{ | ||
return [ | ||
'items' => [Item::class], | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
* @property string $code | ||
* @property int $quantity | ||
*/ | ||
class Item extends DataObject | ||
{ | ||
} | ||
|
||
/** | ||
* @property Cell[][] $cells | ||
*/ | ||
class Matrix extends DataObject | ||
{ | ||
public static function relations() | ||
{ | ||
return ['cells' => [[Cell::class]]]; | ||
} | ||
} | ||
|
||
/** | ||
* @property $v | ||
*/ | ||
class Cell extends DataObject | ||
{ | ||
} | ||
|
||
class UserModel extends Model | ||
{ | ||
use HasCastables; | ||
|
||
protected $fillable = ['mobile']; | ||
|
||
protected static $castables = [ | ||
'mobile' => [Mobile::class, 'decode'], | ||
]; | ||
} | ||
|
||
class OrderModel extends Model | ||
{ | ||
use HasCastables; | ||
|
||
protected $fillable = ['items']; | ||
|
||
protected static $castables = [ | ||
'items' => [Item::class], | ||
]; | ||
} |