1+ <?php
2+ /**
3+ *
4+ */
5+
6+ namespace Mobtexting \DialPlan ;
7+
8+ abstract class DialPlan{
9+ private $ name ;
10+ private $ attributes ;
11+ private $ next ;
12+
13+ /**
14+ * DialPlan Constructor
15+ *
16+ * @param string $name Dial Plan Element Name
17+ * @param array $attributes Attributes of The Dial Plan Element Attributes
18+ */
19+
20+ public function __construct ($ name , $ attributes =NULL ){
21+ $ this ->name = $ name ;
22+ if ($ attributes ) {
23+ $ this ->attributes [] = $ attributes ;
24+ }
25+ $ this ->next = array ();
26+ }
27+
28+ /**
29+ *
30+ */
31+ public function setAttribute ($ name , $ value ){
32+ $ required_attribute = array_filter ($ this ->attributes , function ($ attribute ){
33+ return $ attribute ->name === 'name ' ;
34+ });
35+
36+ $ required_attribute ->setValue ($ value );
37+ }
38+ /**
39+ * Add a Dial Plan Element
40+ *
41+ * @param DialPlan $dialplan Dial Plan to Append
42+ * @return $this
43+ */
44+
45+ public function append ($ dialplan ){
46+ $ this ->next [] = $ dialplan ;
47+ return $ this ;
48+ }
49+
50+ /**
51+ * Convert Dial Plan to json
52+ *
53+ * @return string DialPlan json representation
54+ */
55+
56+ public function toJson (){
57+ return $ this ->_toString ();
58+ }
59+
60+
61+ /**
62+ * Convert Dial Plan to String
63+ *
64+ * @return string DialPlan json representation
65+ */
66+
67+ public function _toString (){
68+ $ element = $ this ->toArray ();
69+ return json_encode ($ element );
70+ }
71+
72+
73+ /**
74+ * Convert Dial Plan to An PHP Array
75+ *
76+ * @return Array DialPlan Array representation
77+ */
78+
79+ public function toArray (){
80+ $ element = array ();
81+ $ attributes = array ();
82+ $ result = array ();
83+ foreach ($ this ->attributes as $ attribute ){
84+ $ attributes = array_merge ($ attribute ->toArray (), $ attributes );
85+ }
86+ $ element [$ this ->name ] = $ attributes ;
87+ if (count ($ this ->next )>0 ){
88+ $ result [] = $ element ;
89+ foreach ($ this ->next as $ basic_element ){
90+ $ result [] = $ basic_element ->toArray ();
91+ }
92+ return $ result ;
93+ }else {
94+ return $ element ;
95+ }
96+
97+ }
98+ }
0 commit comments