PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.6.2/Classes/PHPExcel/Style/Protection.php

#
PHP | 225 lines | 80 code | 25 blank | 120 comment | 10 complexity | 06b8bd987542e71c644e1dac30ae8de9 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2008 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Style
  23. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.4.5, 2007-08-23
  26. */
  27. /** PHPExcel_IComparable */
  28. require_once 'PHPExcel/IComparable.php';
  29. /**
  30. * PHPExcel_Style_Protection
  31. *
  32. * @category PHPExcel
  33. * @package PHPExcel_Style
  34. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  35. */
  36. class PHPExcel_Style_Protection implements PHPExcel_IComparable
  37. {
  38. /** Protection styles */
  39. const PROTECTION_INHERIT = 'inherit';
  40. const PROTECTION_PROTECTED = 'protected';
  41. const PROTECTION_UNPROTECTED = 'unprotected';
  42. /**
  43. * Locked
  44. *
  45. * @var string
  46. */
  47. private $_locked;
  48. /**
  49. * Hidden
  50. *
  51. * @var string
  52. */
  53. private $_hidden;
  54. /**
  55. * Parent Style
  56. *
  57. * @var PHPExcel_Style
  58. */
  59. private $_parent;
  60. /**
  61. * Parent Borders
  62. *
  63. * @var _parentPropertyName string
  64. */
  65. private $_parentPropertyName;
  66. /**
  67. * Create a new PHPExcel_Style_Protection
  68. */
  69. public function __construct()
  70. {
  71. // Initialise values
  72. $this->_locked = self::PROTECTION_INHERIT;
  73. $this->_hidden = self::PROTECTION_INHERIT;
  74. }
  75. /**
  76. * Property Prepare bind
  77. *
  78. * Configures this object for late binding as a property of a parent object
  79. *
  80. * @param $parent
  81. * @param $parentPropertyName
  82. */
  83. public function propertyPrepareBind($parent, $parentPropertyName)
  84. {
  85. // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  86. // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  87. $this->_parent = $parent;
  88. $this->_parentPropertyName = $parentPropertyName;
  89. }
  90. /**
  91. * Property Get Bound
  92. *
  93. * Returns the PHPExcel_Style_Protection that is actual bound to PHPExcel_Style
  94. *
  95. * @return PHPExcel_Style_Protection
  96. */
  97. private function propertyGetBound() {
  98. if(!isset($this->_parent))
  99. return $this; // I am bound
  100. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  101. return $this->_parent->getProtection(); // Another one is bound
  102. return $this; // No one is bound yet
  103. }
  104. /**
  105. * Property Begin Bind
  106. *
  107. * If no PHPExcel_Style_Protection has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  108. *
  109. * @return PHPExcel_Style_Protection
  110. */
  111. private function propertyBeginBind() {
  112. if(!isset($this->_parent))
  113. return $this; // I am already bound
  114. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  115. return $this->_parent->getProtection(); // Another one is already bound
  116. $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName); // Bind myself
  117. $this->_parent = null;
  118. return $this;
  119. }
  120. /**
  121. * Apply styles from array
  122. *
  123. * <code>
  124. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray( array('locked' => true, 'hidden' => false) );
  125. * </code>
  126. *
  127. * @param array $pStyles Array containing style information
  128. * @throws Exception
  129. */
  130. public function applyFromArray($pStyles = null) {
  131. if (is_array($pStyles)) {
  132. if (array_key_exists('locked', $pStyles)) {
  133. $this->setLocked($pStyles['locked']);
  134. }
  135. if (array_key_exists('hidden', $pStyles)) {
  136. $this->setHidden($pStyles['locked']);
  137. }
  138. } else {
  139. throw new Exception("Invalid style array passed.");
  140. }
  141. }
  142. /**
  143. * Get locked
  144. *
  145. * @return string
  146. */
  147. public function getLocked() {
  148. return $this->propertyGetBound()->_locked;
  149. }
  150. /**
  151. * Set locked
  152. *
  153. * @param string $pValue
  154. */
  155. public function setLocked($pValue = self::PROTECTION_INHERIT) {
  156. $this->propertyBeginBind()->_locked = $pValue;
  157. }
  158. /**
  159. * Get hidden
  160. *
  161. * @return string
  162. */
  163. public function getHidden() {
  164. return $this->propertyGetBound()->_hidden;
  165. }
  166. /**
  167. * Set hidden
  168. *
  169. * @param string $pValue
  170. */
  171. public function setHidden($pValue = self::PROTECTION_INHERIT) {
  172. $this->propertyBeginBind()->_hidden = $pValue;
  173. }
  174. /**
  175. * Get hash code
  176. *
  177. * @return string Hash code
  178. */
  179. public function getHashCode() {
  180. $property = $this->propertyGetBound();
  181. return md5(
  182. $property->_locked
  183. . $property->_hidden
  184. . __CLASS__
  185. );
  186. }
  187. /**
  188. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  189. */
  190. public function __clone() {
  191. $vars = get_object_vars($this);
  192. foreach ($vars as $key => $value) {
  193. if (is_object($value)) {
  194. $this->$key = clone $value;
  195. } else {
  196. $this->$key = $value;
  197. }
  198. }
  199. }
  200. }