Skip to content

Commit

Permalink
Add Arrayable illuminate support contract to Model
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Sep 14, 2021
1 parent 50bc66e commit 2e5d4ce
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
17 changes: 14 additions & 3 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
use LdapRecord\Models\Events\Renaming;
use LdapRecord\Query\Model\Builder;
use LdapRecord\Support\Arr;
use Illuminate\Contracts\Support\Arrayable;
use UnexpectedValueException;

/** @mixin Builder */
abstract class Model implements ArrayAccess, JsonSerializable
abstract class Model implements ArrayAccess, Arrayable, JsonSerializable
{
use EscapesValues;
use Concerns\HasEvents;
Expand Down Expand Up @@ -568,13 +569,23 @@ public function __unset($key)
}

/**
* Convert the object into something JSON serializable.
* Convert the model to its JSON encodeable array form.
*
* @return void
*/
public function toArray()
{
return $this->attributesToArray();
}

/**
* Convert the model's attributes into JSON encodeable values.
*
* @return array
*/
public function jsonSerialize()
{
return $this->attributesToArray();
return $this->toArray();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/ActiveDirectory/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function test_object_sid_is_converted()

$m = new Entry(['objectsid' => hex2bin($hex)]);

$this->assertEquals('S-1-5-21-1004336348-1177238915-682003330-512', $m->jsonSerialize()['objectsid'][0]);
$this->assertEquals('S-1-5-21-1004336348-1177238915-682003330-512', $m->toArray()['objectsid'][0]);
}

public function test_is_deleted()
Expand Down
8 changes: 4 additions & 4 deletions tests/Models/ModelAccessorMutatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function test_model_uses_accessor()
$model->createtimestamp = $date;

$this->assertEquals(['bar'], $model->getAttributes()['foo']);
$this->assertEquals(['barbaz'], $model->jsonSerialize()['foo']);
$this->assertEquals([$date->format(\DateTimeInterface::ISO8601)], $model->jsonSerialize()['createtimestamp']);
$this->assertEquals(['barbaz'], $model->toArray()['foo']);
$this->assertEquals([$date->format(\DateTimeInterface::ISO8601)], $model->toArray()['createtimestamp']);
$this->assertEquals('barbaz', $model->foo);
$this->assertEquals('barbaz', $model->getAttribute('foo'));
$this->assertEquals('barbaz', $model->getFirstAttribute('foo'));
Expand All @@ -39,7 +39,7 @@ public function test_model_uses_accessor_with_hyphen()
$model = new ModelAccessorStub();

$this->assertEquals('baz-other', $model->getAttribute('foo-bar'));
$this->assertEquals(['baz'], $model->jsonSerialize()['foo-bar']);
$this->assertEquals(['baz'], $model->toArray()['foo-bar']);
$this->assertEquals('baz-other', $model->foo_bar);
$this->assertNull($model->foobar);
$this->assertNull($model->getAttribute('foobar'));
Expand All @@ -55,7 +55,7 @@ public function test_model_uses_mutator()

$this->assertEquals(['setter-baz'], $model->foo);
$this->assertEquals(['setter-baz'], $model->getAttributes()['foo']);
$this->assertEquals(['setter-baz'], $model->jsonSerialize()['foo']);
$this->assertEquals(['setter-baz'], $model->toArray()['foo']);
$this->assertTrue(isset($model->foo));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Models/ModelAttributeAppendsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function test_accessors_are_appended()
{
$this->assertEquals([
'foo' => ['bar'],
], (new ModelAttributeAppendsTestStub())->jsonSerialize());
], (new ModelAttributeAppendsTestStub())->toArray());
}

public function test_get_appends()
Expand Down Expand Up @@ -41,7 +41,7 @@ public function test_appends_with_hyphenated_property()

$this->assertEquals([
'foo-bar' => ['foo-bar'],
], $model->jsonSerialize());
], $model->toArray());
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Models/ModelGuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public function test_object_guid_is_converted()

$m = new Entry(['objectguid' => hex2bin($hex)]);

$this->assertEquals('270db4d0-249d-46a7-9cc5-eb695d9af9ac', $m->jsonSerialize()['objectguid'][0]);
$this->assertEquals('270db4d0-249d-46a7-9cc5-eb695d9af9ac', $m->toArray()['objectguid'][0]);
}
}
18 changes: 9 additions & 9 deletions tests/Models/ModelHiddenAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function test_attributes_can_be_hidden()
'baz' => 'zal',
]);

$this->assertEquals(['baz' => ['zal']], $m->jsonSerialize());
$this->assertEquals(['baz' => ['zal']], $m->toArray());
}

public function test_attributes_can_be_visible()
Expand All @@ -42,7 +42,7 @@ public function test_attributes_can_be_visible()
'baz' => 'zal',
]);

$this->assertEquals(['foo' => ['bar']], $m->jsonSerialize());
$this->assertEquals(['foo' => ['bar']], $m->toArray());
}

public function test_attributes_can_be_made_visible()
Expand All @@ -57,7 +57,7 @@ public function test_attributes_can_be_made_visible()
$this->assertEquals([
'foo' => ['bar'],
'baz' => ['zal'],
], $m->jsonSerialize());
], $m->toArray());
}

public function test_attributes_can_be_made_hidden()
Expand All @@ -69,7 +69,7 @@ public function test_attributes_can_be_made_hidden()

$m->makeHidden('foo');

$this->assertEquals(['baz' => ['zal']], $m->jsonSerialize());
$this->assertEquals(['baz' => ['zal']], $m->toArray());
}

public function test_attributes_can_be_added_as_hidden()
Expand All @@ -81,7 +81,7 @@ public function test_attributes_can_be_added_as_hidden()

$m->addHidden('baz');

$this->assertEmpty($m->jsonSerialize());
$this->assertEmpty($m->toArray());
}

public function test_hidden_cannot_be_overridden_and_made_visible()
Expand All @@ -93,7 +93,7 @@ public function test_hidden_cannot_be_overridden_and_made_visible()

$m->setVisible(['foo']);

$this->assertEmpty($m->jsonSerialize());
$this->assertEmpty($m->toArray());
}

public function test_visible_can_be_overridden()
Expand All @@ -105,7 +105,7 @@ public function test_visible_can_be_overridden()

$m->setHidden(['baz']);

$this->assertEquals(['foo' => ['bar']], $m->jsonSerialize());
$this->assertEquals(['foo' => ['bar']], $m->toArray());
}

public function test_visible_attributes_can_be_added()
Expand All @@ -120,7 +120,7 @@ public function test_visible_attributes_can_be_added()
$this->assertEquals([
'foo' => ['bar'],
'baz' => ['zal'],
], $m->jsonSerialize());
], $m->toArray());
}

public function test_attribute_keys_are_normalized()
Expand All @@ -132,7 +132,7 @@ public function test_attribute_keys_are_normalized()

$m->makeHidden(['FOO', 'bAz']);

$this->assertEmpty($m->jsonSerialize());
$this->assertEmpty($m->toArray());
}
}

Expand Down

0 comments on commit 2e5d4ce

Please sign in to comment.