starship/scalar

为 php5.3 提供的 scalar 对象

dev-master 2013-02-14 14:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:37:48 UTC


README

PHP* 中提供 Scalar 对象,从而帮助改善 PHP 不稳定的 API。最好的部分是您可以使用 PHP 的任何内置字符串/数组函数!

**目前 Scalar 支持字符串和数组,但未来版本将支持整数和浮点数。
**此库需要 PHP 5.3 或更高版本。

安装

安装 react/scalar 的推荐方式是通过 composer

{
    "require": {
        "starship/scalar": "dev-master"
    }
}

示例

###简单的字符串操作
您可以使用任何 PHP 字符串方法。以下是一些示例

require __DIR__.'/vendor/autoload.php';

$my_string = new Starship\Scalar\Str('This is a great string!');

echo $my_string; //Outputs: 'This is a great string!'
echo $my_string->strlen(); //Outputs: 23
echo $my_string->strpos('great'); //Outputs: 10
echo $my_string; //Outputs: 'This is a great string!'

###简单的数组操作

您可以使用任何 PHP 数组方法。以下是一些示例

require __DIR__.'/vendor/autoload.php';

$my_array = new Starship\Scalar\sArray(array(1,2,3,4));

echo $my_array; //Outputs: '[1,2,3,4]'
echo $my_array[0]; //Outputs: 1
echo $my_array[1]; //Outputs: 2
echo $my_array->count(); //Outputs: 4
echo $my_array->implode('_'); //Outputs: '1_2_3_4'
$new_array = $my_array->array_unshift(10);
echo $new_array; //Outputs: '[10,1,2,3,4]'
echo $new_array[0] //Outputs: 10

默认情况下,Scalar 将您的字符串/数组映射到正在执行的 PHP 函数的第一个参数。如您所知,这并不充分,幸运的是 Scalar 提供了两种解决方法。第一种是使用令牌,第二种是使用名为 MethodMapper 的类。让我们看看令牌替换方法

require __DIR__.'/vendor/autoload.php';

$my_string = new Starship\Scalar\Str('This is a great string!');
$my_array = new Starship\Scalar\sArray(array(1,2,3,4));

echo $my_string; //Outputs: 'This is a great string!'

//We use the token '___' which will be replaced by the value of $my_string ('This is a great string!')

echo $my_string->str_replace('great', 'good', '___'); //Outputs: 'This is a good string!'

echo $my_array->implode('|', '___'); //Outputs: '1|2|3|4'

使用令牌替换可能不是最佳选择。这就是 MethodMapper 的作用所在。MethodMapper 允许您为常用的 PHP 函数创建映射。让我们快速看看 MethodMapper 类,然后看看它是如何工作的。

namespace Starship\Scalar;

class MethodMapper
{
	public static $method_map = array(
		"str_replace" => array('haystack'=>3),
		"explode" => array('haystack'=>2),
		"implode" => array('haystack'=>2),
	);	
}

在上面的示例中,我们看到 MethodMapper 类包含一个静态变量 $method_map。$method_map 是一个简单的关联数组。$method_map 中的每个项都使用 PHP 方法名作为键,并包含一个关联数组,告诉 Scalar 查找字符串/数组的值的位置。您可以看到 str_replace 已经被映射,使得查找字符串传递到第三个参数,从而使得下面的示例成为可能,并消除了对令牌的需求

require __DIR__.'/vendor/autoload.php';

$my_string = new Starship\Scalar\Str('This is a great string!');

echo $my_string; //Outputs: 'This is a great string!'
echo $my_string->str_replace('great', 'super great'); //Outputs: 'This is a super great string!'

输出连接

Scalar 允许您将一个或多个方法的输出连接或管道到下一个。为此,您只需在变量中添加调用 "()" 即可。

require __DIR__.'/vendor/autoload.php';

$my_string = new Starship\Scalar\Str('This is a great string!');

echo $my_string; //Outputs: 'This is a great string!'
echo $my_string()->str_replace('great', 'super great')->strlen(); //Outputs: 29
echo $my_string()->str_replace('great', 'super great')->substr(0,10)->strlen(); //Outputs 10;
echo $my_string()->explode('great')->count(); //Outputs: 2
echo $my_string()->explode('great')->implode('wow'); //Outputs: 'This is a wow string!'

$my_array = new Starship\Scalar\sArray(array(1,2,3,4));

echo $my_array()->implode('')->strlen(); //Outputs: 4
echo $my_array()->implode('')->strlen()->rand(10); //Outputs: random number between 4 and 10