first commit

This commit is contained in:
Ryan Ariana
2024-05-06 11:04:37 +07:00
commit aee061ddba
7322 changed files with 2918816 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
branches:
only:
- master
- develop
before_script:
- composer dump
language: php
php:
- '5.6'
- '7.0'
- '7.1'
- nightly

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Thomas Deneulin
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.

View File

@@ -0,0 +1,238 @@
# Morphism PHP
[![Build Status][travis-image]][travis-url]
> Helps you to transform any object structure to another.
This library is inspired by Yann Renaudin's : [Morphism library](https://github.com/emyann/morphism)
## Contribution
- Twitter: [@TDeneulin][twitter-account]
- Pull requests and stars are always welcome 🙏🏽 For bugs and feature requests, [please create an issue](https://github.com/Gmulti/morphism-php/issues)
## Getting started 🚀
Install `morphism-php` using composer : `composer require gmulti/morphism-php`.
```php
use Morphism\Morphism;
```
## What does it do? 🤔
Morphism uses a semantic configuration to go through the collection of graph objects you have to process. Then it extracts and computes the value from the specified path(s). Finally, it sets this value to the destination property from the schema.
## Usage 🍔
Morphism is curried function that allows a partial application with a semantic configuration. You can use it in many ways:
### Example
```php
// Target type you want to have
class User {
public function __construct($firstName, $lastName, $phoneNumber){
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->phoneNumber = $phoneNumber;
       $this->city = null;
  }
}
// Data source you want to map
$data = array(
"name" => "Iron Man",
"firstName" => "Tony",
"lastName" => "Stark",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "212 555-1234"
),
array(
"type" => "mobile",
"number" => "646 555-4567"
)
)
);
// Mapping Schema ( see more examples below )
$schema = array(
"city" => "address.city",
"name" => function($data){
return strtoupper($data["name"]);
}
);
Morphism::setMapper("User", $schema);
// Map using the registered type and the registry
$result = Morphism::map("User", $data);
/// *** OUTPUT *** ///
class User {
public $city // string(13) "New York City"
public $name // string(8) "iron man"
}
```
### Multidimensional array
```php
// Target type you want to have
class User {
}
// Data source you want to map
$data = array(
array(
"name" => "Iron Man",
"firstName" => "Tony",
"lastName" => "Stark",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "212 555-1234"
),
array(
"type" => "mobile",
"number" => "646 555-4567"
)
)
),
array(
"name" => "Spiderman",
"firstName" => "Peter",
"lastName" => "Parker",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "999 999-9999"
)
)
)
);
// Mapping Schema ( see more examples below )
$schema = array(
"city" => "address.city",
"name" => function($data){
return strtoupper($data["name"]);
}
);
Morphism::setMapper("User", $schema);
// Map using the registered type and the registry
$result = Morphism::map("User", $data);
/// *** OUTPUT *** ///
array(
class User {
public $city // string(13) "New York City"
public $name // string(8) "iron man"
},
class User {
public $city // string(13) "New York City"
public $name // string(8) "spiderman"
}
)
```
## Schema Examples
### Dataset sample
```php
$data = array(
"name" => "Iron Man",
"firstName" => "Tony",
"lastName" => "Stark",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "212 555-1234"
),
array(
"type" => "mobile",
"number" => "646 555-4567"
)
)
);
// Target type you want to have
class User {
}
```
### Agregator
```php
// Schema
$schema = array(
"fullName" => array("firstName", "lastName")
);
Morphism::setMapper("User", $schema);
// Map using the registered type and the registry
$result = Morphism::map("User", $data);
/// *** OUTPUT *** ///
class User {
public $fullName // "Tony Stark"
}
```
### Computing over Flattening / Projection
```php
// Schema
$schema = array(
"city" => (object) array(
"path" => "address.city",
"fn" => function($city) {
return strtolower($city);
}
),
"nbContacts" => function($data){
return count($data["phoneNumber"]);
}
);
Morphism::setMapper("User", $schema);
// Map using the registered type and the registry
$result = Morphism::map("User", $data);
/// *** OUTPUT *** ///
class User {
public $city // "new york city" <= strtolower
public $nbContacts // 2 <= computed from the object
}
```
## License
MIT © [Thomas Deneulin][twitter-account]
[twitter-account]: https://twitter.com/TDeneulin
[travis-image]: https://travis-ci.org/Gmulti/morphism-php.svg?branch=master
[travis-url]: https://travis-ci.org/Gmulti/morphism-php

View File

@@ -0,0 +1,20 @@
{
"name": "gmulti/morphism-php",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Thomas DENEULIN",
"email": "thomas@delipress.io"
}
],
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"Morphism\\": "lib/"
}
},
"require-dev": {
"phpunit/phpunit": "^6.5"
}
}

View File

@@ -0,0 +1,91 @@
<?php
namespace Morphism\Helpers;
#[AllowDynamicProperties]
trait MorphismHelper {
/**
* Source : https://stackoverflow.com/questions/5458241/php-dynamic-array-index-name
*
* @static
* @param array $array
* @param array $indexes
*/
protected static function getArrayValue(array $array, array $indexes)
{
if (count($array) == 0 || count($indexes) == 0) {
return false;
}
$index = array_shift($indexes);
if(!array_key_exists($index, $array)){
return false;
}
$value = $array[$index];
if (count($indexes) == 0) {
return $value;
}
if(!is_array($value)) {
return false;
}
return self::getArrayValue($value, $indexes);
}
/**
* @static
* @param array $paths
* @param array $object
* @return string
*/
protected static function agregator($paths, $object){
return array_reduce($paths, function($delta, $path) use ($object) {
$searchPath = $path;
if(!is_array($path)){
$searchPath = array($path);
}
return trim(sprintf("%s %s", $delta, self::getArrayValue($object, $searchPath) ));
});
}
/**
* @static
* @param array $object
* @param array $schema
* @param array $data
* @return object
*/
protected static function transformValuesFromObject($object, $schema, $data){
foreach($schema as $key => $target){ // iterate on every action of the schema
if(is_string($target)){ // Target<String>: string path => [ target: 'source' ]
$indexes = explode(".", $target);
$object->{$key} = self::getArrayValue($data, $indexes);
}
else if(is_callable($target)){
$object->{$key} = call_user_func($target, $data);
}
else if (is_array($target)){
$object->{$key} = self::agregator($target, $data);
}
else if(is_object($target) ) {
$searchPath = $target->path;
if(is_array($target->path)){
$value = self::agregator($target->path, $data);
}
else{
$indexes = explode(".", $target->path);
$value = self::getArrayValue($data, $indexes);
}
$object->{$key} = call_user_func($target->fn, $value);
}
}
return $object;
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace Morphism;
use Morphism\Helpers\MorphismHelper;
#[AllowDynamicProperties]
abstract class Morphism {
use MorphismHelper;
protected static $registries = array();
public static function register($type, $schema) {
}
/**
* @static
* @param string $type
* @return bool
*/
public static function exists($type){
return array_key_exists($type, self::$registries);
}
/**
* @static
* @param string $type
* @return array
*/
public static function getMapper($type){
return self::$registries[$type];
}
/**
* @static
* @param string $type
* @param array $schema
*/
public static function setMapper($type, $schema){
if (!$type) {
throw new \Exception('type paramater is required when register a mapping');
}
if (!$schema) {
throw new \Exception('schema paramater is required when register a mapping');
}
self::$registries[$type] = $schema;
}
/**
* @static
* @param string $type
*/
public static function deleteMapper($type){
unset(self::$registries[$type]);
}
/**
* @static
* @param string $type
* @param array $data
*/
public static function map($type, $data){
if(!Morphism::exists($type)){
throw new \Exception(sprintf("Mapper for %s not exist", $type));
}
$reflectedClass = new \ReflectionClass($type);
if(!$reflectedClass->isInstantiable()){
throw new \Exception($type . " is not an instantiable class.");
}
if(isset($data[0])){
return array_map(function($arr) use($reflectedClass, $type){
$instance = $reflectedClass->newInstance();
return self::transformValuesFromObject($instance, Morphism::getMapper($type), $arr);
}, $data);
}
else{
$instance = $reflectedClass->newInstance();
return self::transformValuesFromObject($instance, Morphism::getMapper($type), $data);
}
}
}

View File

@@ -0,0 +1,7 @@
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Morphism">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@@ -0,0 +1,5 @@
<?php
class User {
}

View File

@@ -0,0 +1,112 @@
<?php
use PHPUnit\Framework\TestCase;
use Morphism\Morphism;
class MorphismArrayTest extends TestCase
{
public function setUp(){
$this->data = array(
array(
"name" => "Iron Man",
"firstName" => "Tony",
"lastName" => "Stark",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "212 555-1234"
),
array(
"type" => "mobile",
"number" => "646 555-4567"
)
)
),
array(
"name" => "Spiderman",
"firstName" => "Peter",
"lastName" => "Parker",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "293 093-2321"
)
)
)
);
}
public function testActionArrayStringPath()
{
$schema = array(
"city" => "address.city"
);
Morphism::setMapper("User", $schema);
$result = Morphism::map("User", $this->data);
$this->assertEquals(count($result), 2);
$this->assertEquals($result[0]->city, "New York City");
}
public function testActionFunctionPath(){
$schema = array(
"city" => function($data){
return strtolower($data["address"]["city"]);
}
);
Morphism::setMapper("User", $schema);
$result = Morphism::map("User", $this->data);
$this->assertEquals($result[0]->city, "new york city");
$this->assertEquals($result[1]->city, "new york city");
}
public function testActionAgregatorPath(){
$schema = array(
"fullName" => array(
"firstName", "lastName"
)
);
Morphism::setMapper("User", $schema);
$result = Morphism::map("User", $this->data);
$this->assertEquals($result[0]->fullName, "Tony Stark");
$this->assertEquals($result[1]->fullName, "Peter Parker");
}
public function testActionFunctionObjectPath(){
$schema = array(
"city" => (object) array(
"path" => "address.city",
"fn" => function($city) {
return strtolower($city);
}
)
);
Morphism::setMapper("User", $schema);
$result = Morphism::map("User", $this->data);
$this->assertEquals($result[0]->city, "new york city");
$this->assertEquals($result[1]->city, "new york city");
}
}

View File

@@ -0,0 +1,91 @@
<?php
use PHPUnit\Framework\TestCase;
use Morphism\Morphism;
require __DIR__ . "/Mocks/User.php";
class MorphismTest extends TestCase
{
public function setUp(){
$this->data = array(
"name" => "Iron Man",
"firstName" => "Tony",
"lastName" => "Stark",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "212 555-1234"
),
array(
"type" => "mobile",
"number" => "646 555-4567"
)
)
);
}
public function testActionStringPath(){
$schema = array(
"city" => "address.city"
);
Morphism::setMapper("User", $schema);
$result = Morphism::map("User", $this->data);
$this->assertEquals($result->city, "New York City");
}
public function testActionFunctionPath(){
$schema = array(
"city" => function($data){
return strtolower($data["address"]["city"]);
}
);
Morphism::setMapper("User", $schema);
$result = Morphism::map("User", $this->data);
$this->assertEquals($result->city, "new york city");
}
public function testActionAgregatorPath(){
$schema = array(
"fullName" => array(
"firstName", "lastName"
)
);
Morphism::setMapper("User", $schema);
$result = Morphism::map("User", $this->data);
$this->assertEquals($result->fullName, "Tony Stark");
}
public function testActionFunctionObjectPath(){
$schema = array(
"city" => (object) array(
"path" => "address.city",
"fn" => function($city) {
return strtolower($city);
}
)
);
Morphism::setMapper("User", $schema);
$result = Morphism::map("User", $this->data);
$this->assertEquals($result->city, "new york city");
}
}