Skip to content
This repository has been archived by the owner on Oct 20, 2020. It is now read-only.

Commit

Permalink
V2. New structure, daisy chaining now works! SuperModel renamed to Ha…
Browse files Browse the repository at this point in the history
…sSubModels. SubModel renamed to HasAppendedFields. Tests
  • Loading branch information
biscofil committed Oct 10, 2019
1 parent 3313984 commit 164b431
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@
"require": {
"laravel/framework": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "^8.3",
"orchestra/testbench": "^4.1"
},
"autoload": {
"psr-4": {
"Biscofil\\LaravelSubmodels\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Biscofil\\LaravelSubmodels\\Test\\": "tests/"
}
}
}
22 changes: 22 additions & 0 deletions phpunit.xml
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>
19 changes: 7 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ In order to accomplish this result, each Model that has to be extended must impl
``` php
class User extends Authenticatable{

use SuperModel;
use HasSubModels;

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -74,27 +74,22 @@ class User extends Authenticatable{
}
```

On the other side, each sub model can implement `getAppendedFillable` that returns the list of fillable parameters. This list will be merged with the list of the parent class.
On the other side, each sub model can add the `appendedFillable` PRIVATE property that contains the list of fillable parameters. This list will be merged with the list of the parent class.

``` php
class AdminUser extends User{

use SubModel;
use HasAppendedFields;

private $appendedFillable = [
'admin_parameter'
];

public function newQuery()
{
return $this->scopeAdmins(parent::newQuery());
}

/**
* @return array
*/
public function getAppendedFillable()
{
return [
'admin_parameter'
];
}
}

```
Expand Down
31 changes: 31 additions & 0 deletions src/HasAppendedFields.php
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()));
}


}
8 changes: 6 additions & 2 deletions src/SuperModel.php → src/HasSubModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Biscofil\LaravelSubmodels;

trait SuperModel
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

trait HasSubModels
{

/**
Expand All @@ -15,6 +18,7 @@ trait SuperModel
public function newFromBuilder($attributes = [], $connection = null)
{

/** @var Model $model */
$model = $this->newInstance([], true);

$model->setRawAttributes((array)$attributes, true);
Expand All @@ -41,7 +45,7 @@ public abstract function getSubModelClass($model);

/**
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|null
* @return Builder|Model|null
*/
public static function create(array $attributes = [])
{
Expand Down
25 changes: 0 additions & 25 deletions src/SubModel.php

This file was deleted.

25 changes: 25 additions & 0 deletions tests/AdminUser.php
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;
}
}
52 changes: 52 additions & 0 deletions tests/BaseUser.php
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'], []);
}
}
51 changes: 51 additions & 0 deletions tests/CustomerUser.php
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;
}

}
58 changes: 58 additions & 0 deletions tests/DemoTest.php
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);
}
}
Loading

0 comments on commit 164b431

Please sign in to comment.