-
Notifications
You must be signed in to change notification settings - Fork 0
/
facade.php
70 lines (60 loc) · 1.45 KB
/
facade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Created by PhpStorm.
* User: netAir
* Date: 17-7-17
* Time: 下午4:46
*/
class CD
{
public $tracks = [];
public $band = '';
public $title = '';
public function __construct(string $title, string $band, array $tracks)
{
$this->title = $title;
$this->band = $band;
$this->tracks = $tracks;
}
}
//下面两个类负责处理CD类以获取最终所需信息格式
class CDUpperCase
{
public static function makeString(CD $cd, string $type)
{
$cd->$type = strtoupper($cd->$type);
}
public static function makeArray(CD $cd, string $type)
{
$cd->$type = array_map('strtoupper', $cd->$type);
}
}
class CDMakeXML
{
public static function create(CD $cd)
{
$doc = new DOMDocument();
/*
* 根据$cd生成相应XML字符串
* ......
*/
return $doc->saveXML();
}
}
//使用装饰器将生成最终结果(调用以上两类中的各个方法)的过程进行包装
class WebServiceFacade
{
public static function makeXMLCall(CD $cd)
{
CDUpperCase::makeString($cd, 'title');
CDUpperCase::makeString($cd, 'band');
CDUpperCase::makeArray($cd, 'tracks');
$xml = CDMakeXML::create($cd);
return $xml;
}
}
$tracksFromExternalSource = ['a', 'b', 'c'];
$title = 'title';
$band = 'band';
$cd = new CD($title, $band, $tracksFromExternalSource);
print WebServiceFacade::makeXMLCall($cd);