Skip to content

Commit c428b4f

Browse files
author
liutao
committed
feat: format
1 parent 662451d commit c428b4f

File tree

7 files changed

+36
-36
lines changed

7 files changed

+36
-36
lines changed

src/Attribute/DataAttribute.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MicroPHP\Data\Attribute;
46

57
use Attribute;

src/Data.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Data implements ArrayAble, ArrayAccess, JsonSerializable
2323
protected ?ReflectionClass $_staticReflection = null;
2424

2525
/**
26+
* @param mixed $data
2627
* @throws
2728
*/
2829
public function __construct($data = [])
@@ -40,14 +41,23 @@ public function __debugInfo(): ?array
4041
return $this->objectToArray($this, false);
4142
}
4243

44+
public function __serialize(): array
45+
{
46+
$data = [];
47+
foreach ($this->getStaticReflection()->getProperties() as $property) {
48+
if (! $this->isInsideProperty($property)) {
49+
$data[$property->getName()] = $property->getValue($this);
50+
}
51+
}
52+
return $data;
53+
}
4354

4455
public static function from($data): static
4556
{
4657
return new static($data);
4758
}
4859

4960
/**
50-
* @param array $data
5161
* @return $this
5262
* @throws
5363
*/
@@ -62,17 +72,17 @@ public function fill(array $data): static
6272
$camelCasePropertyName = Str::camel($propertyName);
6373
$snakePropertyName = Str::snake($propertyName);
6474
if (
65-
!array_key_exists($camelCasePropertyName, $data)
66-
&& !array_key_exists($snakePropertyName, $data)
67-
&& !$property->isInitialized($this)
75+
! array_key_exists($camelCasePropertyName, $data)
76+
&& ! array_key_exists($snakePropertyName, $data)
77+
&& ! $property->isInitialized($this)
6878
) {
6979
throw new Exception("Property {$property->getName()} is not set in : " . get_class($this));
7080
}
7181
$type = $property->getType();
7282
$value = $data[$camelCasePropertyName] ?? ($data[$snakePropertyName] ?? null);
7383
if ($type instanceof ReflectionUnionType || $type instanceof ReflectionIntersectionType) {
7484
$property->setValue($this, $value);
75-
} elseif ($type->isBuiltin() && !is_null($value)) {
85+
} elseif ($type->isBuiltin() && ! is_null($value)) {
7686
$property->setValue($this, $value);
7787
} elseif (PHP_VERSION_ID > 80100 && enum_exists($type->getName())) {
7888
if (is_int($value) || is_string($value)) {
@@ -132,7 +142,7 @@ protected function propertyToArray(object $object, bool $toSnake, ReflectionProp
132142
protected function forValue(mixed $value, bool $toSnake)
133143
{
134144
if (is_array($value)) {
135-
return array_map(fn($item) => $this->forValue($item, $toSnake), $value);
145+
return array_map(fn ($item) => $this->forValue($item, $toSnake), $value);
136146
}
137147
if (is_object($value)) {
138148
if (PHP_VERSION_ID > 80100 && enum_exists(get_class($value))) {
@@ -142,7 +152,6 @@ protected function forValue(mixed $value, bool $toSnake)
142152
return $value->toArray();
143153
}
144154
return $this->objectToArray($value, $toSnake);
145-
146155
}
147156
return $value;
148157
}
@@ -183,18 +192,5 @@ protected function beforeFill(array $data): array
183192
return $data;
184193
}
185194

186-
protected function afterFill(array $data)
187-
{
188-
}
189-
190-
public function __serialize(): array
191-
{
192-
$data = [];
193-
foreach ($this->getStaticReflection()->getProperties() as $property) {
194-
if (!$this->isInsideProperty($property)) {
195-
$data[$property->getName()] = $property->getValue($this);
196-
}
197-
}
198-
return $data;
199-
}
200-
}
195+
protected function afterFill(array $data) {}
196+
}

src/Traits/ArrayAccessTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MicroPHP\Data\Traits;
46

57
use ReturnTypeWillChange;

src/Util/Str.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace MicroPHP\Data\Util;
56

6-
77
class Str
88
{
99
private static array $_strCache = [];
@@ -21,7 +21,7 @@ public static function snake(string $str, string $delimiter = '_'): string
2121
if (isset(static::$_strCache['snake'][$str])) {
2222
return static::$_strCache['snake'][$str];
2323
}
24-
if (!ctype_lower($str)) {
24+
if (! ctype_lower($str)) {
2525
$str = preg_replace('/\s+/u', '', ucwords($str));
2626

2727
$str = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $str));
@@ -30,7 +30,7 @@ public static function snake(string $str, string $delimiter = '_'): string
3030
return static::$_strCache['snake'][$str] = $str;
3131
}
3232

33-
public static function lower($value): array|bool|string|null
33+
public static function lower($value): null|array|bool|string
3434
{
3535
return mb_strtolower($value, 'UTF-8');
3636
}
@@ -50,4 +50,4 @@ public static function studly(string $value, string $gap = ''): string
5050

5151
return str_replace(' ', $gap, $value);
5252
}
53-
}
53+
}

tests/Feature/DataTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use MicroPHP\Data\Attribute\DataAttribute;
46
use MicroPHP\Data\Data;
57
use MicroPHP\Data\Util\Str;
68

79
test('data', function () {
8-
910
$value = ['name' => '123', 'phones' => [1, 2], 'b' => ['name' => 'hello'], 'address_detail' => 'haa'];
1011
$a = A::from($value);
1112

@@ -38,7 +39,6 @@
3839
->toEqual($value['name']);
3940
});
4041

41-
4242
test('union type', function () {
4343
expect(Str::snake('HelloWorld'))->toBe('hello_world')
4444
->and(Str::camel('hello_world'))->toBe('helloWorld')
@@ -53,7 +53,6 @@
5353
$c = serialize($c);
5454
$c = unserialize($c);
5555
expect($c->a)->toBe(enumA::A);
56-
5756
});
5857

5958
it('throws exception', function () {
@@ -74,6 +73,7 @@ class A extends Data
7473
public string $name;
7574

7675
public array $phones;
76+
7777
public B $b;
7878

7979
public string $addressDetail;
@@ -82,7 +82,6 @@ class A extends Data
8282
class B extends Data
8383
{
8484
public string $name;
85-
8685
}
8786

8887
class C extends Data
@@ -99,7 +98,7 @@ enum enumA: string
9998

10099
class D extends Data
101100
{
102-
public string|int $name;
101+
public int|string $name;
103102
}
104103

105104
class E extends Data
@@ -111,4 +110,4 @@ public function __construct(string $name)
111110
$this->name = $name;
112111
parent::__construct();
113112
}
114-
}
113+
}

tests/Pest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
|--------------------------------------------------------------------------
57
| Test Case

tests/TestCase.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Tests;
46

57
use PHPUnit\Framework\TestCase as BaseTestCase;
68

7-
abstract class TestCase extends BaseTestCase
8-
{
9-
//
10-
}
9+
abstract class TestCase extends BaseTestCase {}

0 commit comments

Comments
 (0)