Skip to content

Commit

Permalink
Initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
fab2s committed Mar 24, 2024
0 parents commit 8c26f49
Show file tree
Hide file tree
Showing 33 changed files with 2,046 additions and 0 deletions.
108 changes: 108 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# laravel default
*.css linguist-vendored
*.less linguist-vendored

# These settings are for any web project

# Handle line endings automatically for files detected as text
# and leave all files detected as binary untouched.
* text=auto

#
# The above will handle all files NOT found below
#

#
## These files are text and should be normalized (Convert crlf => lf)
#

# source code
*.php text
*.css text
*.sass text
*.scss text
*.less text
*.styl text
*.js text
*.coffee text
*.json text
*.htm text
*.html text
*.xml text
*.svg text
*.txt text
*.ini text
*.inc text
*.pl text
*.rb text
*.py text
*.scm text
*.sql text
*.sh text
*.bat text

# templates
*.ejs text
*.hbt text
*.jade text
*.haml text
*.hbs text
*.dot text
*.tmpl text
*.phtml text

# server config
.htaccess text

# git config
.gitattributes text
.gitignore text

# code analysis config
.jshintrc text
.jscsrc text
.jshintignore text
.csslintrc text

# misc config
*.yaml text
*.yml text
.editorconfig text

# build config
*.npmignore text
*.bowerrc text

# Heroku
Procfile text
.slugignore text

# Documentation
*.md text
LICENSE text
AUTHORS text

#
## These files are binary and should be left untouched
#

# (binary is a macro for -text -diff)
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.ico binary
*.mov binary
*.mp4 binary
*.mp3 binary
*.flv binary
*.fla binary
*.swf binary
*.gz binary
*.zip binary
*.7z binary
*.ttf binary
*.eot binary
*.woff binary
*.pyc binary
*.pdf binary
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
.*.cache
composer.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 fab2s

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
114 changes: 114 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Dt0

`Dt0` (_DeeTO_ or _DeTZerO_) is a DTO (_Data-Transport-Object_) PHP implementation than can both secure mutability and implement convenient ways to take control over input and output in various format.

## Installation

`Dt0` can be installed using composer:

```shell
composer require "fab2s/dt0"
```

Once done, you can start playing :

```php

use fab2s\Dt0\Dt0;

// works if all public props have defaults
$dt0 = new SomeDt0();

// set at least props without default
$dt0 = new SomeDt0(readOnlyProp: $someValue /*, ... */); // <= argument order does not matter
// unless SomeDt0 has a constructor

// same as
$dt0 = SomeDt0::make(readOnlyProp: $someValue /*, ... */); // <= argument order never matter

$value = $dt0->readOnlyProp; // $someValue

/** @var array|string|SomeDt0|Dt0|null $wannaBeDt0 */
$dt0 = SomeDt0::tryFrom($wannaBeDt0);

/** @var Dt0 $dt0 */

// recursively applied among Dt0 members
$array = $dt0->toArray();

// toArray with call to jsonSerialize on compatible members
$jsonArray = $dt0->toJsonArray();
// same as
$jsonArray = $dt0->jsonSerialize();

// toJson
$json = $dt0->toJson();
// same as
$json = json_decode($dt0);

// serializable
$serialized = serialize($dt0);
$dt0 = unserialize($serialized);

```

## Casting

`Dt0` comes with two `Attributes` : `Casts` and `Cast`

`Cast` is used to define how to handle a property as a **property attribute** and `Casts` is used to set many `Cast` at once as a **class attribute**.

````php

use fab2s\Dt0\Dt0;

#[Casts(
new Cast(default: 'defaultFromCast', propName: 'propClassCasted'),
// same as
propClassCasted: new Cast(default: 'defaultFromCast'),
)]
class MyDt0 extends Dt0
{
public readonly string $propClassCasted;

#[Cast(default: null)]
public readonly ?string $propCasted;
}

$dt0 = MyDt0::make();
/**
[
'propClassCasted' => 'defaultFromCast',
'propCasted' => null,
]
*/

$dt0 = MyDt0::make(propCasted:'Oh Yeah');
/**
[
'propClassCasted' => 'defaultFromCast',
'propCasted' => 'Oh Yeah',
]
*/

$dt0 = MyDt0::fromArray(['propCasted' => 'Oh', 'propClassCasted' => 'Ho']);
/**
[
'propClassCasted' => 'Oh',
'propCasted' => 'Ho',
]
*/

````

## Requirements

`Dt0` is tested against php 8.1 and 8.2

## Contributing

Contributions are welcome, do not hesitate to open issues and submit pull requests.

## License

`Dt0` is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).
58 changes: 58 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "fab2s/dt0",
"description": "Dt0, a DTO PHP implementation than can both secure mutability and implement convenient ways to take control over input and output in various format",
"type": "library",
"authors": [{
"name": "Fabrice de Stefanis"
}],
"support": {
"issues": "https://github.com/fab2s/Dt0/issues",
"source": "https://github.com/fab2s/Dt0"
},
"keywords": [
"Data-Transport-Object",
"DTO",
"DT0",
"symfony",
"laravel",
"PHP",
"Serializable",
"immutable",
"JSON",
"Data-Processing"
],
"license": [
"MIT"
],
"require": {
"php": "^8.1"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
"laravel/pint": "^1.10",
"orchestra/testbench": "^7.0|^8.0|^9.0",
"fab2s/math": "*"
},
"suggest": {
"laravel/laravel": "To use Laravel (The awesome) implementations"
},
"autoload": {
"psr-4": {
"fab2s\\Dt0\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"fab2s\\Dt0\\Tests\\": "tests"
}
},
"scripts": {
"post-update-cmd": [
"rm -rf .*.cache"
],
"post-install-cmd": [
"rm -rf .*.cache"
],
"fix": "@php vendor/bin/pint --config pint.json"
}
}
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="CI">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
Loading

0 comments on commit 8c26f49

Please sign in to comment.