Ajout jsonserializable
This commit is contained in:
@@ -5,9 +5,10 @@ namespace Kletellier\PdoWrapper;
|
|||||||
use ArrayIterator;
|
use ArrayIterator;
|
||||||
use Countable;
|
use Countable;
|
||||||
use IteratorAggregate;
|
use IteratorAggregate;
|
||||||
|
use JsonSerializable;
|
||||||
use Traversable;
|
use Traversable;
|
||||||
|
|
||||||
class Collection implements IteratorAggregate, Countable
|
class Collection implements IteratorAggregate, Countable, JsonSerializable
|
||||||
{
|
{
|
||||||
private array $elements;
|
private array $elements;
|
||||||
|
|
||||||
@@ -111,4 +112,9 @@ class Collection implements IteratorAggregate, Countable
|
|||||||
{
|
{
|
||||||
return new ArrayIterator($this->elements);
|
return new ArrayIterator($this->elements);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize(): mixed
|
||||||
|
{
|
||||||
|
return $this->elements;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
namespace Kletellier\PdoWrapper;
|
namespace Kletellier\PdoWrapper;
|
||||||
|
|
||||||
class Model
|
use JsonSerializable;
|
||||||
|
|
||||||
|
class Model implements JsonSerializable
|
||||||
{
|
{
|
||||||
protected mixed $values;
|
protected mixed $values;
|
||||||
protected array $updated;
|
protected array $updated;
|
||||||
@@ -153,4 +155,9 @@ class Model
|
|||||||
|
|
||||||
return $table;
|
return $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize(): mixed
|
||||||
|
{
|
||||||
|
return $this->values;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,9 @@ test("collection", function () {
|
|||||||
expect($collo)->toBeInstanceOf(Collection::class);
|
expect($collo)->toBeInstanceOf(Collection::class);
|
||||||
expect(count($collo))->toBe(2);
|
expect(count($collo))->toBe(2);
|
||||||
|
|
||||||
|
$json = json_encode($collo);
|
||||||
|
expect($json)->toBeJson();
|
||||||
|
|
||||||
$pluck = $collo->pluck("key");
|
$pluck = $collo->pluck("key");
|
||||||
expect($pluck)->toBeInstanceOf(Collection::class);
|
expect($pluck)->toBeInstanceOf(Collection::class);
|
||||||
expect(count($pluck))->toBe(2);
|
expect(count($pluck))->toBe(2);
|
||||||
|
|||||||
@@ -53,6 +53,12 @@ test("model find", function() {
|
|||||||
$row->col1 = "test";
|
$row->col1 = "test";
|
||||||
$res = $row->save();
|
$res = $row->save();
|
||||||
expect($res)->toBe(true);
|
expect($res)->toBe(true);
|
||||||
|
|
||||||
|
$json = json_encode($row);
|
||||||
|
expect($json)->toBeJson();
|
||||||
|
|
||||||
|
$obj = json_decode($json);
|
||||||
|
expect($obj->col1)->toBe("test");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("querybuilder", function() {
|
test("querybuilder", function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user