sabloger/ php-strict
如果你想要更清晰的PHP,你将喜欢Php-Strict!如果你需要一个传递打包参数到函数的方法,你将喜欢Php-Strict!如果你需要一个验证参数并消除无效参数上的异常和数据缺失的方法,你将喜欢Php-Strict!如果你想要一个类似于Java和其他强类型语言的严格类型ArrayList...
0.1
2017-10-17 06:51 UTC
Requires
- php: >=5.6.0
This package is not auto-updated.
Last update: 2024-09-29 03:26:01 UTC
README
PHP 5的严格类型对象基础和ArrayList!(5.4及以上!)
介绍
- 如果你想要更清晰的PHP,你将喜欢Php-Strict!
- 如果你需要一个传递打包参数到函数的方法,你将喜欢Php-Strict!
- 如果你需要一个验证参数并消除无效参数上的异常和数据缺失的方法,你将喜欢Php-Strict!
- 如果你想要一个类似于Java和其他强类型语言的严格类型ArrayList,你将喜欢Php-Strict!
- 绑定到HTTP请求即将推出!!
安装
将以下代码添加到你的composer.json
中,并运行composer update
{ "require": { "sabloger/php-strict": "dev-master" } }
使用:对象
首先从BaseObject
扩展一个子类,实现父类抽象方法,设置预定义值,实例化后使用!最后运行$obj->validate()
以验证数据!例如:子类
use Php_Strict\BaseObject; /** * Class Book * @package Php_Strict * @method Book setTitle(string $value) * @method Book setAuthor(string $value) * @method Book setYear(int $value) * @method Book setSome_Object(Object $value) * @method string getTitle() * @method string getAuthor() * @method int getYear() * @method Object getSome_Object() */ class Book extends BaseObject { /** * @return array */ public function getFieldsStub() { return [ 'title' => 'string', // All of types as you want!! 'author' => 'string', 'year' => 'int', 'some_object' => Object::class ]; } /** * @return array */ public function getRequiredFields() { return [ 'title', 'year' ]; } /** * If you want to allow setting undefined properties, set it True! * @return bool */ protected function isUndefinedSettingAllowed() { return false; } }
- 为了方便使用并在IDE中获取字段设置器和获取器的建议,我强烈建议编写类级别的文档,就像上面的示例一样。
使用
$book = new Book(["Title" => "Advanced PHP Programming"]); // Its case-insensitive and auto case-correcter!! :) $book->setAuthor("George Schlossnagle"); $book["Year"] = 2004; $obj = new Object(); $obj->Foo = "bar"; $obj["foO"] = "BAR"; // Yessss its case-insensitive and auto case-correcter!! ["Foo": "BAR"] $book->set("some_object",$obj); // Several access methods!! Use these as you want!! print $book; // It's echoable!! //$book->toJson(); // Jsonable :) //$book->toArray(); // Arrayable :) foreach ($book as $key => $value) { // Iterateable :) echo "key: $key , value: $value \n"; } /* Out: {"title":"Advanced PHP Programming","author":"George Schlossnagle","year":2004,"some_object":{"Foo":"BAR"}} key: title , value: Advanced PHP Programming key: author , value: George Schlossnagle key: year , value: 2004 key: some_object , value: {"Foo":"BAR"} */
使用:ArrayList
可以直接实例化它,并扩展它进行定制。仅用于实例化、设置类型和使用!!
基本使用
//Scalar: $strArr = new ArrayList('string'); $strArr[] = "correct!"; $strArr[] = 123; //PHP Fatal error: Uncaught exception 'Php_Strict\Exceptions\InvalidItemTypeException' with message 'Invalid item type exception: Expected type was "string" given "integer"!' print $strArr->toJson(); //Object: $obj = new Object(); $obj->Foo = "bar"; $obj["foO"] = "BAR"; $arr = new ArrayList(Object::class); //$arr[0] = 11; //PHP Fatal error: Uncaught exception 'Php_Strict\Exceptions\InvalidItemTypeException' with message 'Invalid item type exception: Expected type was "Php_Strict\Object" given "integer"!' $arr[0] = $obj; $obj->foo = "bar"; $arr[1] = $obj; $arr[0]->baz = "bag"; $arr[2] = $obj; print $arr . "\n"; print $obj; // Its by-reference!
高级使用
use Php_Strict\ArrayList; class BookArrayList extends ArrayList { /** * @param array $items * @return BookArrayList */ public static function newLib(array $items = []) { return (new self($items)); } /** * BookArrayList constructor. * @param array $items */ public function __construct(array $items = []) { parent::__construct(Book::class, $items); } /** * Validate all of items using Object->validate() and ArrayList each() * @throws \Exception * @return null|true */ public function validate() { parent::each(function (Book $item, $key) { // ArrayList has each(closure) method! :) try { $item->validate(); } catch (\Exception $exception) { throw new \Exception(sprintf('ArrayList validation failed at offset (%s) with message: %s' , $key, $exception->getMessage())); } }); return true; } }
使用
$book = new Book(["Title" => "Advanced PHP Programming"]); $book->setAuthor("George Schlossnagle"); $bookArr = BookArrayList::newLib(); $bookArr[] = $book; $bookArr->validate(); // PHP Fatal error: Uncaught exception 'Exception' with message 'ArrayList validation failed at offset (0) with message: Required fields are not filled. unfilled required fields: (["year"])' foreach ($bookArr as $book) { // Iterateable :) echo "item: $book \n"; }
许可证
此库在MIT许可证下发布。