gressus/tools

此包的最新版本(1.1.8)没有可用的许可证信息。

PHP 开发工具

1.1.8 2024-01-30 14:24 UTC

This package is auto-updated.

Last update: 2024-08-30 01:11:33 UTC


README

Gressus Tools 是一系列 PHP 脚本,用于简化数据操作、CSV 读写和对象访问。

数据映射

您可以将源数组映射到目标数组,甚至应用过滤器和后转换器。这有助于您在编程从一种格式到另一种格式的大数据集的导入脚本时。

namespace Gressus\Tools;
require('../autoload.php');

$dataMapperService = new DataMapperService(
    array(
        'Identifier' => 'id',
        'Group + Name' => new Mapper\Concat(array(
            'group',
            new Mapper\FirstNotEmpty(array('full_name','name')))
        ),
        'Counter' => new Mapper\Counter(),
        'FirstChar' => new Mapper\FirstChar('name'),
        'Name' => new Mapper\FirstNotEmpty(array('full_name','name')),
        'Hash' => new Mapper\All(),
        'Weapons' => new Mapper\ArrayPath('special/weapons'),
    ),
    array(
          new Converter\Serialize('Hash'),
          new Converter\Md5('Hash'),
          new Converter\Implode('Weapons'),
    ),
    array(
        array('score',new Filter\GreaterThanFilter(0)),
    ),
    array(
        array('Counter',new Filter\GreaterThanFilter(1)),
    )
);



$data = array(
    array('id' => '1', 'group' => 'Police',   'name' => 'Melanie',   'score' => 1, 'full_name' => 'Melanie Meyer', 'special' => array('weapons' => array('Walter','Tonfa'))),
    array('id' => '2', 'group' => 'Police',   'name' => 'Kerstin',   'score' => 1, 'full_name' => 'Kerstin Meyer', 'special' => array('weapons' => array('Walter','Tonfa'))),
    array('id' => '3', 'group' => 'Police',   'name' => 'Thomas',    'score' => 4, 'full_name' => 'Thomas Taffil', 'special' => array('weapons' => array('Walter','Tonfa'))),
    array('id' => '5', 'group' => 'Gangster', 'name' => 'Hafti',     'score' => 10, 'special' => array('weapons' => array('Knife','AKAI 47'))),
    array('id' => '6', 'group' => 'Gangster', 'name' => 'Fler',      'score' => 0),
    array('id' => '7', 'group' => 'Press',    'name' => 'Steiger',   'score' => 0),
    array('id' => '8', 'group' => 'Press',    'name' => 'Sz',        'score' => 5),
    array('id' => '9', 'group' => 'Press',    'name' => 'Max',       'score' => 1),
    array('id' => '10','group' => 'Press',    'name' => 'Max',       'score' => 1),
);


$mappedData = $dataMapperService->map($data);

print(json_encode($mappedData,JSON_PRETTY_PRINT));

输出

[
    {
        "Identifier": "2",
        "Group + Name": "Police Kerstin Meyer",
        "Counter": 2,
        "FirstChar": "K",
        "Name": "Kerstin Meyer",
        "Hash": "150ca40755bb997e775fcd0a94fc0147",
        "Weapons": "Walter , Tonfa"
    },
    {
        "Identifier": "3",
        "Group + Name": "Police Thomas Taffil",
        "Counter": 3,
        "FirstChar": "T",
        "Name": "Thomas Taffil",
        "Hash": "e80d48253791a5a083f2b49b0d4a7b70",
        "Weapons": "Walter , Tonfa"
    },
    {
        "Identifier": "5",
        "Group + Name": "Gangster Hafti",
        "Counter": 4,
        "FirstChar": "H",
        "Name": "Hafti",
        "Hash": "e8bf5ce0d5e2d75346b5b4e1282c47d6",
        "Weapons": "Knife , AKAI 47"
    },
    {
        "Identifier": "8",
        "Group + Name": "Press Sz",
        "Counter": 5,
        "FirstChar": "S",
        "Name": "Sz",
        "Hash": "8ad9d3eae1f9fa75fdbcb3fbd0bac00e",
        "Weapons": null
    },
    {
        "Identifier": "9",
        "Group + Name": "Press Max",
        "Counter": 6,
        "FirstChar": "M",
        "Name": "Max",
        "Hash": "8a85c8790f0029a12584bec43c1734d0",
        "Weapons": null
    },
    {
        "Identifier": "10",
        "Group + Name": "Press Max",
        "Counter": 7,
        "FirstChar": "M",
        "Name": "Max",
        "Hash": "9b5b1d4c5ca965f0d2bcd4e039b48cba",
        "Weapons": null
    }
]

数据减少

(在 PHP 数组上行为类似于 MySQL group by)

namespace Gressus\Tools;
require('../autoload.php');

$reducer = new ReducerService(
    'group',
    array(
        'id' => new Reducer\ConcatReducer(),
        'name' => new Reducer\ConcatReducer(array('distinct' => true)),
        'score' => new Reducer\SumReducer(),
    ),
    array(
        array('score',new Filter\GreaterThanFilter(0)),
        array('score',new Filter\LowerThanFilter(10)),
    )
);

$data = array(
    array('id' => '1','group' => 'Police', 'name' => 'Melanie','score' => 1),
    array('id' => '2','group' => 'Police', 'name' => 'Kerstin','score' => 1),
    array('id' => '3','group' => 'Police', 'name' => 'Thomas','score' => 4),
    array('id' => '5','group' => 'Gangster', 'name' => 'Hafti','score' => 10),
    array('id' => '6','group' => 'Gangster', 'name' => 'Fler','score' => 0),
    array('id' => '7','group' => 'Press', 'name' => 'Steiger','score' => 0),
    array('id' => '8','group' => 'Press', 'name' => 'Sz','score' => 5),
    array('id' => '9','group' => 'Press', 'name' => 'Max','score' => 1),
    array('id' => '10','group' => 'Press', 'name' => 'Max','score' => 1),
);

$reducedData = $reducer->reduce($data);

print(json_encode($reducedData,JSON_PRETTY_PRINT));

输出

{
    "Police": {
        "id": "1, 2, 3",
        "name": "Melanie, Kerstin, Thomas",
        "score": 6
    },
    "Press": {
        "id": "8, 9, 10",
        "name": "Sz, Max",
        "score": 7
    }
}

读取 CSV 数据

namespace Gressus\Tools;
require('../autoload.php');

$csvService = new CsvService();

$csvService->read('../Data/example-data.csv');

print_r($csvService->getAssociatedArrayData());

写入 CSV 数据

namespace Gressus\Tools;
require('../autoload.php');

$data = array(
    array('id' => '1', 'group' => 'Police',   'name' => 'Melanie',   'score' => 1, 'full_name' => 'Melanie Meyer'),
    array('id' => '2', 'group' => 'Police',   'name' => 'Kerstin',   'score' => 1, 'full_name' => 'Kerstin Meyer'),
    array('id' => '3', 'group' => 'Police',   'name' => 'Thomas',    'score' => 4, 'full_name' => 'Thomas Thiel'),
    array('id' => '5', 'group' => 'Gangster', 'name' => 'Hafti',     'score' => 10,),
    array('id' => '6', 'group' => 'Gangster', 'name' => 'Fler',      'score' => 0),
    array('id' => '7', 'group' => 'Press',    'name' => 'Steiger',   'score' => 0),
    array('id' => '8', 'group' => 'Press',    'name' => 'Sz',        'score' => 5),
    array('id' => '9', 'group' => 'Press',    'name' => 'Max',       'score' => 1),
    array('id' => '10','group' => 'Press',    'name' => 'Max',       'score' => 1),
);


$csvService = new CsvService();
$csvService
    ->setAssociatedArrayData($data)
    ->setFileName('../Data/example-data-write.csv')
    ->write();

致谢

非常感谢 TUDOCK。我最初是在 TUDOCK 的舒适办公室开始这些工具的开发工作的。