Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support json string on cast setter #90

Merged
merged 2 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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