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

Normalize array persistence seed data #710

Merged
merged 3 commits into from
Apr 21, 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
88 changes: 70 additions & 18 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
class Array_ extends Persistence
{
/** @var array */
private $data;
private $seedData;

public function __construct(array $data = [])
{
$this->data = $data;
}
/** @var array<string, array> */
private $data;

/**
* Array of last inserted ids per table.
Expand All @@ -31,6 +29,52 @@ public function __construct(array $data = [])
*/
protected $lastInsertIds = [];

public function __construct(array $data = [])
{
$this->seedData = $data;

// if there is no model table specified, then create fake one named 'data'
// and put all persistence data in there 1/2
if (count($this->seedData) > 0 && !isset($this->seedData['data'])) {
$rowSample = reset($this->seedData);
if (is_array($rowSample) && !is_array(reset($rowSample))) {
$this->seedData = ['data' => $this->seedData];
}
}
}

private function seedData(Model $model): void
{
if (isset($this->data[$model->table])) {
return;
}

$this->data[$model->table] = [];

if (isset($this->seedData[$model->table])) {
$rows = $this->seedData[$model->table];
unset($this->seedData[$model->table]);

foreach ($rows as $id => $row) {
$this->saveRow($model, $row, $id);
}
}

// for array persistence join which accept table directly (without model initialization)
foreach ($model->getFields() as $field) {
if ($field->hasJoin()) {
$join = $field->getJoin();
$joinTable = \Closure::bind(function () use ($join) {
return $join->foreign_table;
}, null, Array_\Join::class)();
if (isset($this->seedData[$joinTable])) {
$dummyJoinModel = new Model($this, ['table' => $joinTable]);
$this->add($dummyJoinModel);
}
}
}
}

/**
* @deprecated TODO temporary for these:
* - https://github.com/atk4/data/blob/90ab68ac063b8fc2c72dcd66115f1bd3f70a3a92/src/Reference/ContainsOne.php#L119
Expand All @@ -39,6 +83,8 @@ public function __construct(array $data = [])
*/
public function getRawDataByTable(Model $model, string $table): array
{
$this->seedData($model);

$rows = [];
foreach ($this->data[$table] as $id => $row) {
$this->addIdToLoadRow($model, $row, $id);
Expand Down Expand Up @@ -114,32 +160,28 @@ public function add(Model $model, array $defaults = []): Model

$model = parent::add($model, $defaults);

// if there is no model table specified, then create fake one named 'data'
// and put all persistence data in there 2/2
if (!$model->table) {
$model->table = 'data';
}

if ($model->id_field && $model->hasField($model->id_field)) {
$f = $model->getField($model->id_field);
if (!$f->type) {
$f->type = 'integer';
}
}

// if there is no model table specified, then create fake one named 'data'
// and put all persistence data in there
if (!$model->table) {
$model->table = 'data'; // fake table name 'data'
if (!isset($this->data[$model->table]) || count($this->data) !== 1) {
$this->data = [$model->table => $this->data];
}
}

// if there is no such table in persistence, then create empty one
if (!isset($this->data[$model->table])) {
$this->data[$model->table] = [];
}
$this->seedData($model);

return $model;
}

public function tryLoad(Model $model, $id): ?array
{
$this->seedData($model);

if (!isset($this->data[$model->table])) {
throw (new Exception('Table was not found in the array data source'))
->addMoreInfo('table', $model->table);
Expand Down Expand Up @@ -179,6 +221,8 @@ public function tryLoad(Model $model, $id): ?array
*/
public function insert(Model $model, array $data)
{
$this->seedData($model);

$data = $this->typecastSaveRow($model, $data);

$id = $data[$model->id_field] ?? $this->generateNewId($model);
Expand All @@ -197,6 +241,8 @@ public function insert(Model $model, array $data)
*/
public function update(Model $model, $id, array $data)
{
$this->seedData($model);

$data = $this->typecastSaveRow($model, $data);

$this->saveRow($model, array_merge($this->data[$model->table][$id] ?? [], $data), $id);
Expand All @@ -211,6 +257,8 @@ public function update(Model $model, $id, array $data)
*/
public function delete(Model $model, $id)
{
$this->seedData($model);

unset($this->data[$model->table][$id]);
}

Expand All @@ -221,6 +269,8 @@ public function delete(Model $model, $id)
*/
public function generateNewId(Model $model)
{
$this->seedData($model);

$type = $model->id_field ? $model->getField($model->id_field)->type : 'integer';

switch ($type) {
Expand Down Expand Up @@ -286,6 +336,8 @@ public function export(Model $model, array $fields = null, bool $typecast = true
*/
public function initAction(Model $model, array $fields = null): \Atk4\Data\Action\Iterator
{
$this->seedData($model);

$data = $this->data[$model->table];
array_walk($data, function (&$row, $id) use ($model) {
$this->addIdToLoadRow($model, $row, $id);
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Static_.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(array $data = null)
if (!is_array($row1)) {
// convert array of strings into array of hashes
foreach ($data as $k => $str) {
$data[$k] = ['id' => $k, 'name' => $str];
$data[$k] = ['name' => $str];
}
unset($str);

Expand Down
64 changes: 32 additions & 32 deletions tests/JoinArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ public function testJoinLoading()
{
$db = new Persistence\Array_([
'user' => [
1 => ['id' => 1, 'name' => 'John', 'contact_id' => 1],
2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1],
3 => ['id' => 3, 'name' => 'Joe', 'contact_id' => 2],
1 => ['name' => 'John', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'Joe', 'contact_id' => 2],
], 'contact' => [
1 => ['id' => 1, 'contact_phone' => '+123'],
2 => ['id' => 2, 'contact_phone' => '+321'],
1 => ['contact_phone' => '+123'],
2 => ['contact_phone' => '+321'],
],
]);
$m_u = new Model($db, ['table' => 'user']);
Expand Down Expand Up @@ -235,12 +235,12 @@ public function testJoinUpdate()
{
$db = new Persistence\Array_([
'user' => [
1 => ['id' => 1, 'name' => 'John', 'contact_id' => 1],
2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1],
3 => ['id' => 3, 'name' => 'Joe', 'contact_id' => 2],
1 => ['name' => 'John', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'Joe', 'contact_id' => 2],
], 'contact' => [
1 => ['id' => 1, 'contact_phone' => '+123'],
2 => ['id' => 2, 'contact_phone' => '+321'],
1 => ['contact_phone' => '+123'],
2 => ['contact_phone' => '+321'],
],
]);
$m_u = new Model($db, ['table' => 'user']);
Expand All @@ -257,11 +257,11 @@ public function testJoinUpdate()
$this->assertSame([
'user' => [
1 => ['name' => 'John 2', 'contact_id' => 1],
2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1],
3 => ['id' => 3, 'name' => 'Joe', 'contact_id' => 2],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'Joe', 'contact_id' => 2],
], 'contact' => [
1 => ['contact_phone' => '+555'],
2 => ['id' => 2, 'contact_phone' => '+321'],
2 => ['contact_phone' => '+321'],
],
], $this->getInternalPersistenceData($db));

Expand All @@ -273,7 +273,7 @@ public function testJoinUpdate()
$this->assertSame([
'user' => [
1 => ['name' => 'John 2', 'contact_id' => 1],
2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
], 'contact' => [
1 => ['contact_phone' => '+555'],
Expand All @@ -289,7 +289,7 @@ public function testJoinUpdate()
$this->assertEquals([
'user' => [
1 => ['name' => 'John 2', 'contact_id' => 1],
2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
4 => ['name' => 'YYY', 'contact_id' => 3],
], 'contact' => [
Expand All @@ -304,14 +304,14 @@ public function testJoinDelete()
{
$db = new Persistence\Array_([
'user' => [
1 => ['id' => 1, 'name' => 'John 2', 'contact_id' => 1],
2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1],
3 => ['id' => 3, 'name' => 'XX', 'contact_id' => 2],
4 => ['id' => 4, 'name' => 'YYY', 'contact_id' => 3],
1 => ['name' => 'John 2', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
4 => ['name' => 'YYY', 'contact_id' => 3],
], 'contact' => [
1 => ['id' => 1, 'contact_phone' => '+555'],
2 => ['id' => 2, 'contact_phone' => '+999'],
3 => ['id' => 3, 'contact_phone' => '+777'],
1 => ['contact_phone' => '+555'],
2 => ['contact_phone' => '+999'],
3 => ['contact_phone' => '+777'],
],
]);
$m_u = new Model($db, ['table' => 'user']);
Expand All @@ -325,12 +325,12 @@ public function testJoinDelete()

$this->assertSame([
'user' => [
2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1],
3 => ['id' => 3, 'name' => 'XX', 'contact_id' => 2],
4 => ['id' => 4, 'name' => 'YYY', 'contact_id' => 3],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
4 => ['name' => 'YYY', 'contact_id' => 3],
], 'contact' => [
2 => ['id' => 2, 'contact_phone' => '+999'],
3 => ['id' => 3, 'contact_phone' => '+777'],
2 => ['contact_phone' => '+999'],
3 => ['contact_phone' => '+777'],
],
], $this->getInternalPersistenceData($db));
}
Expand All @@ -339,12 +339,12 @@ public function testLoadMissing()
{
$db = new Persistence\Array_([
'user' => [
2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1],
3 => ['id' => 3, 'name' => 'XX', 'contact_id' => 2],
4 => ['id' => 4, 'name' => 'YYY', 'contact_id' => 3],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
4 => ['name' => 'YYY', 'contact_id' => 3],
], 'contact' => [
2 => ['id' => 2, 'contact_phone' => '+999'],
3 => ['id' => 3, 'contact_phone' => '+777'],
2 => ['contact_phone' => '+999'],
3 => ['contact_phone' => '+777'],
],
]);
$m_u = new Model($db, ['table' => 'user']);
Expand Down
Loading