Skip to content

Commit

Permalink
Merge pull request #76 from jeyroik/v6
Browse files Browse the repository at this point in the history
added setParamValue method
  • Loading branch information
jeyroik authored Jul 16, 2023
2 parents d055711 + 1b5dd3e commit 1c3c280
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 6.22.0

- Added method `IHaveParams::setParamValue(string $paramName, mixed $value): static`.

# 6.21.3

- Added output for already existing records on `updateAndThrowIfExist(...)`.
Expand Down
8 changes: 7 additions & 1 deletion src/components/installers/InstallerEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
use extas\components\exceptions\AlreadyExist;
use extas\components\Plugins;
use extas\components\SystemContainer;
use extas\components\THasOutput;
use extas\interfaces\IHaveOutput;
use extas\interfaces\repositories\IRepository;
use extas\interfaces\stages\IStageBeforeInstallEntity;
use extas\interfaces\stages\IStageIsToInstallEntity;

class InstallerEntities
class InstallerEntities implements IHaveOutput
{
use THasOutput;

protected const FIELD__NAME = 'name';

protected array $app = [];
Expand Down Expand Up @@ -84,5 +88,7 @@ protected function installTable(string $tableName, array $entities): void
continue;
}
}

$this->appendOutput($repo->getOutput(), $tableName . '_repo');
}
}
14 changes: 14 additions & 0 deletions src/components/parameters/THasParams.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace extas\components\parameters;

use extas\components\exceptions\MissedOrUnknown;
use extas\interfaces\parameters\IHaveParams;
use extas\interfaces\parameters\IParam;
use extas\interfaces\parameters\IParams;
Expand Down Expand Up @@ -39,4 +40,17 @@ public function getParamsValues(): array
{
return array_column($this->getParams(), IParam::FIELD__VALUE, IParam::FIELD__NAME);
}

public function setParamValue(string $paramName, mixed $value): static
{
$params = $this->buildParams();

if (!$params->hasOne($paramName)) {
throw new MissedOrUnknown('parameter "' . $paramName . '"');
}

$this->config[IHaveParams::FIELD__PARAMS][$paramName][IParam::FIELD__VALUE] = $value;

return $this;
}
}
15 changes: 3 additions & 12 deletions src/components/repositories/RepoItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,13 @@ public static function updateAndThrowIfExist(IRepository $repo, IHaveConfig &$it
$exists = $repo->one($where);

if ($exists) {
$changed = false;
$repo->appendOutput(['Record already exists:' . print_r($exists->__toArray(), true)], 'repo-item');
foreach ($item as $field => $value) {
if ($exists[$field] == $value) {
continue;
}

$repo->appendOutput(['replace "' . $field . '" with ' . print_r($value, true)], 'repo-item');
$exists[$field] = $value;
$changed = true;
}
if ($changed) {
$repo->update($exists);
$repo->appendOutput(['Update existing record, new state is ' . print_r($exists->__toArray(), true)], 'repo-item');
}

$repo->update($exists);
$repo->appendOutput(['Updated existing record, new state is ' . print_r($exists->__toArray(), true)], 'repo-item');

throw new AlreadyExist($repo->getName());
}
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/parameters/IHaveParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public function setParams(array $params): static;
public function addParam(IParam $param): static;
public function buildParams(): IParams;
public function getParamsValues(): array;
public function setParamValue(string $paramName, mixed $value): static;
}
5 changes: 4 additions & 1 deletion tests/parameters/ParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testParams()
IParametred::FIELD__NAME => 'p1',
IParametred::FIELD__PARAMS => [
'par1' => [
'name' => 'par1'
IParam::FIELD__NAME => 'par1'
]
]
]
Expand Down Expand Up @@ -105,5 +105,8 @@ protected function getSubjectForExtension(): string
]);

$this->assertCount(1, $withParams->getParams());

$withParams->setParamValue('test3', 'new_test_value_3');
$this->assertEquals('new_test_value_3', $withParams->buildParams()->buildOne('test3')->getValue());
}
}
29 changes: 25 additions & 4 deletions tests/toolkit/RepoItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ public function testUpdateAndThrowIfExist(): void
$obj = new class ([
'id' => 1,
'test' => 1,
'other' => 1
'other' => [
'sub' => [
'value' => 1
]
]
]) extends Item{
protected function getSubjectForExtension(): string
{
Expand All @@ -90,15 +94,32 @@ protected function getSubjectForExtension(): string
$repo = $obj->tests();
$repo->create($obj);

$obj['other'] = 2;
$obj2 = new class ([
'test' => 1,
'other' => [
'sub' => [
'value' => 2
]
]
]) extends Item{
protected function getSubjectForExtension(): string
{
return '';
}
};

$catched = false;

try {
RepoItem::updateAndThrowIfExist($repo, $obj, ['test']);
RepoItem::updateAndThrowIfExist($repo, $obj2, ['test']);
} catch(AlreadyExist $e) {
$this->assertEquals('Tests already exists', $e->getMessage());
$objFromDb = $repo->one(['test' => 1]);
$this->assertEquals(2, $objFromDb['other']);
$this->assertEquals(2, $objFromDb['other']['sub']['value']);
$this->assertEquals(['repo-item: Updated existing record, new state is '.print_r($objFromDb->__toArray(), true)], $repo->getOutput());
$catched = true;
}
$this->assertTrue($catched);
$this->deleteRepo('tests');
}

Expand Down

0 comments on commit 1c3c280

Please sign in to comment.