Skip to content

Commit 5024e8f

Browse files
committed
init
1 parent aa30355 commit 5024e8f

57 files changed

Lines changed: 2855 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.buildpath
2+
.settings/
3+
.project
4+
*.patch
5+
.idea/
6+
.git/
7+
runtime/
8+
tests/.phpunit/
9+
vendor/
10+
.phpintel/
11+
.env
12+
.DS_Store
13+
.phpunit*
14+
*.cache
15+
.vscode/
16+
*core
17+
*.override.*
18+
composer.lock
19+
*.log
20+
.output.txt

compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
app:
3+
container_name: morph-app
4+
image: php:8.3
5+
volumes:
6+
- ./:/opt/www
7+
environment:
8+
- SONAR_TOKEN=${SONAR_TOKEN:-undefined}
9+
entrypoint: [ 'tail', '-f', '/dev/null' ]
10+
restart: on-failure

composer.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "devitools/morph",
3+
"type": "library",
4+
"license": "MIT",
5+
"keywords": [
6+
"php"
7+
],
8+
"description": "The definitive serializer and deserializer for PHP",
9+
"autoload": {
10+
"psr-4": {
11+
"Morph\\": "src/"
12+
},
13+
"files": [
14+
"functions/notation.php"
15+
]
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"Morph\\Test\\": "tests/"
20+
}
21+
},
22+
"require": {
23+
"php": "^8.3",
24+
"ext-json": "*",
25+
"jawira/case-converter": "^3.5"
26+
},
27+
"require-dev": {
28+
"bnf/phpstan-psr-container": "^1.1",
29+
"deptrac/deptrac": "^3.0",
30+
"php-mock/php-mock-phpunit": "^2.12",
31+
"phpmd/phpmd": "^2.15",
32+
"phpstan/phpstan": "^2",
33+
"phpunit/phpunit": "^10.5",
34+
"rector/rector": "^2",
35+
"roave/security-advisories": "dev-latest",
36+
"robiningelbrecht/phpunit-pretty-print": "^1.3",
37+
"squizlabs/php_codesniffer": "^3.11",
38+
"vimeo/psalm": "^5.26"
39+
},
40+
"minimum-stability": "dev",
41+
"prefer-stable": true,
42+
"config": {
43+
"optimize-autoloader": true,
44+
"sort-packages": true
45+
}
46+
}

functions/cast.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Serendipity\Type\Cast;
6+
7+
use Stringable;
8+
9+
if (! function_exists(__NAMESPACE__ . '\arrayify')) {
10+
/**
11+
* @template T of array-key
12+
* @template U
13+
* @param array<T, U> $default
14+
* @return array<T, U>
15+
*/
16+
function arrayify(mixed $value, array $default = []): array
17+
{
18+
return is_array($value) ? $value : $default;
19+
}
20+
}
21+
22+
if (! function_exists(__NAMESPACE__ . '\mapify')) {
23+
/**
24+
* @param array<string, mixed> $default
25+
* @return array<string, mixed>
26+
*/
27+
function mapify(mixed $data, array $default = []): array
28+
{
29+
$data = match (true) {
30+
is_object($data) => (array) $data,
31+
is_array($data) => $data,
32+
default => $default,
33+
};
34+
$mapping = [];
35+
foreach ($data as $key => $datum) {
36+
$key = is_string($key) ? $key : sprintf('key_%s', $key);
37+
$mapping[$key] = $datum;
38+
}
39+
return $mapping;
40+
}
41+
}
42+
43+
if (! function_exists(__NAMESPACE__ . '\stringify')) {
44+
function stringify(mixed $value, string $default = ''): string
45+
{
46+
return match (true) {
47+
is_string($value) => $value,
48+
is_scalar($value) => (string) $value,
49+
(is_object($value) && method_exists($value, '__toString')) => (string) $value,
50+
$value instanceof Stringable => $value->__toString(),
51+
default => $default,
52+
};
53+
}
54+
}
55+
56+
if (! function_exists(__NAMESPACE__ . '\integerify')) {
57+
function integerify(mixed $value, int $default = 0): int
58+
{
59+
$value = is_numeric($value) ? (int) $value : $value;
60+
return is_int($value) ? $value : $default;
61+
}
62+
}
63+
64+
if (! function_exists(__NAMESPACE__ . '\floatify')) {
65+
function floatify(mixed $value, float $default = 0.0): float
66+
{
67+
$value = is_numeric($value) ? (float) $value : $value;
68+
return is_float($value) ? $value : $default;
69+
}
70+
}
71+
72+
if (! function_exists(__NAMESPACE__ . '\boolify')) {
73+
function boolify(mixed $value, bool $default = false): bool
74+
{
75+
return is_bool($value) ? $value : $default;
76+
}
77+
}

functions/notation.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Serendipity\Notation;
6+
7+
use Jawira\CaseConverter\Convert;
8+
use Morph\Support\Reflective\Notation;
9+
10+
use function Serendipity\Type\Cast\stringify;
11+
12+
if (! function_exists(__NAMESPACE__ . '\format')) {
13+
function format(string $string, Notation $notation): string
14+
{
15+
return match ($notation) {
16+
Notation::SNAKE => snakify($string),
17+
Notation::CAMEL => camelify($string),
18+
Notation::PASCAL => pascalify($string),
19+
Notation::ADA => adaify($string),
20+
Notation::MACRO => macroify($string),
21+
Notation::KEBAB => kebabify($string),
22+
Notation::TRAIN => trainify($string),
23+
Notation::COBOL => cobolify($string),
24+
Notation::LOWER => lowerify($string),
25+
Notation::UPPER => upperify($string),
26+
Notation::TITLE => titlelify($string),
27+
Notation::SENTENCE => sentencify($string),
28+
Notation::DOT => dotify($string),
29+
Notation::NONE => $string,
30+
};
31+
}
32+
}
33+
34+
if (! function_exists(__NAMESPACE__ . '\snakify')) {
35+
function snakify(string $string, bool $includeDigits = true): string
36+
{
37+
$string = (new Convert($string))->toSnake();
38+
if ($includeDigits) {
39+
return ltrim(stringify(preg_replace('/(\d+)/', '_$0', $string)), '_');
40+
}
41+
return $string;
42+
}
43+
}
44+
45+
if (! function_exists(__NAMESPACE__ . '\camelify')) {
46+
function camelify(string $string): string
47+
{
48+
return (new Convert($string))->toCamel();
49+
}
50+
}
51+
52+
if (! function_exists(__NAMESPACE__ . '\pascalify')) {
53+
function pascalify(string $string): string
54+
{
55+
return (new Convert($string))->toPascal();
56+
}
57+
}
58+
59+
if (! function_exists(__NAMESPACE__ . '\adaify')) {
60+
function adaify(string $string): string
61+
{
62+
return (new Convert($string))->toAda();
63+
}
64+
}
65+
66+
if (! function_exists(__NAMESPACE__ . '\macroify')) {
67+
function macroify(string $string): string
68+
{
69+
return (new Convert($string))->toMacro();
70+
}
71+
}
72+
73+
if (! function_exists(__NAMESPACE__ . '\kebabify')) {
74+
function kebabify(string $string): string
75+
{
76+
return (new Convert($string))->toKebab();
77+
}
78+
}
79+
80+
if (! function_exists(__NAMESPACE__ . '\trainify')) {
81+
function trainify(string $string): string
82+
{
83+
return (new Convert($string))->toTrain();
84+
}
85+
}
86+
87+
if (! function_exists(__NAMESPACE__ . '\cobolify')) {
88+
function cobolify(string $string): string
89+
{
90+
return (new Convert($string))->toCobol();
91+
}
92+
}
93+
94+
if (! function_exists(__NAMESPACE__ . '\lowerify')) {
95+
function lowerify(string $string): string
96+
{
97+
return (new Convert($string))->toLower();
98+
}
99+
}
100+
101+
if (! function_exists(__NAMESPACE__ . '\upperify')) {
102+
function upperify(string $string): string
103+
{
104+
return (new Convert($string))->toUpper();
105+
}
106+
}
107+
108+
if (! function_exists(__NAMESPACE__ . '\titlelify')) {
109+
function titlelify(string $string): string
110+
{
111+
return (new Convert($string))->toTitle();
112+
}
113+
}
114+
115+
if (! function_exists(__NAMESPACE__ . '\sentencify')) {
116+
function sentencify(string $string): string
117+
{
118+
return (new Convert($string))->toSentence();
119+
}
120+
}
121+
122+
if (! function_exists(__NAMESPACE__ . '\dotify')) {
123+
function dotify(string $string): string
124+
{
125+
return (new Convert($string))->toDot();
126+
}
127+
}

src/Contract/Collectable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Morph\Contract;
6+
7+
interface Collectable
8+
{
9+
public function all(): array;
10+
11+
public function push(object $datum): void;
12+
}

src/Contract/Exportable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Morph\Contract;
6+
7+
interface Exportable
8+
{
9+
public function export(): mixed;
10+
}

src/Contract/Formatter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Morph\Contract;
6+
7+
interface Formatter
8+
{
9+
public function format(mixed $value, mixed $option = null): mixed;
10+
}

src/Contract/Message.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Morph\Contract;
6+
7+
use Morph\Support\Set;
8+
9+
interface Message
10+
{
11+
public function properties(): Set;
12+
13+
public function content(): mixed;
14+
}

src/Contract/Testing/Faker.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Morph\Contract\Testing;
6+
7+
use Morph\Support\Set;
8+
9+
interface Faker
10+
{
11+
public function generate(string $name, array $arguments = []): mixed;
12+
13+
/**
14+
* @template U of object
15+
* @param class-string<U> $class
16+
*/
17+
public function fake(string $class, array $presets = []): Set;
18+
}

0 commit comments

Comments
 (0)