Skip to content

Commit

Permalink
Fix DiContainerTrait::setDefault /w numeric key (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored May 21, 2022
1 parent 8f2fcf7 commit bec0237
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
container:
image: ghcr.io/mvorisek/image-php:latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
ref: ${{ github.ref }}

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
type: 'StaticAnalysis'
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Configure PHP
run: |
Expand Down Expand Up @@ -84,7 +84,7 @@ jobs:
LOG_COVERAGE: "${{ fromJSON('{true: \"1\", false: \"\"}')[matrix.php == '8.0' && matrix.type == 'Phpunit' && (github.event_name == 'pull_request' || (github.event_name == 'push' && (github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master')))] }}"
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Configure PHP
run: |
Expand Down
6 changes: 3 additions & 3 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Configuration files can be of 4 types : php, php-inline, json, yaml.
Loading can be done in this way :

$object = new Object();
$object->readConfig('config.php','php');
$object->readConfig('config.php', 'php');

After loading, configuration elements can be retrieved in this way :

$object->getConfig('element_key','if not defined use this as default');
$object->getConfig('element_key', 'if not defined use this as default');

if you need an element that is declared inside an array you can use a special syntax :

$object->getConfig('level1_array/level2_array/element_key','if not defined use this as default');
$object->getConfig('level1_array/level2_array/element_key', 'if not defined use this as default');

Element in config can be defined even manually :

Expand Down
4 changes: 2 additions & 2 deletions docs/dynamicmethod.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Introduction
Adds ability to add methods into objects dynamically. That's like a "trait"
feature of a PHP, but implemented in run-time::

$object->addMethod('test', function($o, $args) { echo 'hello, '.$args[0]; });
$object->addMethod('test', function($o, $args) { echo 'hello, ' . $args[0]; });
$object->test('world');

Global Methods
Expand All @@ -21,7 +21,7 @@ implements :php:trait:`HookTrait` then executing $object->test() will also
look for globally-registered method inside the application::

$object->getApp()->addGlobalMethod('test', function($app, $o, $args) {
echo 'hello, '.$args[0];
echo 'hello, ' . $args[0];
});

$object->test('world');
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Hook-based dynamic Methods
dynamically.
That's like a "trait" feature of a PHP, but implemented in run-time::

$object->addMethod('test', function($o, $args) { echo 'hello, '.$args[0]; } );
$object->addMethod('test', function($o, $args) { echo 'hello, ' . $args[0]; } );
$object->test('world');
// outputs: hello, world

Expand Down
2 changes: 1 addition & 1 deletion docs/initializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Declare a object class in your framework::
}

function render() {
return '<input name="'.$this->name.'" value="'.$value.'"/>';
return '<input name="' . $this->name . '" value="' . $value . '"/>';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function readConfig($files = ['config.php'], string $format = 'php')

break;
default:
throw (new Exception('Unknown Format. Allowed formats: php, json, yml.'))
throw (new Exception('Unknown Format. Allowed formats: php, json, yml'))
->addMoreInfo('file', $file)
->addMoreInfo('format', $format);
}
Expand Down
4 changes: 4 additions & 0 deletions src/DiContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ trait DiContainerTrait
public function setDefaults(array $properties, bool $passively = false)
{
foreach ($properties as $key => $val) {
if (is_int($key)) {
$key = (string) $key;
}

if (property_exists($this, $key)) {
if ($passively && $this->{$key} !== null) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected function _factory($seed, array $defaults = null): object
}

if ((!is_array($seed) && !is_object($seed))) {
throw new Exception('Use of non-array seed ($seed type = ' . gettype($seed) . ') is not supported.');
throw new Exception('Use of non-array seed ($seed type = ' . gettype($seed) . ') is not supported');
}

array_unshift($defaults, null); // insert argument 0
Expand Down
2 changes: 1 addition & 1 deletion src/TraitUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function hasTrait($class, string $traitName): bool
// prevent mass use for other than internal use then we can decide
// if we want to keep support this or replace with pure interfaces
if (!str_starts_with($traitName, 'Atk4\Core\\')) {
throw new Exception('Core::hasTrait is not indended for use with other than \Atk4\Core\* traits.');
throw new Exception('Core::hasTrait is not indended for use with other than \Atk4\Core\* traits');
}

if (!isset(self::$_hasTraitCache[$class][$traitName])) {
Expand Down
10 changes: 5 additions & 5 deletions tests/DiContainerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ public function testFromSeed(): void
StdSat2::fromSeed([StdSat::class]);
}

public function testNoPropExNumeric(): void
public function testNoPropExStandard(): void
{
$this->expectException(\Error::class);
$this->expectException(Exception::class);
$m = new FactoryDiMock2();
$m->setDefaults([5 => 'qwerty']);
$m->setDefaults(['not_exist' => 'qwerty']);
}

public function testNoPropExStandard(): void
public function testNoPropExNumeric(): void
{
$this->expectException(Exception::class);
$m = new FactoryDiMock2();
$m->setDefaults(['not_exist' => 'qwerty']);
$m->setDefaults([5 => 'qwerty']);
}

public function testProperties(): void
Expand Down

0 comments on commit bec0237

Please sign in to comment.