Skip to content

Commit

Permalink
Replace is_callable by instanceof Closure
Browse files Browse the repository at this point in the history
  • Loading branch information
guidocella committed Feb 13, 2017
1 parent 01da690 commit 69ff658
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ $populator->add(Post::class, 5, [
]);
```

The model received by the closures will have non-callable attributes and callable attributes of columns that come before in the database already set.
The model received by the closures will have non-closure attributes and closure attributes of columns that come before in the database already set.

You can also pass an array of functions as the 4th argument and they'll be called before the model's insertion.

Expand Down Expand Up @@ -254,7 +254,7 @@ States will work even if you define them without defining their model in the fac

### Closure attributes

Populator will call closure attributes in factory definitions and states together with those of custom attributes and with the same arguments. So like with custom attributes, they will receive the model with non-callable attributes and the return values of callable attributes that come before in the database set. This means that you can do something like this:
Populator will call closure attributes in factory definitions and states together with those of custom attributes and with the same arguments. So like with custom attributes, they will receive the model with non-closure attributes and the return values of closure attributes that come before in the database set. This means that you can do something like this:

```php
$factory->define(Post::class, function (Faker\Generator $faker) {
Expand Down
16 changes: 9 additions & 7 deletions src/ModelPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,21 +547,23 @@ protected function fillModel(

// To maximize the number of attributes already set in the
// model when accessing it from closure custom attributes,
// we'll first set all the non-callable attributes.
// we'll first set all the non-closure attributes.
$closureAttributes = [];

foreach ($attributes as $column => $attribute) {
if (is_callable($attribute)) {
$closureAttributes[$column] = $attribute;
foreach ($attributes as $key => $value) {
// Don't use is_callable() here since it returns true for values
// that happen to be function names, e.g. country code "IS".
if ($value instanceof \Closure) {
$closureAttributes[$key] = $value;
} else {
$model->$column = $attribute;
$model->$key = $value;
}
}

// We'll set the remaining attributes while evaluating the closures,
// so that the model will have the return values of previous closures already set.
foreach ($closureAttributes as $column => $attribute) {
$model->$column = $attribute($model, $insertedPKs);
foreach ($closureAttributes as $key => $value) {
$model->$key = $value($model, $insertedPKs);
}
}

Expand Down

0 comments on commit 69ff658

Please sign in to comment.