Skip to content

Commit a3da4db

Browse files
first commit
0 parents  commit a3da4db

File tree

9 files changed

+274
-0
lines changed

9 files changed

+274
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
.idea/
3+
/composer.lock
4+
/phpunit.xml

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# A Simple Method to Make a Data Mapper in PHP
2+
In most projects, we have to map data to other cases. For example we want to map `1m` to `1 minute` or `3d` to `3 days`.
3+
4+
This package is a data mapper and we can create our mapper based on that very easily.
5+
6+
## Install
7+
```
8+
$ composer require a-afsharfarnia/data-mapper
9+
```
10+
11+
### How to use
12+
You can find samples of using this package in `/src/samples`. You can have a look on `TimeMapper` and `LanguageMapper` classes.
13+
14+
In fact, in your project, for each map data, you must create a class which is extended of `Afsharfarnia\DataMapper\mappers\Mapper` of this package. Then in each class you must define a `$mapper` static property which includes its mapper data.

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "a-afsharfarnia/data-mapper",
3+
"type": "helper",
4+
"description": "This is a package to make a simple data mapper",
5+
"keywords": ["a-afsharfarnia", "data-mapper", "data mapper", "map-data", "map data"],
6+
"homepage": "https://github.com/a-afsharfarnia/php-data-mapper",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Abbas Afsharfarnia",
11+
"email": "[email protected]",
12+
"homepage": "http://afsharfarnia.com",
13+
"role": "Senior Backend Developer"
14+
}
15+
],
16+
"require": {
17+
"php": "7.2.*"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit" : ">=7.0"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Afsharfarnia\\DataMapper\\": "src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Afsharfarnia\\DataMapper\\tests\\": "tests/"
30+
}
31+
}
32+
}

src/mappers/Mapper.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Project_Name: Data Mapper in PHP
4+
* package_Name: a-afsharfarnia/data-mapper
5+
* Created_By: Abbas Afsharfarnia (07/10/2019 10:06)
6+
* Project_Address: https://github.com/a-afsharfarnia/php-data-mapper
7+
*/
8+
9+
namespace Afsharfarnia\DataMapper\mappers;
10+
11+
/**
12+
* Abstract Class Mapper
13+
* Description: Define mapData and unmapData methods
14+
*/
15+
abstract class Mapper implements MapperInterface
16+
{
17+
/**
18+
* use this method to map data
19+
*
20+
* @param $data
21+
* @return mixed
22+
*/
23+
public static function mapData($data) {
24+
$mapper = get_called_class()::$mapper;
25+
26+
if(array_key_exists($data, $mapper)) {
27+
$mapData = $mapper[$data];
28+
return $mapData;
29+
}
30+
31+
return "Data not found in mapping list";
32+
}
33+
34+
/**
35+
* use this method to unmap data
36+
*
37+
* @param $data
38+
* @return mixed
39+
*/
40+
public static function unmapData($data) {
41+
$mapper = get_called_class()::$mapper;
42+
43+
$unmapData = array_search ($data, $mapper);
44+
45+
if(!empty($unmapData)) {
46+
return $unmapData;
47+
}
48+
49+
return "Data not found in mapping list";
50+
}
51+
}

src/mappers/MapperInterface.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Project_Name: Data Mapper in PHP
4+
* package_Name: a-afsharfarnia/data-mapper
5+
* Created_By: Abbas Afsharfarnia (07/10/2019 10:06)
6+
* Project_Address: https://github.com/a-afsharfarnia/php-data-mapper
7+
*/
8+
9+
namespace Afsharfarnia\DataMapper\mappers;
10+
11+
/**
12+
* Interface MapperInterface
13+
* Description: Interface for Mapper abstract class
14+
*/
15+
interface MapperInterface
16+
{
17+
/**
18+
* use this method to map data
19+
*
20+
* @param $data
21+
* @return mixed
22+
*/
23+
public static function mapData($data);
24+
25+
/**
26+
* use this method to unmap data
27+
*
28+
* @param $data
29+
* @return mixed
30+
*/
31+
public static function unmapData($data);
32+
}

src/samples/LanguageMapper.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Project_Name: Data Mapper in PHP
4+
* package_Name: a-afsharfarnia/data-mapper
5+
* Created_By: Abbas Afsharfarnia (07/10/2019 10:06)
6+
* Project_Address: https://github.com/a-afsharfarnia/php-data-mapper
7+
*/
8+
9+
namespace Afsharfarnia\DataMapper\samples;
10+
11+
use Afsharfarnia\DataMapper\mappers\Mapper;
12+
13+
/**
14+
* Class LanguageMapper
15+
* Description: a sample of a mapper class
16+
*/
17+
class LanguageMapper extends Mapper
18+
{
19+
/** @var array $mapper */
20+
public static $mapper = [
21+
"en" => "English",
22+
"de" => "German",
23+
"fr" => "French",
24+
"ar" => "Arabic",
25+
];
26+
}

src/samples/TimeMapper.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Project_Name: Data Mapper in PHP
4+
* package_Name: a-afsharfarnia/data-mapper
5+
* Created_By: Abbas Afsharfarnia (07/10/2019 10:06)
6+
* Project_Address: https://github.com/a-afsharfarnia/php-data-mapper
7+
*/
8+
9+
namespace Afsharfarnia\DataMapper\samples;
10+
11+
use Afsharfarnia\DataMapper\mappers\Mapper;
12+
13+
/**
14+
* Class TimeMapper
15+
* Description: a sample of a mapper class
16+
*/
17+
class TimeMapper extends Mapper
18+
{
19+
/** @var array $mapper */
20+
public static $mapper = [
21+
"1m" => "1minute",
22+
"3m" => "3minutes",
23+
"5m" => "5minutes",
24+
"15m" => "15minutes",
25+
"30m" => "30minutes",
26+
"1h" => "1hour",
27+
"2h" => "2hours",
28+
"4h" => "4hours",
29+
"6h" => "6hours",
30+
"8h" => "8hours",
31+
"12h" => "12hours",
32+
"1d" => "1day",
33+
"3d" => "3days",
34+
"1w" => "1week",
35+
"1M" => "1month"
36+
];
37+
}

src/samples/UseSample.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Project_Name: Data Mapper in PHP
4+
* package_Name: a-afsharfarnia/data-mapper
5+
* Created_By: Abbas Afsharfarnia (07/10/2019 10:06)
6+
* Project_Address: https://github.com/a-afsharfarnia/php-data-mapper
7+
*/
8+
9+
use Afsharfarnia\DataMapper\samples\LanguageMapper;
10+
use Afsharfarnia\DataMapper\samples\TimeMapper;
11+
12+
echo TimeMapper::mapData("1m");
13+
echo "-----------";
14+
echo LanguageMapper::unmapData("German");

tests/TimeMapperTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Project_Name: Data Mapper in PHP
4+
* package_Name: a-afsharfarnia/data-mapper
5+
* Created_By: Abbas Afsharfarnia (07/10/2019 10:06)
6+
* Project_Address: https://github.com/a-afsharfarnia/php-data-mapper
7+
*/
8+
9+
namespace Afsharfarnia\DataMapper\tests;
10+
11+
use PHPUnit\Framework\TestCase;
12+
use Afsharfarnia\DataMapper\samples\LanguageMapper;
13+
use Afsharfarnia\DataMapper\samples\TimeMapper;
14+
15+
/**
16+
* Class TimeMapperTest
17+
* Description: a test class for testing the whole package
18+
*/
19+
class TimeMapperTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider timeMapperDataProvider
23+
* @param $data
24+
* @param $expectResult
25+
*/
26+
public function testTimeMapper($data, $expectResult) {
27+
$time = TimeMapper::mapData($data);
28+
$this->assertEquals($expectResult, $time);
29+
}
30+
31+
/**
32+
* @dataProvider languageMapperDataProvider
33+
* @param $data
34+
* @param $expectResult
35+
*/
36+
public function testLanguageMapper($data, $expectResult) {
37+
$time = LanguageMapper::unmapData($data);
38+
$this->assertEquals($expectResult, $time);
39+
}
40+
41+
/**
42+
* @return array
43+
*/
44+
public function timeMapperDataProvider()
45+
{
46+
return array(
47+
array('1m', '1minute'),
48+
array('30m', '30minutes'),
49+
array('12h', '12hours')
50+
);
51+
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public function languageMapperDataProvider()
57+
{
58+
return array(
59+
array('German', 'de'),
60+
array('French', 'fr'),
61+
array('English', 'en')
62+
);
63+
}
64+
}

0 commit comments

Comments
 (0)