Skip to content

Commit

Permalink
feat: add some new string utl func and update base object
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 22, 2024
1 parent f25aa92 commit 4c8bc02
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 21 deletions.
11 changes: 11 additions & 0 deletions src/Obj/AbstractDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Toolkit\Stdlib\Obj;

/**
* Class AbstractDTO
*/
abstract class AbstractDTO extends BaseObject
{

}
33 changes: 17 additions & 16 deletions src/Obj/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ abstract class BaseObject implements JsonSerializable
use QuickInitTrait;

/**
* Field alias mapping. [alias => field]
* JSON field mapping. Format: [json-field => field]
*
* @var array
*/
protected array $_aliasMap = [];
protected array $_jsonMap = [];

/**
* Class constructor.
Expand Down Expand Up @@ -58,12 +58,12 @@ protected function init(): void
* - Automatically convert fields to camelCase format
*
* @param array $data
* @param array $aliasMap
* @param array $jsonMap json field mapping
*/
public function load(array $data, array $aliasMap = []): void
public function load(array $data, array $jsonMap = []): void
{
if ($data) {
Obj::init($this, $data, true, $aliasMap ?: $this->_aliasMap);
Obj::init($this, $data, true, $jsonMap ?: $this->_jsonMap);
}
}

Expand All @@ -76,7 +76,7 @@ public function load(array $data, array $aliasMap = []): void
*/
public function setValue(string $field, mixed $value): static
{
Obj::init($this, [$field => $value], true, $this->_aliasMap);
Obj::init($this, [$field => $value], true, $this->_jsonMap);
return $this;
}

Expand All @@ -85,19 +85,20 @@ public function setValue(string $field, mixed $value): static
*/
public function toArray(): array
{
return Obj::toArray($this, false, false);
return $this->convToMap(true, true);
}

/**
* Convert to array map.
*
* @param bool $filter
* @param bool $filter exclude empty value
* @param bool $useJsonMap use json field map
*
* @return array
*/
public function toMap(bool $filter = false): array
public function toMap(bool $filter = false, bool $useJsonMap = false): array
{
return $this->convToMap($filter);
return $this->convToMap($filter, $useJsonMap);
}

/**
Expand All @@ -107,7 +108,7 @@ public function toMap(bool $filter = false): array
*/
public function toCleaned(): array
{
return $this->convToMap(true);
return $this->convToMap(true, true);
}

/**
Expand Down Expand Up @@ -135,8 +136,8 @@ protected function convToMap(bool $filter = false, bool $aliased = false): array
{
$data = [];
$full = get_object_vars($this);
if ($aliased && $this->_aliasMap) {
$this->_name2alias = array_flip($this->_aliasMap);
if ($aliased && $this->_jsonMap) {
$this->_name2alias = array_flip($this->_jsonMap);
}

// filter empty value
Expand Down Expand Up @@ -211,11 +212,11 @@ protected function convToMap(bool $filter = false, bool $aliased = false): array
* 转成 JSON 字符串
*
* @param bool $filter filter empty value
* @param bool $aliased
* @param bool $aliased use json field map
*
* @return string
*/
public function toJson(bool $filter = true, bool $aliased = false): string
public function toJson(bool $filter = true, bool $aliased = true): string
{
return Json::unescaped($this->convToMap($filter, $aliased));
}
Expand All @@ -233,7 +234,7 @@ public function __toString(): string
*/
public function jsonSerialize(): array
{
return $this->toMap(true);
return $this->convToMap(true, true);
}

}
1 change: 1 addition & 0 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
*/
final class Str extends StringHelper
{

}
70 changes: 69 additions & 1 deletion src/Str/Traits/StringTruncateHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,41 @@ trait StringTruncateHelperTrait
/// Truncate
////////////////////////////////////////////////////////////////////////

/**
* @param string $s
* @param array $prefixes
*
* @return string
*/
public static function removePrefixes(string $s, array $prefixes): string
{
foreach ($prefixes as $prefix) {
if (str_starts_with($s, $prefix)) {
return substr($s, strlen($prefix));
}
}
return $s;
}

/**
* @param string $s
* @param array $suffixes
*
* @return string
*/
public static function removeSuffixes(string $s, array $suffixes): string
{
foreach ($suffixes as $suffix) {
if (str_ends_with($s, $suffix)) {
return substr($s, 0, -strlen($suffix));
}
}
return $s;
}

/**
* @param string $s
* @param string $start
* @param string $start prefix string
* @param int|null $length
*
* @return string
Expand All @@ -53,6 +85,42 @@ public static function afterStart(string $s, string $start, ?int $length = null)
return substr($s, strlen($start), $length);
}

/**
* get substring before $sub.
* eg: s='hello/world', sub='/' => 'hello'
*
* @param string $s
* @param string $sub
* @param string $fallback
*
* @return string
*/
public static function before(string $s, string $sub, string $fallback = ''): string
{
if ($i = strpos($s, $sub)) {
return substr($s, 0, $i);
}
return $fallback;
}

/**
* get substring after $sub.
* eg: s='hello/world', sub='/' => 'world'
*
* @param string $s
* @param string $sub
* @param string $fallback
*
* @return string
*/
public static function after(string $s, string $sub, string $fallback = ''): string
{
if (($i = strpos($s, $sub)) !== false) {
return substr($s, $i + strlen($sub));
}
return $fallback;
}

/**
* @param string $str
* @param int $start
Expand Down
23 changes: 21 additions & 2 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public static function getDefault(string $type): float|bool|int|array|string|nul
return match ($type) {
self::INT, self::INTEGER => 0,
self::BOOL, self::BOOLEAN => false,
self::FLOAT => (float)0,
self::DOUBLE => (double)0,
self::FLOAT, self::DOUBLE => 0.0,
self::STRING => '',
self::ARRAY => [],
default => null,
Expand Down Expand Up @@ -188,4 +187,24 @@ public static function complexes(): array
self::RESOURCE,
];
}

/**
* @param string $type
*
* @return bool
*/
public static function isInt(string $type): bool
{
return in_array($type, [self::INT, self::INTEGER], true);
}

/**
* @param string $type
*
* @return bool
*/
public static function isComplex(string $type): bool
{
return in_array($type, self::complexes(), true);
}
}
2 changes: 1 addition & 1 deletion test/Obj/ADemoPo2.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class ADemoPo2 extends AbstractObj
{
protected array $_aliasMap = [
protected array $_jsonMap = [
'ot2' => 'oneTwo2',
];

Expand Down
2 changes: 1 addition & 1 deletion test/Obj/BaseObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function testSubArray(): void
$this->assertArrayNotHasKey('str1', $subItem);
}

public function testUseAlias(): void
public function testUseJsonMap(): void
{
// use snake
$po = new ADemoPo2(['one_two' => 'abc']);
Expand Down
10 changes: 10 additions & 0 deletions test/Str/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,14 @@ public function testRenderVars(): void
$text = Str::renderVars('tags: ${ tags }', $vars, '${%s}');
$this->assertEquals('tags: [php, java]', $text);
}

public function testBeforeAfter(): void
{
$s = 'hello/world';
$this->assertEquals('hello', Str::before($s, '/'));
$this->assertEquals('some', Str::before($s, '+', 'some'));

$this->assertEquals('world', Str::after($s, '/'));
$this->assertEquals('some', Str::after($s, '+', 'some'));
}
}

0 comments on commit 4c8bc02

Please sign in to comment.