manulaiko/php-consistentor

处理PHP函数名不一致问题的最简单方法

dev-master 2016-07-21 00:21 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:53:16 UTC


README

内容

介绍

PHP最糟糕的事情之一就是其函数名缺乏一致性,即使在相似/相关函数之间

<?php

/*
 * Some functions use underscores (_) to separate words
 * while other similar functions don't
 */
gettype("foo");            // No separation between words
get_class(new stdClass()); // Words separated by underscores

/*
 * Other functions mix `to` with 2
 */
bin2hex("foo");    // 2
strtolower("FOO"); // to

这个库为这些问题提供了一个快速简单的解决方案,使用起来就像这样简单

<?php
/**
 * Load PHPConsistentor
 */
require_once("PHPConsistentor.php");

// Call `PHPConsistentor::init` with the preferred settings
PHPConsistentor::init([
    "word_separation" => PHPConsistentor::WORD_SEPARATION_UNDERSCORE,
    "word_cut"        => PHPConsistentor::WORD_CUT_NO_CUT,
    "to_2"            => PHPConsistentor::TO_2_TO,
]);

get_type("foo");           //gettype
get_class(new stdClass()); //get_class

binary_to_hexadecimal("foo"); //bin2hex
string_to_lowercase("FOO");   //strtolower
new_line_to_br("f
o
o");                          //nl2br

这就完成了,现在你可以编写代码而不用担心函数名的一致性。

安装

你可以直接通过github,composer或下载Alexya Framework来获取它

手动安装

要手动安装 PHPConsistentor,只需克隆此存储库(或下载最新版本)并在你的PHP脚本中包含该文件

<?php
/**
 * Load PHPConsistentor
 */
require_once("path/to/PHPConsistentor.php");

// Pretty hard, right?

Composer

最简单的方式可能是通过composer安装,所以只需将以下行添加到你的 composer.php 文件中,并执行 composer update

{
    "require": {
        "manulaiko/php-consistentor": "dev-master"
    }
}

或执行以下命令

composer require manulaiko/php-consistentor:dev-master

Alexya Framework

PHPConsistentor 与最新版本的 Alexya Framework 预先构建,并位于命名空间 \Alexya\PHPConsistentor

<?php
/**
 * Load Alexya's core
 */
require_once("bootstrap.php");

\Alexya\PHPConsistentor::init([
    "word_separation" => PHPConsistentor::WORD_SEPARATION_UNDERSCORE,
    "word_cut"        => PHPConsistentor::WORD_CUT_NO_CUT,
    "to_2"            => PHPConsistentor::TO_2_TO,
]);

入门

开始使用 PHPConsistentor 就像在你的脚本中包含 PHPConsistentor.php 文件并调用 PHPConsistentor::init 方法一样简单。

此方法将自动根据配置数组(作为参数发送)添加函数别名。

数组可能包含以下索引

<?php
/**
 * Load PHPConsistentor
 */
require_once("PHPConsistentor.php");

/**
 * PHPConsistentor's configuration array
 */
$configuration = [
    /**
     * The way on which word separation will look.
     *
     * Possible values:
     *
     * - `PHPConsistentor::WORD_SEPARATION_UNDERSCORE`
     *       Separates the words with underscores like `get_class()`
     * - `PHPConsistentor::WORD_SEPARATION_NO_SEPARATION`
     *       Don't separates the words like `getclass()`
     * - `PHPConsistentor::WORD_SEPARATION_CAMEL_CASE`
     *       Separates the words with camelCase like `getClass()`
     */
    "word_separation" => PHPConsistentor::WORD_SEPARATION_UNDERSCORE, //Default

    /**
     * The way on which words will be cutted
     *
     * Possible values:
     *
     * - `PHPConsistentor::WORD_CUT_NO_CUT`
     *       Don't cuts the words like `binary2hexadecimal()`
     * - `PHPConsistentor::WORD_CUT_CUT`
     *       Conserves the default word cuts like `bin2hex()`
     */
    "word_cut" => PHPConsistentor::WORD_CUT_CUT, // Default

    /**
     * The way on which `2`, `to` functions should look
     *
     * Possible values:
     *
     * - `PHPConsistentor::TO_2_TO`
     *        Translates `2` functions to `to` functions like `nltobr()`
     *
     * - `PHPConsistentor::TO_2_2`
     *       Translates `to` functions to `2` functions like `str2lower()`
     */
    "to_2" => PHPConsistentor::TO_2_TO // Default
];

PHPConsistentor::init($configuration);

如果你通过 Alexya Framework 使用 PHPConsistentor,则可以在 config/alexya.php 下的 PHPConsistentor 索引中编辑默认设置.git