|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OSS\Model; |
| 4 | + |
| 5 | +use OSS\Core\OssException; |
| 6 | + |
| 7 | + |
| 8 | +/** |
| 9 | + * Class ReplicationConfig |
| 10 | + * @package OSS\Model |
| 11 | + * @link https://help.aliyun.com/document_detail/177800.htm |
| 12 | + */ |
| 13 | +class ReplicationConfig implements XmlConfig |
| 14 | +{ |
| 15 | + |
| 16 | + /** |
| 17 | + * @var ReplicationRule[] |
| 18 | + */ |
| 19 | + private $rule; |
| 20 | + |
| 21 | + /** |
| 22 | + * @param $rule ReplicationRule |
| 23 | + */ |
| 24 | + public function addRule($rule){ |
| 25 | + $this->rule[] = $rule; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @return ReplicationRule[] |
| 30 | + */ |
| 31 | + public function getRules(){ |
| 32 | + return $this->rule; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Parse the xml into this object. |
| 37 | + * |
| 38 | + * @param string $strXml |
| 39 | + * @throws OssException |
| 40 | + * @return null |
| 41 | + */ |
| 42 | + public function parseFromXml($strXml) |
| 43 | + { |
| 44 | + $this->rule = array(); |
| 45 | + $xml = simplexml_load_string($strXml); |
| 46 | + if (!isset($xml->Rule)) return; |
| 47 | + $this->parseRule($xml->Rule); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param $rules |
| 52 | + */ |
| 53 | + private function parseRule($rules) |
| 54 | + { |
| 55 | + if(isset($rules)){ |
| 56 | + foreach ($rules as $rule){ |
| 57 | + $replicationRule = new ReplicationRule(); |
| 58 | + if (isset($rule->ID)){ |
| 59 | + $replicationRule->setId(strval($rule->ID)); |
| 60 | + } |
| 61 | + if (isset($rule->PrefixSet)){ |
| 62 | + foreach ($rule->PrefixSet->Prefix as $prefix){ |
| 63 | + $replicationRule->setPrefixSet(strval($prefix)); |
| 64 | + } |
| 65 | + } |
| 66 | + if (isset($rule->Action)){ |
| 67 | + $replicationRule->setAction(strval($rule->Action)); |
| 68 | + } |
| 69 | + if (isset($rule->Destination)){ |
| 70 | + $this->parseDestination($rule->Destination,$replicationRule); |
| 71 | + } |
| 72 | + if (isset($rule->Status)){ |
| 73 | + $replicationRule->setStatus(strval($rule->Status)); |
| 74 | + } |
| 75 | + if (isset($rule->HistoricalObjectReplication)){ |
| 76 | + $replicationRule->setHistoricalObjectReplication(strval($rule->HistoricalObjectReplication)); |
| 77 | + } |
| 78 | + if (isset($rule->SyncRole)){ |
| 79 | + $replicationRule->setSyncRole(strval($rule->SyncRole)); |
| 80 | + } |
| 81 | + if (isset($rule->SourceSelectionCriteria)){ |
| 82 | + $this->parseSourceSelectionCriteria($rule->SourceSelectionCriteria,$replicationRule); |
| 83 | + } |
| 84 | + if (isset($rule->EncryptionConfiguration)){ |
| 85 | + $this->parseEncryptionConfiguration($rule->EncryptionConfiguration,$replicationRule); |
| 86 | + } |
| 87 | + if (isset($rule->RTC->Status)){ |
| 88 | + $replicationRule->setRTC(strval($rule->RTC->Status)); |
| 89 | + } |
| 90 | + $this->addRule($replicationRule); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param $destination array |
| 98 | + * @param $replicationRule ReplicationRule |
| 99 | + */ |
| 100 | + private function parseDestination($destination,&$replicationRule) |
| 101 | + { |
| 102 | + if(isset($destination)){ |
| 103 | + $replicationDestination = new ReplicationDestination(); |
| 104 | + if (isset($destination->Bucket)){ |
| 105 | + $replicationDestination->setBucket(strval($destination->Bucket)); |
| 106 | + } |
| 107 | + if (isset($destination->Location)){ |
| 108 | + $replicationDestination->setLocation(strval($destination->Location)); |
| 109 | + } |
| 110 | + if (isset($destination->TransferType)){ |
| 111 | + $replicationDestination->setTransferType(strval($destination->TransferType)); |
| 112 | + } |
| 113 | + $replicationRule->addDestination($replicationDestination); |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param $sourceSelectionCriteria array |
| 120 | + * @param $replicationRule ReplicationRule |
| 121 | + */ |
| 122 | + private function parseSourceSelectionCriteria($sourceSelectionCriteria,&$replicationRule) |
| 123 | + { |
| 124 | + if(isset($sourceSelectionCriteria)){ |
| 125 | + $replicationSourceSelectionCriteria = new ReplicationSourceSelectionCriteria(); |
| 126 | + if (isset($sourceSelectionCriteria->SseKmsEncryptedObjects->Status)){ |
| 127 | + $replicationSourceSelectionCriteria->setStatus(strval($sourceSelectionCriteria->SseKmsEncryptedObjects->Status)); |
| 128 | + } |
| 129 | + $replicationRule->addSourceSelectionCriteria($replicationSourceSelectionCriteria); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @param $encryptionConfiguration array |
| 136 | + * @param $replicationRule ReplicationRule |
| 137 | + */ |
| 138 | + private function parseEncryptionConfiguration($encryptionConfiguration,&$replicationRule) |
| 139 | + { |
| 140 | + if(isset($encryptionConfiguration)){ |
| 141 | + $replicationEncryptionConfiguration = new ReplicationEncryptionConfiguration(); |
| 142 | + if (isset($encryptionConfiguration->ReplicaKmsKeyID)){ |
| 143 | + $replicationEncryptionConfiguration->setReplicaKmsKeyID(strval($encryptionConfiguration->ReplicaKmsKeyID)); |
| 144 | + } |
| 145 | + $replicationRule->addEncryptionConfiguration($replicationEncryptionConfiguration); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Serialize the object to xml |
| 151 | + * |
| 152 | + * @return string |
| 153 | + */ |
| 154 | + public function serializeToXml() |
| 155 | + { |
| 156 | + |
| 157 | + $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><ReplicationConfiguration></ReplicationConfiguration>'); |
| 158 | + foreach ($this->rule as $rule) { |
| 159 | + $xmlRule = $xml->addChild('Rule'); |
| 160 | + $rule->appendToXml($xmlRule); |
| 161 | + } |
| 162 | + return $xml->asXML(); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Serialize the object into xml string. |
| 167 | + * |
| 168 | + * @return string |
| 169 | + */ |
| 170 | + public function __toString() |
| 171 | + { |
| 172 | + return $this->serializeToXml(); |
| 173 | + } |
| 174 | + |
| 175 | +} |
| 176 | + |
| 177 | + |
0 commit comments