ไลบรารี php อย่างง่ายซึ่งมีการยืนยันและวิธีการป้องกันสำหรับการตรวจสอบความถูกต้องอินพุต (ไม่มีการกรอง!) ในรูปแบบธุรกิจ ไลบรารี และโค้ดระดับต่ำของแอปพลิเคชัน ไลบรารีสามารถใช้เพื่อนำเงื่อนไขก่อน/หลังไปใช้กับข้อมูลอินพุตได้
แนวคิดคือการลดจำนวนโค้ดสำหรับการนำการยืนยันไปใช้ในโมเดลของคุณ และยังลดความซับซ้อนของพาธโค้ดเพื่อใช้การยืนยันอีกด้วย เมื่อการยืนยันล้มเหลว จะมีข้อยกเว้นเกิดขึ้น โดยลบความจำเป็นสำหรับ if-clause ในโค้ดของคุณ
ไลบรารีไม่ได้ใช้ Symfony หรือ Zend Validators ด้วยเหตุผล: การตรวจสอบต้องเป็นโค้ดระดับต่ำ รวดเร็ว และไม่เชิงวัตถุเพื่อนำไปใช้ทุกที่ที่จำเป็น การใช้ไลบรารีใดไลบรารีหนึ่งจากทั้งสองไลบรารีนั้นจำเป็นต้องมีการสร้างอ็อบเจ็กต์หลาย ๆ อันโดยใช้ส่วนประกอบโลแคล การแปล และอื่นๆ มันบวมมากเกินไป
การใช้ผู้แต่ง:
composer require beberlei/assert
<?php
use Assert Assertion ;
function duplicateFile ( $ file , $ times )
{
Assertion :: file ( $ file );
Assertion :: digit ( $ times );
for ( $ i = 0 ; $ i < $ times ; $ i ++) {
copy ( $ file , $ file . $ i );
}
}
การใช้งานแบบเรียลไทม์ด้วย Azure Blob Storage:
<?php
public function putBlob ( $ containerName = '' , $ blobName = '' , $ localFileName = '' , $ metadata = array (), $ leaseId = null , $ additionalHeaders = array ())
{
Assertion :: notEmpty ( $ containerName , ' Container name is not specified ' );
self :: assertValidContainerName ( $ containerName );
Assertion :: notEmpty ( $ blobName , ' Blob name is not specified. ' );
Assertion :: notEmpty ( $ localFileName , ' Local file name is not specified. ' );
Assertion :: file ( $ localFileName , ' Local file name is not specified. ' );
self :: assertValidRootContainerBlobName ( $ containerName , $ blobName );
// Check file size
if ( filesize ( $ localFileName ) >= self :: MAX_BLOB_SIZE ) {
return $ this -> putLargeBlob ( $ containerName , $ blobName , $ localFileName , $ metadata , $ leaseId , $ additionalHeaders );
}
// Put the data to Windows Azure Storage
return $ this -> putBlobData ( $ containerName , $ blobName , file_get_contents ( $ localFileName ), $ metadata , $ leaseId , $ additionalHeaders );
}
วิธีการช่วยเหลือ ( Assertion::nullOr*
) มีไว้เพื่อตรวจสอบว่าค่าเป็นโมฆะหรือคงไว้สำหรับการยืนยัน:
<?php
Assertion :: nullOrMax ( null , 42 ); // success
Assertion :: nullOrMax ( 1 , 42 ); // success
Assertion :: nullOrMax ( 1337 , 42 ); // exception
Assertion::all*
วิธีการตรวจสอบว่าค่าที่ให้ไว้ทั้งหมดมีไว้สำหรับการยืนยันหรือไม่ มันจะส่งข้อยกเว้นของการยืนยันที่ไม่ถือเป็นค่าใดค่าหนึ่ง:
<?php
Assertion :: allIsInstanceOf ( array ( new stdClass, new stdClass), ' stdClass ' ); // success
Assertion :: allIsInstanceOf ( array ( new stdClass, new stdClass), ' PDO ' ); // exception
การใช้ API แบบคงที่กับค่าต่างๆ นั้นละเอียดมากเมื่อตรวจสอบค่ากับการยืนยันหลายรายการ เริ่มต้นด้วย Assert 2.6.7 คลาส Assert
จะให้ API ที่แม่นยำยิ่งขึ้นสำหรับการยืนยัน โดยเริ่มจาก Assert::that($value)
จากนั้นรับการยืนยันที่คุณต้องการเรียกใช้บนอินเทอร์เฟซที่คล่องแคล่ว คุณจะต้องระบุ $value
เพียงครั้งเดียว
<?php
Assert :: that ( $ value )-> notEmpty ()-> integer ();
Assert :: that ( $ value )-> nullOr ()-> string ()-> startsWith ( " Foo " );
Assert :: that ( $ values )-> all ()-> float ();
นอกจากนี้ยังมีฟังก์ชันทางลัดสองฟังก์ชัน Assert::thatNullOr()
และ Assert::thatAll()
เปิดใช้งานตัวช่วย "nullOr" หรือ "all" ตามลำดับ
มีหลายกรณีในการพัฒนาเว็บ โดยเฉพาะอย่างยิ่งเมื่อเกี่ยวข้องกับแบบฟอร์ม คุณต้องการรวบรวมข้อผิดพลาดต่างๆ แทนที่จะยกเลิกข้อผิดพลาดแรกโดยตรง นี่คือสิ่งที่การยืนยันที่ขี้เกียจมีไว้เพื่อ API ของพวกเขาทำงานเหมือนกับ Assert::that()
API ได้อย่างคล่องแคล่ว แต่แทนที่จะส่ง Exception โดยตรง พวกเขารวบรวมข้อผิดพลาดทั้งหมดและทริกเกอร์ข้อยกเว้นเมื่อมีการเรียกใช้เมธอด verifyNow()
บนอ็อบเจ็กต์ AssertSoftAssertion
เท่านั้น
<?php
Assert :: lazy ()
-> that ( 10 , ' foo ' )-> string ()
-> that ( null , ' bar ' )-> notEmpty ()
-> that ( ' string ' , ' baz ' )-> isArray ()
-> verifyNow ();
วิธีการ that($value, $propertyPath)
ต้องการเส้นทางคุณสมบัติ (ชื่อ) เพื่อให้คุณทราบวิธีแยกแยะข้อผิดพลาดในภายหลัง
เมื่อเกิดความล้มเหลว verifyNow()
จะส่งข้อยกเว้น Assert\LazyAssertionException
พร้อมข้อความรวม:
The following 3 assertions failed:
1) foo: Value "10" expected to be string, type integer given.
2) bar: Value "<NULL>" is empty, but non empty value was expected.
3) baz: Value "string" is not an array.
คุณยังสามารถดึงข้อมูล AssertionFailedException
ทั้งหมดได้โดยการเรียก getErrorExceptions()
สิ่งนี้อาจมีประโยชน์ เช่น เพื่อสร้างการตอบสนองต่อความล้มเหลวสำหรับผู้ใช้
สำหรับผู้ที่ต้องการบันทึกข้อผิดพลาดหลายรายการในค่าเดียวเมื่อใช้สายการยืนยันแบบ Lazy คุณอาจติดตามการเรียกของคุณ that
ใช้ tryAll
เพื่อดำเนินการยืนยันทั้งหมดกับค่า และบันทึกข้อความแสดงข้อผิดพลาดในการยืนยันที่ล้มเหลวทั้งหมด นี่คือตัวอย่าง:
Assert :: lazy ()
-> that ( 10 , ' foo ' )-> tryAll ()-> integer ()-> between ( 5 , 15 )
-> that ( null , ' foo ' )-> tryAll ()-> notEmpty ()-> string ()
-> verifyNow ();
ข้างต้นแสดงวิธีใช้ฟังก์ชันนี้เพื่อปรับแต่งพฤติกรรมการรายงานความล้มเหลวอย่างละเอียด แต่เพื่อให้การตรวจจับความล้มเหลวทั้งหมดง่ายยิ่งขึ้น คุณยังอาจเรียก tryAll
ก่อนที่จะทำการยืนยันใดๆ ดังด้านล่าง ซึ่งจะช่วยลดการเรียกใช้เมธอด และมีลักษณะการทำงานเหมือนกับข้างต้น
Assert :: lazy ()-> tryAll ()
-> that ( 10 , ' foo ' )-> integer ()-> between ( 5 , 15 )
-> that ( null , ' foo ' )-> notEmpty ()-> string ()
-> verifyNow ();
ฟังก์ชั่นต่อไปนี้มีอยู่เป็นนามแฝงของ Assert
static Constructor:
Assertthat()
AssertthatAll()
AssertthatNullOr()
Assertlazy()
การใช้ตัวสร้างฟังก์ชันหรือแบบคงที่นั้นเป็นความชอบส่วนตัวโดยสิ้นเชิง
หมายเหตุ: ตัวสร้างการทำงานจะไม่ทำงานกับส่วนขยาย Assertion
อย่างไรก็ตาม มันเป็นเรื่องเล็กน้อยที่จะสร้างฟังก์ชันเหล่านี้ขึ้นใหม่ในลักษณะที่ชี้ไปที่คลาสขยาย
<?php
use Assert Assertion ;
Assertion :: alnum (mixed $ value );
Assertion :: base64 (string $ value );
Assertion :: between (mixed $ value , mixed $ lowerLimit , mixed $ upperLimit );
Assertion :: betweenExclusive (mixed $ value , mixed $ lowerLimit , mixed $ upperLimit );
Assertion :: betweenLength (mixed $ value , int $ minLength , int $ maxLength );
Assertion :: boolean (mixed $ value );
Assertion :: choice (mixed $ value , array $ choices );
Assertion :: choicesNotEmpty (array $ values , array $ choices );
Assertion :: classExists (mixed $ value );
Assertion :: contains (mixed $ string , string $ needle );
Assertion ::count(array| Countable | ResourceBundle | SimpleXMLElement $ countable , int $ count );
Assertion :: date (string $ value , string $ format );
Assertion :: defined (mixed $ constant );
Assertion :: digit (mixed $ value );
Assertion :: directory (string $ value );
Assertion :: e164 (string $ value );
Assertion :: email (mixed $ value );
Assertion :: endsWith (mixed $ string , string $ needle );
Assertion :: eq (mixed $ value , mixed $ value2 );
Assertion :: eqArraySubset (mixed $ value , mixed $ value2 );
Assertion :: extensionLoaded (mixed $ value );
Assertion :: extensionVersion (string $ extension , string $ operator , mixed $ version );
Assertion :: false (mixed $ value );
Assertion :: file (string $ value );
Assertion :: float (mixed $ value );
Assertion :: greaterOrEqualThan (mixed $ value , mixed $ limit );
Assertion :: greaterThan (mixed $ value , mixed $ limit );
Assertion :: implementsInterface (mixed $ class , string $ interfaceName );
Assertion :: inArray (mixed $ value , array $ choices );
Assertion :: integer (mixed $ value );
Assertion :: integerish (mixed $ value );
Assertion :: interfaceExists (mixed $ value );
Assertion :: ip (string $ value , int $ flag = null );
Assertion :: ipv4 (string $ value , int $ flag = null );
Assertion :: ipv6 (string $ value , int $ flag = null );
Assertion :: isArray (mixed $ value );
Assertion :: isArrayAccessible (mixed $ value );
Assertion :: isCallable (mixed $ value );
Assertion ::isCountable(array| Countable | ResourceBundle | SimpleXMLElement $ value );
Assertion :: isInstanceOf (mixed $ value , string $ className );
Assertion :: isJsonString (mixed $ value );
Assertion :: isObject (mixed $ value );
Assertion :: isResource (mixed $ value );
Assertion :: isTraversable (mixed $ value );
Assertion :: keyExists (mixed $ value , string|int $ key );
Assertion :: keyIsset (mixed $ value , string|int $ key );
Assertion :: keyNotExists (mixed $ value , string|int $ key );
Assertion :: length (mixed $ value , int $ length );
Assertion :: lessOrEqualThan (mixed $ value , mixed $ limit );
Assertion :: lessThan (mixed $ value , mixed $ limit );
Assertion :: max (mixed $ value , mixed $ maxValue );
Assertion ::maxCount(array| Countable | ResourceBundle | SimpleXMLElement $ countable , int $ count );
Assertion :: maxLength (mixed $ value , int $ maxLength );
Assertion :: methodExists (string $ value , mixed $ object );
Assertion :: min (mixed $ value , mixed $ minValue );
Assertion ::minCount(array| Countable | ResourceBundle | SimpleXMLElement $ countable , int $ count );
Assertion :: minLength (mixed $ value , int $ minLength );
Assertion :: noContent (mixed $ value );
Assertion :: notBlank (mixed $ value );
Assertion :: notContains (mixed $ string , string $ needle );
Assertion :: notEmpty (mixed $ value );
Assertion :: notEmptyKey (mixed $ value , string|int $ key );
Assertion :: notEq (mixed $ value1 , mixed $ value2 );
Assertion :: notInArray (mixed $ value , array $ choices );
Assertion :: notIsInstanceOf (mixed $ value , string $ className );
Assertion :: notNull (mixed $ value );
Assertion :: notRegex (mixed $ value , string $ pattern );
Assertion :: notSame (mixed $ value1 , mixed $ value2 );
Assertion :: null (mixed $ value );
Assertion :: numeric (mixed $ value );
Assertion :: objectOrClass (mixed $ value );
Assertion :: phpVersion (string $ operator , mixed $ version );
Assertion :: propertiesExist (mixed $ value , array $ properties );
Assertion :: propertyExists (mixed $ value , string $ property );
Assertion :: range (mixed $ value , mixed $ minValue , mixed $ maxValue );
Assertion :: readable (string $ value );
Assertion :: regex (mixed $ value , string $ pattern );
Assertion :: same (mixed $ value , mixed $ value2 );
Assertion :: satisfy (mixed $ value , callable $ callback );
Assertion :: scalar (mixed $ value );
Assertion :: startsWith (mixed $ string , string $ needle );
Assertion :: string (mixed $ value );
Assertion :: subclassOf (mixed $ value , string $ className );
Assertion :: true (mixed $ value );
Assertion :: uniqueValues (array $ values );
Assertion :: url (mixed $ value );
Assertion :: uuid (string $ value );
Assertion :: version (string $ version1 , string $ operator , string $ version2 );
Assertion :: writeable (string $ value );
ข้อควรจำ: เมื่อจำเป็นต้องมีพารามิเตอร์การกำหนดค่า พารามิเตอร์จะถูกส่งผ่านหลังค่าเสมอ ค่าจะเป็นพารามิเตอร์แรกเสมอ
หากการยืนยันใดๆ ล้มเหลว AssertAssertionFailedException
จะถูกส่งออกไป คุณสามารถส่งผ่านอาร์กิวเมนต์ที่เรียกว่า $message
ไปยังการยืนยันใดๆ เพื่อควบคุมข้อความยกเว้นได้ ทุกข้อยกเว้นประกอบด้วยข้อความเริ่มต้นและรหัสข้อความที่ไม่ซ้ำกันตามค่าเริ่มต้น
<?php
use Assert Assertion ;
use Assert AssertionFailedException ;
try {
Assertion :: integer ( $ value , " The pressure of gas is measured in integers. " );
} catch ( AssertionFailedException $ e ) {
// error handling
$ e -> getValue (); // the value that caused the failure
$ e -> getConstraints (); // the additional constraints of the assertion.
}
AssertAssertionFailedException
เป็นเพียงอินเทอร์เฟซและการใช้งานเริ่มต้นคือ AssertInvalidArgumentException
ซึ่งขยาย SPL InvalidArgumentException
คุณสามารถเปลี่ยนข้อยกเว้นที่ใช้ในระดับตามแพ็คเกจได้
คุณสามารถส่งการโทรกลับเป็นพารามิเตอร์ข้อความได้ ซึ่งช่วยให้คุณสร้างข้อความของคุณเองได้ก็ต่อเมื่อการยืนยันล้มเหลว แทนที่จะทำทุกครั้งที่คุณรันการทดสอบ
การโทรกลับจะมาพร้อมกับอาร์เรย์ของพารามิเตอร์ที่ใช้สำหรับการยืนยัน
เนื่องจากการยืนยันบางอย่างเรียกการยืนยันอื่น การเรียกกลับของคุณจะต้องยกตัวอย่างอาร์เรย์เพื่อพิจารณาว่าการยืนยันใดล้มเหลว
อาร์เรย์จะมีคีย์ที่เรียกว่า ::assertion
ซึ่งระบุว่าการยืนยันใดล้มเหลว
การโทรกลับควรส่งคืนสตริงที่จะใช้เป็นข้อความข้อยกเว้น
เพื่อปกป้องไลบรารีของคุณจากข้อบกพร่องที่เป็นไปได้ การตีความที่ผิด หรือการแตก BC ภายใน Assert คุณควรแนะนำคลาสย่อยการยืนยันตามไลบรารี/โปรเจ็กต์ ซึ่งคุณสามารถแทนที่ข้อยกเว้นที่เกิดขึ้นได้เช่นกัน
นอกจากนี้ คุณยังสามารถแทนที่เมธอด AssertAssertion::stringify()
เพื่อให้การตีความประเภทของคุณเองในระหว่างการจัดการข้อผิดพลาด
<?php
namespace MyProject ;
use Assert Assertion as BaseAssertion ;
class Assertion extends BaseAssertion
{
protected static $ exceptionClass = ' MyProjectAssertionFailedException ' ;
}
ตั้งแต่เวอร์ชัน 2.9.2 เป็นต้นไป Lazy Assertions สามารถเข้าถึงการยืนยันเพิ่มเติมใดๆ ที่มีอยู่ในคลาสการยืนยันของคุณเอง
โปรดดูการมีส่วนร่วมสำหรับรายละเอียดเพิ่มเติม