Skip to content

Commit

Permalink
Support json string on cast setter
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Oct 29, 2021
1 parent 714224b commit 8cf7d8e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: true
fail-fast: false
matrix:
php: [8.0]
laravel: [8.*]
Expand All @@ -23,9 +23,9 @@ jobs:
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: laravel_schemaless_attributes
MYSQL_ROOT_PASSWORD: root_password
MYSQL_ROOT_PASSWORD: null
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_PASSWORD: null
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
Expand Down
13 changes: 13 additions & 0 deletions src/Casts/SchemalessAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public function get($model, $key, $value, $attributes)
*/
public function set($model, $key, $value, $attributes)
{
if ($this->isJson($value)) {
return $value;
}

return json_encode($value);
}

protected function isJson($value): bool
{
if (! is_string($value)) {
return false;
}

return $value === json_encode(json_decode($value));
}
}
21 changes: 21 additions & 0 deletions tests/HasSchemalessAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public function an_schemaless_attribute_can_be_set()
$this->assertEquals('value', $this->testModel->schemaless_attributes->name);
}

/** @test */
public function an_schemaless_attribute_can_be_set_from_json()
{
$this->testModel->schemaless_attributes = json_encode(['name' => 'value']);

$this->assertEquals('value', $this->testModel->schemaless_attributes->name);
}

/** @test */
public function it_can_determine_if_it_has_a_schemaless_attribute()
{
Expand Down Expand Up @@ -245,6 +253,19 @@ public function it_can_add_and_save_schemaless_attributes_in_one_go()
$this->assertEquals($array, $testModel->schemaless_attributes->all());
}

/** @test */
public function it_can_and_save_schemaless_attributes_from_json()
{
$array = [
'name' => 'value',
'name2' => 'value2',
];

$testModel = TestModel::create(['schemaless_attributes' => json_encode($array)]);

$this->assertEquals($array, $testModel->schemaless_attributes->all());
}

/** @test */
public function it_has_a_scope_to_get_models_with_the_given_schemaless_attributes()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ services:
restart: always
image: mysql/mysql-server:8.0
environment:
MYSQL_ROOT_PASSWORD: "root_password"
MYSQL_ROOT_PASSWORD: null
MYSQL_DATABASE: "laravel_schemaless_attributes"
MYSQL_USER: "username"
MYSQL_PASSWORD: "password"
MYSQL_PASSWORD: null
MYSQL_ROOT_HOST: "0.0.0.0"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
ports:
Expand Down

0 comments on commit 8cf7d8e

Please sign in to comment.