kheaactua / argument-validator
此包的最新版本(dev-master)没有提供许可证信息。
一个简单的PHP类,用于验证和设置函数的输入。当函数接受大量输入时非常有用。
dev-master
2014-07-08 16:41 UTC
This package is not auto-updated.
Last update: 2024-09-24 08:06:19 UTC
README
一个简单的类,用于弥补PHP中关键字参数的不足。这对于接受许多参数的函数很有用,其中许多参数是可选的,并具有默认值。
安装
创建一个composer.json
{ "require": { "kheaactua/argument-validator": "dev-master" } }
并运行
$ wget https://getcomposer.org.cn/composer.phar $ php composer.phar install
安装完成后,您需要在脚本中包含vendor/autoload.php
以使类可用。
require_once('vendor/autoload.php');
示例
在这个例子中,我们有一个Order类,它有一个toArray方法,该方法接受配置并将Order对象序列化为一个数组,然后用于将其序列化为HTML/Text/LaTeX等。
此方法有许多配置选项。例如,在订单中每个项目的HTML输出上显示项目操作按钮,显示哪些标题,谁请求输出等。
use ArgumentValidator\ArgumentValidator; class Order extends BaseOrder { protected $toArrayConfig; public function __construct() { $this->toArrayConfig = new ArgumentValidator(); $this->toArrayConfig->addOpt('auth', 'obj:\Mayofest\Auth', false, \Mayofest\Auth::getInstance()); $this->toArrayConfig->addOpt('itemActions', 'bool', false, false); // Actions for items, delete, discount, refund $this->toArrayConfig->addOpt('adminView', 'bool', false, false); // Top toggle button, FB and Email icon $this->toArrayConfig->addOpt('adminOrderActions', 'bool', false, false); // Button buttons, recalc, del, cancel $this->toArrayConfig->addOpt('userOrderActions', 'bool', false, false); // User buttons, confirm, cancel $this->toArrayConfig->addOpt('activity', 'bool', false, true); // Show order acitivty $this->toArrayConfig->addOpt('itemTax', 'bool', false, false); // Show tax on individual items $this->toArrayConfig->addOpt('omit_discounts', 'bool', false, false); // Don't show discounts $this->toArrayConfig->addOpt('omit_donations', 'bool', false, false); // Don't show donations $this->toArrayConfig->addOpt('groupSimilar', 'bool', false, false); // Group by class & options, sum quantity and price $this->toArrayConfig->addOpt('labelDonations', 'bool', false, false); // Change "Donation" to "5$ donation" $this->toArrayConfig->addOpt('moneyFmt', 'text', false, MONEY_FMT); $this->toArrayConfig->addOpt('shortTitle', 'bool', false, false); // Change "Donation" to "5$ donation" $this->toArrayConfig->addOpt('statusInTitle', 'bool', false, false); // Put the status in the title } /** * Converts the order object into an array that is used by output serializers (HTML, LaTeX, Text, etc) * @param ArgumentValidator $config Config output (see the constructor for an up to date list if inputs) * * @returns string All the info to be rendered */ public function __toArray($config = array()) { // Let the exception rise $config = $this->toArrayConfig->validate($config, $str); // Our object (array) // Most options removed for example $obj = array('headers'=>array(), 'orderId' => $oid, // Order title 'userName' => $user->getName(), 'userNameAndId' => $user->getNameAndId(), 'userShortName' => sprintf('%s. %s', substr($user->getFirstName(), 0, 1), $user->getLastName()), 'title' => NULL, // Order title 'itemTax' => $config->getConf('itemTax'), 'items' => array(), // button "objects" // ... ); if ($config->getConf('adminView')) { $obj['user_fb_id'] = $this->getUserRelatedByUserId()->getFbId(); $obj['user_email'] = $this->getUserRelatedByUserId()->getEmail(); $obj['user_contact_html'] = $this->getUserRelatedByUserId()->getNameAndContact(); } // Headers $obj['headers'] = array('item'=>'Item', 'details' => 'Details', 'quantity' => 'Quantity'); if ($config->getConf('itemTax')) $obj['headers']['itemTax'] = 'Tax'; if ($config->getConf('itemActions')) $obj['headers']['itemActions'] = 'Actions'; // ... return $obj; } }