Skip to content

Commit

Permalink
up: update obj box and some str util
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 20, 2024
1 parent 99803f7 commit f25aa92
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
65 changes: 57 additions & 8 deletions src/Obj/ObjectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class ObjectBox implements ContainerInterface
private bool $callInit = true;

/**
* @var string
* @var string Will call init method on object create. Default is 'initObj()'
*/
private string $initMethod = 'init';
private string $initMethod = 'initObj';

/**
* Created objects
Expand All @@ -70,9 +70,57 @@ class ObjectBox implements ContainerInterface
private array $objects = [];

/**
* Definitions for create objects
* Definitions for create objects.
*
* For definition format:
*
* - Closure(ObjectBox): object
* - Object and has __invoke()
* - string: an function name
* - array: callable array [class, method]
* - array: config array for create object
* - more, any type as config data.
*
* ```php
* [
* // array config of definition
* 'myObj' => [
* 'class' => MyObj::class,
* '__opt' => [
* 'callInit' => true, // call init method on object create. Default is true
* 'objType' => ObjectBox::TYPE_SINGLETON, // default is TYPE_SINGLETON
* 'argsForNew' => [
* 'foo' => 'bar',
* ],
* ],
* // props settings ...
* 'propName' => value,
* ],
* // use creator func
* 'myObj2' => [
* '__creator' => function (ObjectBox $box) {
* return new MyObj2();
* },
* '__opt' => [],
* // props settings ...
* 'propName' => value,
* ],
* // use [class, method] of definition
* 'myObj3' => [MyObj3::class, 'createObj3'],
* // function name of definition
* 'myObj4' => 'my_create_func',
* // Closure object
* 'myObj5' => function (ObjectBox $box) {
* return new MyObj5();
* },
* // callable object: has __invoke() method
* 'myObj6' => new Obj6Factory(),
* ]
*
* ```
*
* @var array
* @see createObject()
*/
private array $definitions = [];

Expand Down Expand Up @@ -108,10 +156,11 @@ public function get(string $id): mixed

if (is_object($obj)) {
$callInit = $opt['callInit'] ?? $this->callInit;
$initMth = $this->initMethod;

// has init method in the object, call it.
if ($callInit && method_exists($obj, $this->initMethod)) {
$obj->init();
if ($callInit && $initMth && method_exists($obj, $initMth)) {
$obj->$initMth();
}

// storage it on type is TYPE_SINGLETON
Expand All @@ -130,7 +179,7 @@ public function get(string $id): mixed
/**
* Create object from definition.
*
* return options:
* return [obj, options], options:
*
* ```php
* [
Expand Down Expand Up @@ -188,7 +237,7 @@ protected function createObject(mixed $value): array
}
}

// as config data.
// save as config data.
if ($obj === null) {
$obj = $value;
}
Expand All @@ -209,7 +258,7 @@ protected function createObject(mixed $value): array
*
* ```php
* [
* 'class' => class-string,
* 'class' => 'class-string',
* // '__creator' => callable(ObjectBox): object, // can also use creator func.
*
* // option for create object.
Expand Down
12 changes: 12 additions & 0 deletions src/Str/Traits/StringTruncateHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ trait StringTruncateHelperTrait
/// Truncate
////////////////////////////////////////////////////////////////////////

/**
* @param string $s
* @param string $start
* @param int|null $length
*
* @return string
*/
public static function afterStart(string $s, string $start, ?int $length = null): string
{
return substr($s, strlen($start), $length);
}

/**
* @param string $str
* @param int $start
Expand Down
2 changes: 1 addition & 1 deletion src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @package Toolkit\Stdlib
* @see \gettype()
*/
final class Type
class Type
{
// ------ basic types ------

Expand Down

0 comments on commit f25aa92

Please sign in to comment.