Skip to content

Commit

Permalink
Fix some prefix collisions when extracting default values.
Browse files Browse the repository at this point in the history
  • Loading branch information
jails committed Feb 11, 2019
1 parent cc35493 commit 0c7c8bb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,11 +1,11 @@
sudo: required
dist: xenial
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3

matrix:
fast_finish: true
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -12,7 +12,7 @@
"crysalead/validator": "~3.0"
},
"require-dev": {
"kahlan/kahlan": "~3.0"
"kahlan/kahlan": "~4.5"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 17 additions & 1 deletion spec/Suite/Schema.spec.php
Expand Up @@ -257,6 +257,23 @@

});

it("correctly sets default values with stars and prefix collisions", function() {

$schema = new Schema();
$schema->column('datasource', ['type' => 'boolean', 'default' => false]);
$schema->column('data', ['type' => 'object', 'default' => []]);
$schema->column('data.*', ['type' => 'object', 'default' => []]);
$schema->column('data.*.checked', ['type' => 'boolean', 'default' => true]);

$schema->locked(true);

$document = new Document(['schema' => $schema]);

expect($document->get('datasource'))->toBe(false);
expect($document->get('data.value1.checked'))->toBe(true);

});

});

describe("->type()", function() {
Expand Down Expand Up @@ -1304,7 +1321,6 @@

});


describe(".bulkInsert()", function() {

it("throws an ORMException", function() {
Expand Down
7 changes: 4 additions & 3 deletions src/Schema.php
Expand Up @@ -347,10 +347,11 @@ public function defaults($basePath = null)
{
$defaults = [];
foreach ($this->_columns as $key => $value) {
if ($basePath && strpos($key, $basePath) !== 0) {
continue;
if ($basePath) {
$fieldName = strpos($key, $basePath . '.') === 0 ? substr($key, strlen($basePath) + 1) : null;
} else {
$fieldName = $key;
}
$fieldName = $basePath ? substr($key, strlen($basePath) + 1) : $key;
if (!$fieldName || $fieldName === '*' || strpos($fieldName, '.') !== false) {
continue;
}
Expand Down

0 comments on commit 0c7c8bb

Please sign in to comment.