dgame / php-soap
该包已被弃用且不再维护。未建议替代包。
php soap
v0.8.5
2018-03-22 15:16 UTC
Requires
- php: >=7.0
- dgame/php-iterator: >=0.3.0
- dgame/php-object: >=0.4.0
- monolog/monolog: ^1.22
Requires (Dev)
- phpunit/phpunit: 5.*.*
README
将XML文档转换为对象或从对象构建XML。
湿化
要湿化XML文件,只需编写具有适当属性/方法的相应对象。让我们以这个XML文件为例
<?xml version="1.0" encoding="utf-8" ?> <soap-env> <person name="Max Musterman"> <car marke="BMW" kennung="i8"/> <phone name="iPhone">9</phone> <birth-place>Hamburg</birth-place> <address> <street>Hauptstraße 1</street> <plz>245698</plz> </address> </person> <person name="Dr. Dolittle"> <car marke="Audi" kennung="A3"/> <phone name="Sony">Xperia Z3</phone> <birth-place>München</birth-place> <address> <street>Partkstraße</street> <plz>365494</plz> </address> </person> </soap-env>
要湿化它,需要一个Root
或Envelope
类,一个Person
类,一个Car
类,一个Phone
类和一个Address
类。为了找到这些对象,它们应该以XML标签命名,或者您可以在ClassMapper上注册它们。为了将数据放入对象中,方法应该以数据来源的XML标签命名,以set
或append
开头。例如,要将名称放入人名中,尝试使用以下三种变体来填充对象
- 使用名为
name
的公共属性 - 使用公共方法:
setName
- 使用公共方法:
appendName
这是通过php-object包实现的。
但让我们看看湿化过程的整个示例
$doc = new DOMDocument(); $doc->loadXML(...); $mapper = new ClassMapper( [ 'Root' => TestRoot::class, 'Person' => TestPerson::class, 'Car' => TestCar::class, 'Phone' => TestPhone::class, 'Address' => TestAddress::class ] ); $mapper->appendPattern('/^(?:soap\-?)?env(?:elope)?/iS', 'Root'); $hydrator = new Hydrator($mapper); $objects = $hydrator->hydrateDocument($doc);
就是这样。如您所见,通过行$mapper->appendPattern('/^(?:soap\-?)?env(?:elope)?/iS', 'Root');
我们应用正则表达式以匹配所有soap XML标签的变体,并确定应使用Root
类。现在我们可以测试结果
$this->assertCount(1, $objects); /** @var TestRoot $root */ $root = $objects[0]; $this->assertNotNull($root); $this->assertInstanceOf(TestRoot::class, $root); $persons = $root->getPersons(); $this->assertCount(2, $persons); $this->assertInstanceOf(TestPerson::class, $persons[0]); $this->assertEquals('Max Musterman', $persons[0]->getName()); $this->assertInstanceOf(TestCar::class, $persons[0]->getCar()); $this->assertEquals('BMW', $persons[0]->getCar()->getMarke()); $this->assertNotEmpty($persons[0]->getCar()->kennung); $this->assertEquals('i8', $persons[0]->getCar()->kennung); $this->assertInstanceOf(TestPhone::class, $persons[0]->getPhone()); $this->assertEquals('iPhone', $persons[0]->getPhone()->getName()); $this->assertEquals(9, $persons[0]->getPhone()->getValue()); $this->assertEquals('Hamburg', $persons[0]->getBirthplace()); $this->assertInstanceOf(TestAddress::class, $persons[0]->getAddress()); $this->assertEquals('Hauptstraße 1', $persons[0]->getAddress()->getStreet()); $this->assertEquals(245698, $persons[0]->getAddress()->getPlz()); $this->assertInstanceOf(TestPerson::class, $persons[1]); $this->assertEquals('Dr. Dolittle', $persons[1]->getName()); $this->assertInstanceOf(TestCar::class, $persons[1]->getCar()); $this->assertEquals('Audi', $persons[1]->getCar()->getMarke()); $this->assertNotEmpty($persons[0]->getCar()->kennung); $this->assertEquals('A3', $persons[1]->getCar()->kennung); $this->assertInstanceOf(TestPhone::class, $persons[1]->getPhone()); $this->assertEquals('Sony', $persons[1]->getPhone()->getName()); $this->assertEquals('Xperia Z3', $persons[1]->getPhone()->getValue()); $this->assertEquals('München', $persons[1]->getBirthplace()); $this->assertInstanceOf(TestAddress::class, $persons[1]->getAddress()); $this->assertEquals('Partkstraße', $persons[1]->getAddress()->getStreet()); $this->assertEquals(365494, $persons[1]->getAddress()->getPlz());
很简单,不是吗?
脱水
当然,您可以从湿化/重新组装的元素中脱水。
$doc2 = $hydrator->assemble($root); $this->assertEqualXMLStructure($doc->documentElement, $doc2->documentElement);
这就足够了。
创建
最后,您还可以从现有的对象创建XML。因此,以下示例显示了四种不同的BiPRO请求
RequestTokenService
$envelope = new Envelope(); $security = new Security(); $token = new UsernameToken('Foo', 'Bar'); $security->appendElement($token); $header = new Header(); $header->appendElement($security); $rst = new RequestSecurityToken(); $rst->appendElement(new BiPROVersion('2.1.6.1.1')); $body = new Body(); $body->appendElement($rst); $envelope->appendElement($header); $envelope->appendElement($body); $assembler = new Assembler(); $envelope->accept($assembler); print $assembler->getDocument()->saveXML();
结果是
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soap:mustUnderstand="1"> <wsse:UsernameToken> <wsse:Username>Foo</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Bar</wsse:Password> </wsse:UsernameToken> </wsse:Security> </soap:Header> <soap:Body> <ns2:RequestSecurityToken xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:ns2="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ns4="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns5="http://www.w3.org/2000/09/xmldsig#" xmlns:ns6="http://schemas.xmlsoap.org/ws/2004/09/policy"> <ns2:TokenType>http://schemas.xmlsoap.org/ws/2005/02/sc/sct</ns2:TokenType> <ns2:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</ns2:RequestType> <allgemein:BiPROVersion xmlns:allgemein="http://www.bipro.net/namespace/allgemein">2.1.6.1.1</allgemein:BiPROVersion> </ns2:RequestSecurityToken> </soap:Body> </soap:Envelope>
ListShipments
$envelope = new Envelope(); $security = new Security(); $token = new SecurityContextToken('bipro:7860072500822840554'); $security->appendElement($token); $header = new Header(); $header->appendElement($security); $request = new Request(new BiPROVersion('2.1.4.1.1')); $request->setConfirm(false); $shipment = new ListShipments($request); $body = new Body(); $body->appendElement($shipment); $envelope->appendElement($header); $envelope->appendElement($body); $assembler = new Assembler(); $envelope->accept($assembler); print $assembler->getDocument()->saveXML();
结果是
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soap:mustUnderstand="1"> <wsc:SecurityContextToken xmlns:wsc="http://schemas.xmlsoap.org/ws/2005/02/sc"> <wsc:Identifier>bipro:7860072500822840554</wsc:Identifier> </wsc:SecurityContextToken> </wsse:Security> </soap:Header> <soap:Body> <transfer:ListShipments xmlns:transfer="http://www.bipro.net/namespace/transfer"> <transfer:Request> <allgemein:BiPROVersion xmlns:allgemein="http://www.bipro.net/namespace/allgemein">2.1.4.1.1</allgemein:BiPROVersion> <transfer:BestaetigeLieferungen>false</transfer:BestaetigeLieferungen> </transfer:Request> </transfer:ListShipments> </soap:Body> </soap:Envelope>
GetShipment
$envelope = new Envelope(); $security = new Security(); $token = new SecurityContextToken('bipro:7860072500822840554'); $security->appendElement($token); $header = new Header(); $header->appendElement($security); $request = new Request(new BiPROVersion('2.1.4.1.1')); $request->setConsumerId(1); $request->setId(1); $request->setConfirm(false); $shipment = new GetShipment($request); $body = new Body(); $body->appendElement($shipment); $envelope->appendElement($header); $envelope->appendElement($body); $assembler = new Assembler(); $envelope->accept($assembler); print $assembler->getDocument()->saveXML();
结果是
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soap:mustUnderstand="1"> <wsc:SecurityContextToken xmlns:wsc="http://schemas.xmlsoap.org/ws/2005/02/sc"> <wsc:Identifier>bipro:7860072500822840554</wsc:Identifier> </wsc:SecurityContextToken> </wsse:Security> </soap:Header> <soap:Body> <transfer:GetShipment xmlns:transfer="http://www.bipro.net/namespace/transfer"> <transfer:Request> <allgemein:BiPROVersion xmlns:allgemein="http://www.bipro.net/namespace/allgemein">2.1.4.1.1</allgemein:BiPROVersion> <transfer:ConsumerID>1</transfer:ConsumerID> <transfer:ID>1</transfer:ID> <transfer:BestaetigeLieferungen>false</transfer:BestaetigeLieferungen> </transfer:Request> </transfer:GetShipment> </soap:Body> </soap:Envelope>
AcknowledgeShipment
$envelope = new Envelope(); $security = new Security(); $token = new SecurityContextToken('bipro:7860072500822840554'); $security->appendElement($token); $header = new Header(); $header->appendElement($security); $request = new Request(new BiPROVersion('2.1.4.1.1')); $request->setConsumerId(1); $request->setId(1); $shipment = new AcknowledgeShipment($request); $body = new Body(); $body->appendElement($shipment); $envelope->appendElement($header); $envelope->appendElement($body); $assembler = new Assembler(); $envelope->accept($assembler); print $assembler->getDocument()->saveXML();
结果是
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <wsc:SecurityContextToken xmlns:wsc="http://schemas.xmlsoap.org/ws/2005/02/sc"> <wsc:Identifier>bipro:7860072500822840554</wsc:Identifier> </wsc:SecurityContextToken> </wsse:Security> </soap:Header> <soap:Body> <transfer:AcknowledgeShipment xmlns:transfer="http://www.bipro.net/namespace/transfer"> <transfer:Request> <allgemein:BiPROVersion xmlns:allgemein="http://www.bipro.net/namespace/allgemein">2.1.4.1.1</allgemein:BiPROVersion> <transfer:ConsumerID>1</transfer:ConsumerID> <transfer:ID>1</transfer:ID> </transfer:Request> </transfer:AcknowledgeShipment> </soap:Body> </soap:Envelope>