Skip to content

Commit

Permalink
Merge pull request #90 from erikn69/cast_support_json_string
Browse files Browse the repository at this point in the history
Support json string on cast setter
  • Loading branch information
freekmurze authored Nov 6, 2021
2 parents 714224b + c01e504 commit 6a27ac4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
8 changes: 3 additions & 5 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,7 @@ jobs:
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: laravel_schemaless_attributes
MYSQL_ROOT_PASSWORD: root_password
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: null
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
Expand All @@ -38,7 +36,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl, exif, iconv
coverage: none

- name: Install dependencies
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: 1 addition & 3 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ 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_ROOT_HOST: "0.0.0.0"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
ports:
Expand Down

0 comments on commit 6a27ac4

Please sign in to comment.