Vtimezone.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * iCalcreator, the PHP class package managing iCal (rfc2445/rfc5445) calendar information.
  4. *
  5. * copyright (c) 2007-2021 Kjell-Inge Gustafsson, kigkonsult, All rights reserved
  6. * Link https://kigkonsult.se
  7. * Package iCalcreator
  8. * Version 2.30
  9. * License Subject matter of licence is the software iCalcreator.
  10. * The above copyright, link, package and version notices,
  11. * this licence notice and the invariant [rfc5545] PRODID result use
  12. * as implemented and invoked in iCalcreator shall be included in
  13. * all copies or substantial portions of the iCalcreator.
  14. *
  15. * iCalcreator is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Lesser General Public License as published
  17. * by the Free Software Foundation, either version 3 of the License,
  18. * or (at your option) any later version.
  19. *
  20. * iCalcreator is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public License
  26. * along with iCalcreator. If not, see <https://www.gnu.org/licenses/>.
  27. *
  28. * This file is a part of iCalcreator.
  29. */
  30. namespace Kigkonsult\Icalcreator;
  31. use Exception;
  32. use function array_keys;
  33. use function sprintf;
  34. use function strtoupper;
  35. /**
  36. * iCalcreator VTIMEZONE component class
  37. *
  38. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  39. * @since 2.29.9 2019-08-05
  40. */
  41. final class Vtimezone extends CalendarComponent
  42. {
  43. use Traits\COMMENTtrait,
  44. Traits\DTSTARTtrait,
  45. Traits\LAST_MODIFIEDtrait,
  46. Traits\RDATEtrait,
  47. Traits\RRULEtrait,
  48. Traits\TZIDtrait,
  49. Traits\TZNAMEtrait,
  50. Traits\TZOFFSETFROMtrait,
  51. Traits\TZOFFSETTOtrait,
  52. Traits\TZURLtrait;
  53. /**
  54. * @var string
  55. */
  56. protected static $compSgn = 'tz';
  57. /**
  58. * Destructor
  59. *
  60. * @since 2.26 - 2018-11-10
  61. */
  62. public function __destruct()
  63. {
  64. if( ! empty( $this->components )) {
  65. foreach( $this->components as $cix => $component ) {
  66. $this->components[$cix]->__destruct();
  67. }
  68. }
  69. unset(
  70. $this->compType,
  71. $this->xprop,
  72. $this->components,
  73. $this->unparsed,
  74. $this->config,
  75. $this->propIx,
  76. $this->compix,
  77. $this->propDelIx
  78. );
  79. unset(
  80. $this->cno,
  81. $this->srtk
  82. );
  83. unset(
  84. $this->comment,
  85. $this->dtstart,
  86. $this->lastmodified,
  87. $this->rdate,
  88. $this->rrule,
  89. $this->tzid,
  90. $this->tzname,
  91. $this->tzoffsetfrom,
  92. $this->tzoffsetto,
  93. $this->tzurl,
  94. $this->timezonetype
  95. );
  96. }
  97. /**
  98. * Return formatted output for calendar component VTIMEZONE object instance
  99. *
  100. * @return string
  101. * @throws Exception (on Rdate err)
  102. * @since 2.29.9 2019-08-05
  103. */
  104. public function createComponent()
  105. {
  106. $compType = strtoupper( $this->getCompType());
  107. $component = sprintf( self::$FMTBEGIN, $compType );
  108. $component .= $this->createTzid();
  109. $component .= $this->createLastmodified();
  110. $component .= $this->createTzurl();
  111. $component .= $this->createDtstart();
  112. $component .= $this->createTzoffsetfrom();
  113. $component .= $this->createTzoffsetto();
  114. $component .= $this->createComment();
  115. $component .= $this->createRdate();
  116. $component .= $this->createRrule();
  117. $component .= $this->createTzname();
  118. $component .= $this->createXprop();
  119. $component .= $this->createSubComponent();
  120. return $component . sprintf( self::$FMTEND, $compType );
  121. }
  122. /**
  123. * Return formatted output for subcomponents
  124. *
  125. * @return string
  126. * @since 2.27.2 - 2018-12-21
  127. * @throws Exception (on Valarm/Standard/Daylight) err)
  128. */
  129. public function createSubComponent()
  130. {
  131. if( self::VTIMEZONE == $this->getCompType()) {
  132. $this->sortVtimezonesSubComponents();
  133. }
  134. return parent::createSubComponent();
  135. }
  136. /**
  137. * Sort Vtimezones subComponents
  138. *
  139. * sort : standard, daylight, in dtstart order
  140. * @since 2.29.1 - 2019-06-28
  141. */
  142. private function sortVtimezonesSubComponents()
  143. {
  144. if( empty( $this->components )) {
  145. return;
  146. }
  147. $stdArr = $dlArr = [];
  148. foreach( array_keys( $this->components ) as $cix ) {
  149. if( empty( $this->components[$cix] )) {
  150. continue;
  151. }
  152. $key = $this->components[$cix]->getDtstart();
  153. if( empty( $key )) {
  154. $key = $cix * 10;
  155. }
  156. else {
  157. $key = $key->getTimestamp();
  158. }
  159. if( self::STANDARD == $this->components[$cix]->getCompType()) {
  160. while( isset( $stdArr[$key] )) {
  161. $key += 1;
  162. }
  163. $stdArr[$key] = $this->components[$cix];
  164. }
  165. elseif( self::DAYLIGHT == $this->components[$cix]->getCompType()) {
  166. while( isset( $dlArr[$key] )) {
  167. $key += 1;
  168. }
  169. $dlArr[$key] = $this->components[$cix];
  170. }
  171. } // end foreach
  172. $this->components = [];
  173. ksort( $stdArr, SORT_NUMERIC );
  174. foreach( $stdArr as $std ) {
  175. $this->components[] = $std;
  176. }
  177. unset( $stdArr );
  178. ksort( $dlArr, SORT_NUMERIC );
  179. foreach( $dlArr as $dl ) {
  180. $this->components[] = $dl;
  181. }
  182. unset( $dlArr );
  183. }
  184. /**
  185. * Return timezone standard object instance
  186. *
  187. * @return Standard
  188. * @since 2.27.2 - 2018-12-21
  189. */
  190. public function newStandard()
  191. {
  192. array_unshift( $this->components, new Standard( $this->getConfig()));
  193. return $this->components[0];
  194. }
  195. /**
  196. * Return timezone daylight object instance
  197. *
  198. * @return Daylight
  199. * @since 2.27.2 - 2018-12-21
  200. */
  201. public function newDaylight()
  202. {
  203. $ix = ( empty( $this->components ))
  204. ? 0
  205. : key( array_slice( $this->components, -1, 1, true )) + 1;
  206. $this->components[$ix] = new Daylight( $this->getConfig());
  207. return $this->components[$ix];
  208. }
  209. }