This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V2. New structure, daisy chaining now works! SuperModel renamed to Ha…
…sSubModels. SubModel renamed to HasAppendedFields. Tests
- Loading branch information
Showing
13 changed files
with
302 additions
and
39 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
/composer.lock |
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,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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,31 @@ | ||
<?php | ||
|
||
namespace Biscofil\LaravelSubmodels; | ||
|
||
trait HasAppendedFields | ||
{ | ||
|
||
/** | ||
* Get the fillable attributes for the model. | ||
* | ||
* @return array | ||
*/ | ||
public function getFillable() | ||
{ | ||
$appendedFillable = property_exists($this, 'appendedFillable') ? $this->appendedFillable : []; | ||
return array_unique(array_merge($appendedFillable, parent::getFillable())); | ||
} | ||
|
||
/** | ||
* Get the attributes for the model. | ||
* | ||
* @return array | ||
*/ | ||
public function getAttributes() | ||
{ | ||
$appendedAttributes = property_exists($this, 'appendedAttributes') ? $this->appendedAttributes : []; | ||
return array_unique(array_merge($appendedAttributes, parent::getAttributes())); | ||
} | ||
|
||
|
||
} |
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 was deleted.
Oops, something went wrong.
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,25 @@ | ||
<?php | ||
|
||
namespace Biscofil\LaravelSubmodels\Test; | ||
|
||
use Biscofil\LaravelSubmodels\HasAppendedFields; | ||
|
||
class AdminUser extends BaseUser | ||
{ | ||
|
||
use HasAppendedFields; | ||
|
||
private $appendedFillable = [ | ||
'admin_name' | ||
]; | ||
|
||
public function operation() | ||
{ | ||
return "admin"; | ||
} | ||
|
||
public function getAppendedFillable() | ||
{ | ||
return $this->appendedFillable; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace Biscofil\LaravelSubmodels\Test; | ||
|
||
|
||
use Biscofil\LaravelSubmodels\HasSubModels; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* @property bool is_admin | ||
*/ | ||
class BaseUser extends Model | ||
{ | ||
use HasSubModels; | ||
|
||
protected $fillable = [ | ||
'is_admin' | ||
]; | ||
|
||
protected $casts = [ | ||
'is_admin' => 'bool' | ||
]; | ||
|
||
/** | ||
* @param $model | ||
* @return string | ||
*/ | ||
public function getSubModelClass($model) | ||
{ | ||
/** @var BaseUser $model */ | ||
if ($model->isAdmin()) { | ||
return AdminUser::class; | ||
} else { | ||
return CustomerUser::class; | ||
} | ||
} | ||
|
||
public function isAdmin() | ||
{ | ||
return $this->is_admin; | ||
} | ||
|
||
public function operation(){ | ||
return "base"; | ||
} | ||
|
||
|
||
public function getData() | ||
{ | ||
return array_merge(['base'], []); | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
namespace Biscofil\LaravelSubmodels\Test; | ||
|
||
use Biscofil\LaravelSubmodels\HasSubModels; | ||
use Biscofil\LaravelSubmodels\HasAppendedFields; | ||
|
||
/** | ||
* @property bool is_nested_customer | ||
*/ | ||
class CustomerUser extends BaseUser | ||
{ | ||
|
||
use HasAppendedFields; | ||
use HasSubModels; | ||
|
||
private $appendedFillable = [ | ||
'customer_name', | ||
'is_nested_customer' | ||
]; | ||
|
||
public function isNestedCustomer() | ||
{ | ||
return $this->is_nested_customer; | ||
} | ||
|
||
/** | ||
* @param CustomerUser $model | ||
* @return string | ||
*/ | ||
public function getSubModelClass($model) | ||
{ | ||
/** @var CustomerUser $model */ | ||
if ($model->isNestedCustomer()) { | ||
return NestedCustomerUser::class; | ||
} else { | ||
return self::class; | ||
} | ||
} | ||
|
||
public function operation() | ||
{ | ||
return "customer"; | ||
} | ||
|
||
public function getAppendedFillable() | ||
{ | ||
return $this->appendedFillable; | ||
} | ||
|
||
} |
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 Biscofil\LaravelSubmodels\Test; | ||
|
||
class DemoTest extends TestCase | ||
{ | ||
|
||
private $baseUser; | ||
private $adminUser; | ||
private $customerUser; | ||
private $nestedCustomerUser; | ||
|
||
public function __construct($name = null, array $data = [], $dataName = '') | ||
{ | ||
parent::__construct($name, $data, $dataName); | ||
|
||
$this->baseUser = new BaseUser(); | ||
|
||
$this->adminUser = new AdminUser(); | ||
$this->customerUser = new CustomerUser(); | ||
$this->nestedCustomerUser = new NestedCustomerUser(); | ||
|
||
} | ||
|
||
public function testAdmin() | ||
{ | ||
$expected = array_merge( | ||
$this->baseUser->getFillable(), | ||
$this->adminUser->getAppendedFillable() | ||
); | ||
$actual = $this->adminUser->getFillable(); | ||
|
||
$this->assertEqualsCanonicalizing($expected, $actual); | ||
} | ||
|
||
public function testCustomer() | ||
{ | ||
$expected = array_merge( | ||
$this->baseUser->getFillable(), | ||
$this->customerUser->getAppendedFillable() | ||
); | ||
$actual = $this->customerUser->getFillable(); | ||
|
||
$this->assertEqualsCanonicalizing($expected, $actual); | ||
} | ||
|
||
public function testNestedCustomer() | ||
{ | ||
$expected = array_unique(array_merge( | ||
$this->baseUser->getFillable(), | ||
$this->customerUser->getAppendedFillable(), | ||
$this->nestedCustomerUser->getAppendedFillable() | ||
)); | ||
$actual = $this->nestedCustomerUser->getFillable(); | ||
|
||
$this->assertEqualsCanonicalizing($expected, $actual); | ||
} | ||
} |
Oops, something went wrong.