การดึงข้อมูลที่มีโครงสร้างใน PHP ขับเคลื่อนโดย LLM ออกแบบมาเพื่อความเรียบง่าย โปร่งใส และการควบคุม
ผู้สอนเป็นไลบรารีที่ช่วยให้คุณสามารถดึงข้อมูลที่มีโครงสร้างและตรวจสอบได้จากอินพุตหลายประเภท: ข้อความ รูปภาพ หรืออาร์เรย์ลำดับการสนทนาสไตล์ OpenAI ขับเคลื่อนโดย Large Language Models (LLM)
ผู้สอนทำให้การรวม LLM ในโครงการ PHP ง่ายขึ้น โดยจะจัดการกับความซับซ้อนในการแยกข้อมูลที่มีโครงสร้างออกจากเอาท์พุต LLM คุณจึงสามารถมุ่งเน้นไปที่การสร้างตรรกะของแอปพลิเคชันและทำซ้ำได้เร็วขึ้น
ผู้สอนสำหรับ PHP ได้รับแรงบันดาลใจจากไลบรารีผู้สอนสำหรับ Python ที่สร้างโดย Jason Liu
นี่คือแอปสาธิต CLI ง่ายๆ ที่ใช้ Instructor เพื่อดึงข้อมูลที่มีโครงสร้างจากข้อความ:
Structure
ตรวจสอบการใช้งานในภาษาอื่นๆ ด้านล่าง:
หากคุณต้องการย้ายผู้สอนเป็นภาษาอื่น โปรดติดต่อเราทาง Twitter เรายินดีที่จะช่วยคุณเริ่มต้น!
ผู้สอนแนะนำการปรับปรุงที่สำคัญสามประการเมื่อเปรียบเทียบกับการใช้งาน API โดยตรง
คุณเพียงแค่ระบุคลาส PHP เพื่อดึงข้อมูลเข้ามาผ่านทาง 'เวทย์มนตร์' ของการแชท LLM เสร็จสิ้น และนั่นมัน
ผู้สอนลดความเปราะบางของโค้ดที่ดึงข้อมูลจากข้อมูลต้นฉบับโดยใช้ประโยชน์จากการตอบสนอง LLM ที่มีโครงสร้าง
ผู้ฝึกสอนช่วยให้คุณเขียนโค้ดที่เรียบง่ายและเข้าใจง่ายขึ้น - คุณไม่จำเป็นต้องกำหนดคำจำกัดความการเรียกใช้ฟังก์ชันที่ยาวหรือเขียนโค้ดเพื่อกำหนด JSON ที่ส่งคืนให้กับออบเจ็กต์ข้อมูลเป้าหมายอีกต่อไป
โมเดลการตอบสนองที่สร้างโดย LLM สามารถตรวจสอบได้โดยอัตโนมัติตามกฎชุดหนึ่ง ปัจจุบันผู้สอนรองรับเฉพาะการตรวจสอบ Symfony เท่านั้น
คุณยังสามารถจัดเตรียมออบเจ็กต์บริบทเพื่อใช้ความสามารถของเครื่องมือตรวจสอบที่ได้รับการปรับปรุงได้
คุณสามารถกำหนดจำนวนครั้งในการลองอีกครั้งสำหรับคำขอได้
ผู้สอนจะทำซ้ำคำขอในกรณีที่มีข้อผิดพลาดในการตรวจสอบความถูกต้องหรือดีซีเรียลไลซ์ตามจำนวนครั้งที่ระบุ โดยพยายามรับการตอบกลับที่ถูกต้องจาก LLM
การติดตั้ง Instructor นั้นง่ายมาก เรียกใช้คำสั่งต่อไปนี้ในเทอร์มินัลของคุณ และคุณจะได้รับประสบการณ์การจัดการข้อมูลที่ราบรื่นยิ่งขึ้น!
composer require cognesy/instructor-php
นี่คือตัวอย่างง่ายๆ ที่แสดงให้เห็นว่าผู้สอนดึงข้อมูลที่มีโครงสร้างจากข้อความที่ให้มา (หรือลำดับข้อความแชท) อย่างไร
คลาสโมเดลการตอบสนองเป็นคลาส PHP ธรรมดาพร้อมคำแนะนำการพิมพ์ที่ระบุประเภทของฟิลด์ของออบเจ็กต์
use Cognesy Instructor Instructor ;
// Step 0: Create .env file in your project root:
// OPENAI_API_KEY=your_api_key
// Step 1: Define target data structure(s)
class Person {
public string $ name ;
public int $ age ;
}
// Step 2: Provide content to process
$ text = " His name is Jason and he is 28 years old. " ;
// Step 3: Use Instructor to run LLM inference
$ person = ( new Instructor )-> respond (
messages: $ text ,
responseModel: Person ::class,
);
// Step 4: Work with structured response data
assert ( $ person instanceof Person ); // true
assert ( $ person -> name === ' Jason ' ); // true
assert ( $ person -> age === 28 ); // true
echo $ person -> name ; // Jason
echo $ person -> age ; // 28
var_dump ( $ person );
// Person {
// name: "Jason",
// age: 28
// }
หมายเหตุ: ผู้สอนสนับสนุนคลาส / วัตถุเป็นแบบจำลองการตอบสนอง ในกรณีที่คุณต้องการแยกประเภทหรือแจงนับแบบง่าย คุณต้องรวมพวกมันไว้ในอะแดปเตอร์สเกลาร์ - ดูหัวข้อด้านล่าง: การแยกค่าสเกลาร์
ผู้สอนอนุญาตให้คุณกำหนดการเชื่อมต่อ API หลายรายการในไฟล์ llm.php
สิ่งนี้มีประโยชน์เมื่อคุณต้องการใช้ LLM หรือผู้ให้บริการ API ที่แตกต่างกันในแอปพลิเคชันของคุณ
การกำหนดค่าเริ่มต้นอยู่ใน /config/llm.php
ในไดเร็กทอรีรากของ Instructor codebase ประกอบด้วยชุดการเชื่อมต่อที่กำหนดไว้ล่วงหน้ากับ LLM API ทั้งหมดที่รองรับโดยผู้สอน
ไฟล์กำหนดค่ากำหนดการเชื่อมต่อกับ LLM API และพารามิเตอร์ นอกจากนี้ยังระบุการเชื่อมต่อเริ่มต้นที่จะใช้เมื่อโทรหาผู้สอนโดยไม่ระบุการเชื่อมต่อไคลเอ็นต์
// This is fragment of /config/llm.php file
' defaultConnection ' => ' openai ' ,
// . . .
' connections ' => [
' anthropic ' => [ ... ],
' azure ' => [ ... ],
' cohere1 ' => [ ... ],
' cohere2 ' => [ ... ],
' fireworks ' => [ ... ],
' gemini ' => [ ... ],
' grok ' => [ ... ],
' groq ' => [ ... ],
' mistral ' => [ ... ],
' ollama ' => [
' providerType ' => LLMProviderType :: Ollama -> value ,
' apiUrl ' => ' http://localhost:11434/v1 ' ,
' apiKey ' => Env :: get ( ' OLLAMA_API_KEY ' , '' ),
' endpoint ' => ' /chat/completions ' ,
' defaultModel ' => ' qwen2.5:0.5b ' ,
' defaultMaxTokens ' => 1024 ,
' httpClient ' => ' guzzle-ollama ' , // use custom HTTP client configuration
],
' openai ' => [ ... ],
' openrouter ' => [ ... ],
' together ' => [ ... ],
// ...
หากต้องการปรับแต่งการเชื่อมต่อที่มีอยู่ คุณสามารถแก้ไขรายการที่มีอยู่หรือเพิ่มของคุณเองได้
การเชื่อมต่อกับ LLM API ผ่านการเชื่อมต่อที่กำหนดไว้ล่วงหน้าทำได้ง่ายเพียงแค่เรียกใช้เมธอด withClient
ด้วยชื่อการเชื่อมต่อ
<?php
// ...
$ user = ( new Instructor )
-> withConnection ( ' ollama ' )
-> respond (
messages: " His name is Jason and he is 28 years old. " ,
responseModel: Person ::class,
);
// ...
คุณสามารถเปลี่ยนตำแหน่งของไฟล์การกำหนดค่าเพื่อให้ผู้สอนใช้ผ่านตัวแปรสภาพแวดล้อม INSTRUCTOR_CONFIG_PATH
ได้ คุณสามารถใช้สำเนาของไฟล์การกำหนดค่าเริ่มต้นเป็นจุดเริ่มต้นได้
ผู้สอนเสนอวิธีใช้ข้อมูลที่มีโครงสร้างเป็นอินพุต สิ่งนี้มีประโยชน์เมื่อคุณต้องการใช้ข้อมูลออบเจ็กต์เป็นอินพุต และรับออบเจ็กต์อื่นที่เป็นผลจากการอนุมาน LLM
ช่อง input
ของเมธอด respond()
และ request()
ของผู้สอนสามารถเป็นอ็อบเจ็กต์ได้ แต่ยังเป็นอาร์เรย์หรือเพียงสตริงก็ได้
<?php
use Cognesy Instructor Instructor ;
class Email {
public function __construct (
public string $ address = '' ,
public string $ subject = '' ,
public string $ body = '' ,
) {}
}
$ email = new Email (
address: ' joe@gmail ' ,
subject: ' Status update ' ,
body: ' Your account has been updated. '
);
$ translation = ( new Instructor )-> respond (
input: $ email ,
responseModel: Email ::class,
prompt: ' Translate the text fields of email to Spanish. Keep other fields unchanged. ' ,
);
assert ( $ translation instanceof Email ); // true
dump ( $ translation );
// Email {
// address: "joe@gmail",
// subject: "Actualización de estado",
// body: "Su cuenta ha sido actualizada."
// }
?>
ผู้สอนตรวจสอบผลลัพธ์ของการตอบสนอง LLM กับกฎการตรวจสอบที่ระบุในแบบจำลองข้อมูลของคุณ
สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับกฎการตรวจสอบที่มีอยู่ ให้ตรวจสอบข้อจำกัดการตรวจสอบของ Symfony
use Symfony Component Validator Constraints as Assert ;
class Person {
public string $ name ;
#[ Assert PositiveOrZero ]
public int $ age ;
}
$ text = " His name is Jason, he is -28 years old. " ;
$ person = ( new Instructor )-> respond (
messages: [[ ' role ' => ' user ' , ' content ' => $ text ]],
responseModel: Person ::class,
);
// if the resulting object does not validate, Instructor throws an exception
ในกรณีที่ระบุพารามิเตอร์ maxRetries และการตอบสนองของ LLM ไม่ตรงตามเกณฑ์การตรวจสอบ ผู้สอนจะพยายามอนุมานในภายหลังจนกว่าผลลัพธ์จะตรงตามข้อกำหนดหรือถึง maxRetries
ผู้สอนใช้ข้อผิดพลาดในการตรวจสอบความถูกต้องเพื่อแจ้ง LLM เกี่ยวกับปัญหาที่ระบุในการตอบกลับ เพื่อให้ LLM สามารถลองแก้ไขตนเองในความพยายามครั้งต่อไป
use Symfony Component Validator Constraints as Assert ;
class Person {
#[ Assert Length (min: 3 )]
public string $ name ;
#[ Assert PositiveOrZero ]
public int $ age ;
}
$ text = " His name is JX, aka Jason, he is -28 years old. " ;
$ person = ( new Instructor )-> respond (
messages: [[ ' role ' => ' user ' , ' content ' => $ text ]],
responseModel: Person ::class,
maxRetries: 3 ,
);
// if all LLM's attempts to self-correct the results fail, Instructor throws an exception
คุณสามารถเรียกเมธอด request()
เพื่อตั้งค่าพารามิเตอร์ของคำขอแล้วเรียก get()
เพื่อรับการตอบกลับ
use Cognesy Instructor Instructor ;
$ instructor = ( new Instructor )-> request (
messages: " His name is Jason, he is 28 years old. " ,
responseModel: Person ::class,
);
$ person = $ instructor -> get ();
ผู้สอนรองรับการสตรีมผลลัพธ์บางส่วน ทำให้คุณสามารถเริ่มประมวลผลข้อมูลได้ทันทีที่พร้อมใช้งาน
<?php
use Cognesy Instructor Instructor ;
$ stream = ( new Instructor )-> request (
messages: " His name is Jason, he is 28 years old. " ,
responseModel: Person ::class,
options: [ ' stream ' => true ]
)-> stream ();
foreach ( $ stream as $ partialPerson ) {
// process partial person data
echo $ partialPerson -> name ;
echo $ partialPerson -> age ;
}
// after streaming is done you can get the final, fully processed person object...
$ person = $ stream -> getLastUpdate ()
// . . . to, for example, save it to the database
$ db -> save ( $ person );
?>
คุณสามารถกำหนดการเรียกกลับ onPartialUpdate()
เพื่อรับผลลัพธ์บางส่วนที่สามารถใช้เพื่อเริ่มการอัปเดต UI ก่อนที่ LLM จะเสร็จสิ้นการอนุมาน
หมายเหตุ: การอัปเดตบางส่วนไม่ได้รับการตรวจสอบ การตอบกลับจะได้รับการตรวจสอบหลังจากได้รับครบถ้วนแล้วเท่านั้น
use Cognesy Instructor Instructor ;
function updateUI ( $ person ) {
// Here you get partially completed Person object update UI with the partial result
}
$ person = ( new Instructor )-> request (
messages: " His name is Jason, he is 28 years old. " ,
responseModel: Person ::class,
options: [ ' stream ' => true ]
)-> onPartialUpdate (
fn( $ partial ) => updateUI ( $ partial )
)-> get ();
// Here you get completed and validated Person object
$ this -> db -> save ( $ person ); // ...for example: save to DB
คุณสามารถระบุสตริงแทนอาร์เรย์ของข้อความได้ สิ่งนี้มีประโยชน์เมื่อคุณต้องการดึงข้อมูลจากบล็อกข้อความเดียวและต้องการให้โค้ดของคุณเรียบง่าย
// Usually, you work with sequences of messages:
$ value = ( new Instructor )-> respond (
messages: [[ ' role ' => ' user ' , ' content ' => " His name is Jason, he is 28 years old. " ]],
responseModel: Person ::class,
);
// ...but if you want to keep it simple, you can just pass a string:
$ value = ( new Instructor )-> respond (
messages: " His name is Jason, he is 28 years old. " ,
responseModel: Person ::class,
);
บางครั้งเราเพียงต้องการได้ผลลัพธ์ที่รวดเร็วโดยไม่ต้องกำหนดคลาสสำหรับโมเดลการตอบสนอง โดยเฉพาะอย่างยิ่งหากเราพยายามที่จะได้คำตอบที่ตรงไปตรงมาและเรียบง่ายในรูปแบบของสตริง จำนวนเต็ม บูลีน หรือทศนิยม ผู้สอนจัดเตรียม API แบบง่ายสำหรับกรณีดังกล่าว
use Cognesy Instructor Extras Scalar Scalar ;
use Cognesy Instructor Instructor ;
$ value = ( new Instructor )-> respond (
messages: " His name is Jason, he is 28 years old. " ,
responseModel: Scalar :: integer ( ' age ' ),
);
var_dump ( $ value );
// int(28)
ในตัวอย่างนี้ เรากำลังแยกค่าจำนวนเต็มค่าเดียวออกจากข้อความ คุณยังสามารถใช้ Scalar::string()
, Scalar::boolean()
และ Scalar::float()
เพื่อแยกค่าประเภทอื่น ๆ
นอกจากนี้ คุณสามารถใช้อะแดปเตอร์สเกลาร์เพื่อแยกหนึ่งในตัวเลือกที่ให้มาได้โดยใช้ Scalar::enum()
use Cognesy Instructor Extras Scalar Scalar ;
use Cognesy Instructor Instructor ;
enum ActivityType : string {
case Work = ' work ' ;
case Entertainment = ' entertainment ' ;
case Sport = ' sport ' ;
case Other = ' other ' ;
}
$ value = ( new Instructor )-> respond (
messages: " His name is Jason, he currently plays Doom Eternal. " ,
responseModel: Scalar :: enum ( ActivityType ::class, ' activityType ' ),
);
var_dump ( $ value );
// enum(ActivityType:Entertainment)
Sequence คือคลาส wrapper ที่สามารถใช้เพื่อแสดงรายการออบเจ็กต์ที่ผู้สอนจะแยกจากบริบทที่ให้มา
โดยทั่วไปจะสะดวกกว่าถ้าไม่สร้างคลาสเฉพาะด้วยคุณสมบัติอาร์เรย์เดียวเพียงเพื่อจัดการรายการของอ็อบเจ็กต์ของคลาสที่กำหนด
คุณสมบัติพิเศษเพิ่มเติมของลำดับคือสามารถสตรีมได้ต่อแต่ละรายการที่เสร็จสมบูรณ์ในลำดับ แทนที่จะอัปเดตคุณสมบัติใดๆ
class Person
{
public string $ name ;
public int $ age ;
}
$ text = <<<TEXT
Jason is 25 years old. Jane is 18 yo. John is 30 years old
and Anna is 2 years younger than him.
TEXT ;
$ list = ( new Instructor )-> respond (
messages: [[ ' role ' => ' user ' , ' content ' => $ text ]],
responseModel: Sequence :: of ( Person ::class),
options: [ ' stream ' => true ]
);
ดูเพิ่มเติมเกี่ยวกับลำดับในส่วนลำดับ
ใช้คำแนะนำประเภท PHP เพื่อระบุประเภทของข้อมูลที่แยกออกมา
ใช้ประเภท nullable เพื่อระบุว่าฟิลด์ที่กำหนดเป็นทางเลือก
class Person {
public string $ name ;
public ? int $ age ;
public Address $ address ;
}
คุณยังสามารถใช้ความคิดเห็นสไตล์ PHP DocBlock เพื่อระบุประเภทของข้อมูลที่แยกออกมาได้ สิ่งนี้มีประโยชน์เมื่อคุณต้องการระบุประเภทคุณสมบัติสำหรับ LLM แต่ไม่สามารถหรือไม่ต้องการบังคับใช้ประเภทในระดับรหัส
class Person {
/** @var string */
public $ name ;
/** @var int */
public $ age ;
/** @var Address $address person's address */
public $ address ;
}
ดูเอกสาร PHPDoc สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับเว็บไซต์ DocBlock
ปัจจุบัน PHP ไม่รองรับข้อมูลทั่วไปหรือคำแนะนำการพิมพ์เพื่อระบุประเภทองค์ประกอบอาร์เรย์
ใช้ความคิดเห็นสไตล์ PHP DocBlock เพื่อระบุประเภทขององค์ประกอบอาร์เรย์
class Person {
// ...
}
class Event {
// ...
/** @var Person[] list of extracted event participants */
public array $ participants ;
// ...
}
ผู้สอนสามารถดึงโครงสร้างข้อมูลที่ซับซ้อนจากข้อความได้ โมเดลการตอบสนองของคุณสามารถมีอ็อบเจ็กต์ที่ซ้อนกัน อาร์เรย์ และแจงนับได้
use Cognesy Instructor Instructor ;
// define a data structures to extract data into
class Person {
public string $ name ;
public int $ age ;
public string $ profession ;
/** @var Skill[] */
public array $ skills ;
}
class Skill {
public string $ name ;
public SkillType $ type ;
}
enum SkillType {
case Technical = ' technical ' ;
case Other = ' other ' ;
}
$ text = " Alex is 25 years old software engineer, who knows PHP, Python and can play the guitar. " ;
$ person = ( new Instructor )-> respond (
messages: [[ ' role ' => ' user ' , ' content ' => $ text ]],
responseModel: Person ::class,
); // client is passed explicitly, can specify e.g. different base URL
// data is extracted into an object of given class
assert ( $ person instanceof Person ); // true
// you can access object's extracted property values
echo $ person -> name ; // Alex
echo $ person -> age ; // 25
echo $ person -> profession ; // software engineer
echo $ person -> skills [ 0 ]-> name ; // PHP
echo $ person -> skills [ 0 ]-> type ; // SkillType::Technical
// ...
var_dump ( $ person );
// Person {
// name: "Alex",
// age: 25,
// profession: "software engineer",
// skills: [
// Skill {
// name: "PHP",
// type: SkillType::Technical,
// },
// Skill {
// name: "Python",
// type: SkillType::Technical,
// },
// Skill {
// name: "guitar",
// type: SkillType::Other
// },
// ]
// }
หากคุณต้องการกำหนดรูปร่างของข้อมูลระหว่างรันไทม์ คุณสามารถใช้คลาส Structure
ได้
โครงสร้างช่วยให้คุณสามารถกำหนดและแก้ไขรูปร่างของข้อมูลที่ต้องการแยกโดย LLM ได้ตามใจชอบ คลาสอาจไม่เหมาะสมที่สุดสำหรับจุดประสงค์นี้ เนื่องจากไม่สามารถประกาศหรือเปลี่ยนแปลงคลาสระหว่างการดำเนินการได้
ด้วยโครงสร้าง คุณสามารถกำหนดรูปร่างข้อมูลแบบกำหนดเองได้แบบไดนามิก เช่น ตามอินพุตของผู้ใช้หรือบริบทของการประมวลผล เพื่อระบุข้อมูลที่คุณต้องการให้ LLM อนุมานจากข้อความหรือข้อความแชทที่ให้มา
ตัวอย่างด้านล่างสาธิตวิธีการกำหนดโครงสร้างและใช้เป็นแบบจำลองการตอบสนอง:
<?php
use Cognesy Instructor Extras Structure Field ;
use Cognesy Instructor Extras Structure Structure ;
enum Role : string {
case Manager = ' manager ' ;
case Line = ' line ' ;
}
$ structure = Structure :: define ( ' person ' , [
Field :: string ( ' name ' ),
Field :: int ( ' age ' ),
Field :: enum ( ' role ' , Role ::class),
]);
$ person = ( new Instructor )-> respond (
messages: ' Jason is 25 years old and is a manager. ' ,
responseModel: $ structure ,
);
// you can access structure data via field API...
assert ( $ person -> field ( ' name ' ) === ' Jason ' );
// ...or as structure object properties
assert ( $ person -> age === 25 );
?>
สำหรับข้อมูลเพิ่มเติม โปรดดูส่วนโครงสร้าง
คุณสามารถระบุรุ่นและตัวเลือกอื่นๆ ที่จะส่งต่อไปยังตำแหน่งข้อมูล OpenAI / LLM
use Cognesy Instructor Features LLM Data LLMConfig ;
use Cognesy Instructor Features LLM Drivers OpenAIDriver ;
use Cognesy Instructor Instructor ;
// OpenAI auth params
$ yourApiKey = Env :: get ( ' OPENAI_API_KEY ' ); // use your own API key
// Create instance of OpenAI driver initialized with custom parameters
$ driver = new OpenAIDriver ( new LLMConfig (
apiUrl: ' https://api.openai.com/v1 ' , // you can change base URI
apiKey: $ yourApiKey ,
endpoint: ' /chat/completions ' ,
metadata: [ ' organization ' => '' ],
model: ' gpt-4o-mini ' ,
maxTokens: 128 ,
));
/// Get Instructor with the default client component overridden with your own
$ instructor = ( new Instructor )-> withDriver ( $ driver );
$ user = $ instructor -> respond (
messages: " Jason (@jxnlco) is 25 years old and is the admin of this project. He likes playing football and reading books. " ,
responseModel: User ::class,
model: ' gpt-3.5-turbo ' ,
options: [ ' stream ' => true ]
);
ผู้สอนเสนอการสนับสนุนนอกกรอบสำหรับผู้ให้บริการ API ต่อไปนี้:
สำหรับตัวอย่างการใช้งาน ให้ตรวจสอบส่วนฮับหรือไดเร็กทอรี examples
ในที่เก็บโค้ด
คุณสามารถใช้ PHP DocBlocks (/** */) เพื่อให้คำแนะนำเพิ่มเติมสำหรับ LLM ในระดับชั้นเรียนหรือภาคสนาม เช่น เพื่อชี้แจงสิ่งที่คุณคาดหวังหรือวิธีที่ LLM ควรประมวลผลข้อมูลของคุณ
ผู้สอนแยกความคิดเห็น PHP DocBlocks จากคลาสและคุณสมบัติที่กำหนด และรวมไว้ในข้อกำหนดของโมเดลการตอบสนองที่ส่งไปยัง LLM
ไม่จำเป็นต้องใช้คำแนะนำ PHP DocBlocks แต่บางครั้งคุณอาจต้องการชี้แจงความตั้งใจของคุณเพื่อปรับปรุงผลลัพธ์การอนุมานของ LLM
/**
* Represents a skill of a person and context in which it was mentioned.
*/
class Skill {
public string $ name ;
/** @var SkillType $type type of the skill, derived from the description and context */
public SkillType $ type ;
/** Directly quoted, full sentence mentioning person's skill */
public string $ context ;
}
คุณสามารถใช้ลักษณะ ValidationMixin เพื่อเพิ่มความสามารถในการตรวจสอบออบเจ็กต์ข้อมูลแบบกำหนดเองที่ง่ายดาย
use Cognesy Instructor Features Validation Traits ValidationMixin ;
class User {
use ValidationMixin ;
public int $ age ;
public int $ name ;
public function validate () : array {
if ( $ this -> age < 18 ) {
return [ " User has to be adult to sign the contract. " ];
}
return [];
}
}
ผู้สอนใช้ส่วนประกอบการตรวจสอบ Symfony เพื่อตรวจสอบความถูกต้องของข้อมูลที่แยกออกมา คุณสามารถใช้คำอธิบายประกอบ #[Assert/Callback] เพื่อสร้างตรรกะการตรวจสอบที่กำหนดเองได้อย่างสมบูรณ์
use Cognesy Instructor Instructor ;
use Symfony Component Validator Constraints as Assert ;
use Symfony