Skip to content

Commit 65a32dd

Browse files
author
Darryl Kuhn
committed
add the ability to get a list of dirty attributes including Json attributes
1 parent bfd58ca commit 65a32dd

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Dialect/Json.php

+32-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ trait Json
1616
'#>>' ];
1717

1818
/**
19-
* Holds the map of attributes and the JSON colums they are stored in.
19+
* Holds the map of attributes and the JSON colums they are stored in. This
20+
* will take the form of:
21+
* [ 'json_element_1' => 'original_column',
22+
* 'json_element_2' => 'original_column' ]
2023
*
2124
* @var array
2225
*/
@@ -247,7 +250,6 @@ protected function mutateAttribute($key, $value)
247250
return null;
248251
}
249252

250-
251253
return parent::mutateAttribute($key, $value);
252254
}
253255

@@ -294,6 +296,34 @@ public function setJsonAttribute($attribute, $key, $value)
294296
return;
295297
}
296298

299+
/**
300+
* Add json attributes to the list of things that have changed (when
301+
* they've changed)
302+
*
303+
* @return Array
304+
*/
305+
public function getDirty( $includeJson = false )
306+
{
307+
$dirty = parent::getDirty();
308+
309+
if ( !$includeJson ) {
310+
return $dirty;
311+
}
312+
313+
foreach (array_unique($this->jsonAttributes) as $attribute) {
314+
$originals[$attribute] = json_decode(array_get($this->original,$attribute,'null'), true);
315+
}
316+
317+
foreach ($this->jsonAttributes as $jsonAttribute => $jsonColumn) {
318+
if ($this->$jsonAttribute !== null &&
319+
$this->$jsonAttribute !== array_get($originals[$jsonColumn], $jsonAttribute)) {
320+
$dirty[$jsonAttribute] = json_encode($this->$jsonAttribute);
321+
}
322+
}
323+
324+
return $dirty;
325+
}
326+
297327
/**
298328
* Allows you to specify if the actual JSON column housing the attributes
299329
* should be shown on toArray() and toJson() calls. Set this value in the

tests/JsonDialectTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,26 @@ public function testHintedJsonColumns()
143143
$this->assertEquals( $mock->testColumn, json_encode(['foo'=>'bar']) );
144144
}
145145

146+
/**
147+
* Assert that defined JSON attributes are returned in the getDirty()
148+
* response when expected.
149+
*/
150+
public function testGetDirtyJson()
151+
{
152+
// Mock the model with data
153+
$mock = new MockJsonDialectModel;
154+
$mock->hintJsonStructure( 'testColumn', json_encode(['foo'=>null]) );
155+
156+
// At this point 'foo' should not be dirty
157+
$this->assertArrayNotHasKey( 'foo', $mock->getDirty(true) );
158+
159+
$mock->setAttribute('testColumn', json_encode(['foo' => 'bar']));
160+
161+
// Now that 'foo' has been changed it should show up in the getDirty()
162+
// response
163+
$this->assertArrayHasKey( 'foo', $mock->getDirty(true) );
164+
}
165+
146166
/**
147167
* Assert that an exception is thrown when given invalid json as a
148168
* structure hint

0 commit comments

Comments
 (0)