/library/Zend/Pdf/Outline/AbstractOutline.php

https://github.com/MarcelloDuarte/zf2 · PHP · 381 lines · 130 code · 44 blank · 207 comment · 9 complexity · 3f1e2c44661ca3d0038e929b2ece9e0b MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_PDF
  17. * @subpackage Zend_PDF_Outline
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Pdf\Outline;
  25. use Zend\Pdf\Exception;
  26. use Zend\Pdf;
  27. use Zend\Pdf\InternalType;
  28. use Zend\Pdf\ObjectFactory;
  29. /**
  30. * Abstract PDF outline representation class
  31. *
  32. * @todo Implement an ability to associate an outline item with a structure element (PDF 1.3 feature)
  33. *
  34. * @uses Countable
  35. * @uses RecursiveIterator
  36. * @uses \Zend\Pdf\Exception
  37. * @uses \Zend\Pdf\Outline\Created
  38. * @uses \Zend\Pdf\ObjectFactory;
  39. * @package Zend_PDF
  40. * @subpackage Zend_PDF_Outline
  41. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. abstract class AbstractOutline implements \RecursiveIterator, \Countable
  45. {
  46. /**
  47. * True if outline is open.
  48. *
  49. * @var boolean
  50. */
  51. protected $_open = false;
  52. /**
  53. * Array of child outlines (array of \Zend\Pdf\Outline\AbstractOutline objects)
  54. *
  55. * @var array
  56. */
  57. public $childOutlines = array();
  58. /**
  59. * Get outline title.
  60. *
  61. * @return string
  62. */
  63. abstract public function getTitle();
  64. /**
  65. * Set outline title
  66. *
  67. * @param string $title
  68. * @return \Zend\Pdf\Outline\AbstractOutline
  69. */
  70. abstract public function setTitle($title);
  71. /**
  72. * Returns true if outline item is open by default
  73. *
  74. * @return boolean
  75. */
  76. public function isOpen()
  77. {
  78. return $this->_open;
  79. }
  80. /**
  81. * Sets 'isOpen' outline flag
  82. *
  83. * @param boolean $isOpen
  84. * @return \Zend\Pdf\Outline\AbstractOutline
  85. */
  86. public function setIsOpen($isOpen)
  87. {
  88. $this->_open = $isOpen;
  89. return $this;
  90. }
  91. /**
  92. * Returns true if outline item is displayed in italic
  93. *
  94. * @return boolean
  95. */
  96. abstract public function isItalic();
  97. /**
  98. * Sets 'isItalic' outline flag
  99. *
  100. * @param boolean $isItalic
  101. * @return \Zend\Pdf\Outline\AbstractOutline
  102. */
  103. abstract public function setIsItalic($isItalic);
  104. /**
  105. * Returns true if outline item is displayed in bold
  106. *
  107. * @return boolean
  108. */
  109. abstract public function isBold();
  110. /**
  111. * Sets 'isBold' outline flag
  112. *
  113. * @param boolean $isBold
  114. * @return \Zend\Pdf\Outline\AbstractOutline
  115. */
  116. abstract public function setIsBold($isBold);
  117. /**
  118. * Get outline text color.
  119. *
  120. * @return \Zend\Pdf\Color\Rgb
  121. */
  122. abstract public function getColor();
  123. /**
  124. * Set outline text color.
  125. * (null means default color which is black)
  126. *
  127. * @param \Zend\Pdf\Color\Rgb $color
  128. * @return \Zend\Pdf\Outline\AbstractOutline
  129. */
  130. abstract public function setColor(Pdf\Color\Rgb $color);
  131. /**
  132. * Get outline target.
  133. *
  134. * @return \Zend\Pdf\InternalStructure\NavigationTarget
  135. */
  136. abstract public function getTarget();
  137. /**
  138. * Set outline target.
  139. * Null means no target
  140. *
  141. * @param \Zend\Pdf\InternalStructure\NavigationTarget|string $target
  142. * @return \Zend\Pdf\Outline\AbstractOutline
  143. */
  144. abstract public function setTarget($target = null);
  145. /**
  146. * Get outline options
  147. *
  148. * @return array
  149. */
  150. public function getOptions()
  151. {
  152. return array('title' => $this->_title,
  153. 'open' => $this->_open,
  154. 'color' => $this->_color,
  155. 'italic' => $this->_italic,
  156. 'bold' => $this->_bold,
  157. 'target' => $this->_target);
  158. }
  159. /**
  160. * Set outline options
  161. *
  162. * @param array $options
  163. * @return \Zend\Pdf\Action\AbstractAction
  164. * @throws \Zend\Pdf\Exception
  165. */
  166. public function setOptions(array $options)
  167. {
  168. foreach ($options as $key => $value) {
  169. switch ($key) {
  170. case 'title':
  171. $this->setTitle($value);
  172. break;
  173. case 'open':
  174. $this->setIsOpen($value);
  175. break;
  176. case 'color':
  177. $this->setColor($value);
  178. break;
  179. case 'italic':
  180. $this->setIsItalic($value);
  181. break;
  182. case 'bold':
  183. $this->setIsBold($value);
  184. break;
  185. case 'target':
  186. $this->setTarget($value);
  187. break;
  188. default:
  189. throw new Exception\InvalidArgumentException("Unknown option name - '$key'.");
  190. break;
  191. }
  192. }
  193. return $this;
  194. }
  195. /**
  196. * Create new Outline object
  197. *
  198. * It provides two forms of input parameters:
  199. *
  200. * 1. \Zend\Pdf\Outline\AbstractOutline::create(string $title[, \Zend\Pdf\InternalStructure\NavigationTarget $target])
  201. * 2. \Zend\Pdf\Outline\AbstractOutline::create(array $options)
  202. *
  203. * Second form allows to provide outline options as an array.
  204. * The followed options are supported:
  205. * 'title' - string, outline title, required
  206. * 'open' - boolean, true if outline entry is open (default value is false)
  207. * 'color' - \Zend\Pdf\Color\Rgb object, true if outline entry is open (default value is null - black)
  208. * 'italic' - boolean, true if outline entry is displayed in italic (default value is false)
  209. * 'bold' - boolean, true if outline entry is displayed in bold (default value is false)
  210. * 'target' - \Zend\Pdf\InternalStructure\NavigationTarget object or string, outline item destination
  211. *
  212. * @return \Zend\Pdf\Outline\AbstractOutline
  213. * @throws \Zend\Pdf\Exception
  214. */
  215. public static function create($param1, $param2 = null)
  216. {
  217. if (is_string($param1)) {
  218. if ($param2 !== null && !($param2 instanceof Pdf\InternalStructure\NavigationTarget || is_string($param2))) {
  219. throw new Exception\InvalidArgumentException('Outline create method takes $title (string) and $target (\Zend\Pdf\InternalStructure\NavigationTarget or string) or an array as an input');
  220. }
  221. return new Created(array('title' => $param1,
  222. 'target' => $param2));
  223. } else {
  224. if (!is_array($param1) || $param2 !== null) {
  225. throw new Exception\InvalidArgumentException('Outline create method takes $title (string) and $destination (\Zend\Pdf\InternalStructure\NavigationTarget) or an array as an input');
  226. }
  227. return new Created($param1);
  228. }
  229. }
  230. /**
  231. * Returns number of the total number of open items at all levels of the outline.
  232. *
  233. * @internal
  234. * @return integer
  235. */
  236. public function openOutlinesCount()
  237. {
  238. $count = 1; // Include this outline
  239. if ($this->isOpen()) {
  240. foreach ($this->childOutlines as $child) {
  241. $count += $child->openOutlinesCount();
  242. }
  243. }
  244. return $count;
  245. }
  246. /**
  247. * Dump Outline and its child outlines into PDF structures
  248. *
  249. * Returns dictionary indirect object or reference
  250. *
  251. * @param \Zend\Pdf\ObjectFactory $factory object factory for newly created indirect objects
  252. * @param boolean $updateNavigation Update navigation flag
  253. * @param \Zend\Pdf\InternalType\AbstractTypeObject $parent Parent outline dictionary reference
  254. * @param \Zend\Pdf\InternalType\AbstractTypeObject $prev Previous outline dictionary reference
  255. * @param SplObjectStorage $processedOutlines List of already processed outlines
  256. * @return \Zend\Pdf\InternalType\AbstractTypeObject
  257. */
  258. abstract public function dumpOutline(ObjectFactory $factory,
  259. $updateNavigation,
  260. InternalType\AbstractTypeObject $parent,
  261. InternalType\AbstractTypeObject $prev = null,
  262. \SplObjectStorage $processedOutlines = null);
  263. ////////////////////////////////////////////////////////////////////////
  264. // RecursiveIterator interface methods
  265. //////////////
  266. /**
  267. * Returns the child outline.
  268. *
  269. * @return \Zend\Pdf\Outline\AbstractOutline
  270. */
  271. public function current()
  272. {
  273. return current($this->childOutlines);
  274. }
  275. /**
  276. * Returns current iterator key
  277. *
  278. * @return integer
  279. */
  280. public function key()
  281. {
  282. return key($this->childOutlines);
  283. }
  284. /**
  285. * Go to next child
  286. */
  287. public function next()
  288. {
  289. return next($this->childOutlines);
  290. }
  291. /**
  292. * Rewind children
  293. */
  294. public function rewind()
  295. {
  296. return reset($this->childOutlines);
  297. }
  298. /**
  299. * Check if current position is valid
  300. *
  301. * @return boolean
  302. */
  303. public function valid()
  304. {
  305. return current($this->childOutlines) !== false;
  306. }
  307. /**
  308. * Returns the child outline.
  309. *
  310. * @return \Zend\Pdf\Outline\AbstractOutline|null
  311. */
  312. public function getChildren()
  313. {
  314. return current($this->childOutlines);
  315. }
  316. /**
  317. * Implements RecursiveIterator interface.
  318. *
  319. * @return bool whether container has any pages
  320. */
  321. public function hasChildren()
  322. {
  323. return count($this->childOutlines) > 0;
  324. }
  325. ////////////////////////////////////////////////////////////////////////
  326. // Countable interface methods
  327. //////////////
  328. /**
  329. * count()
  330. *
  331. * @return int
  332. */
  333. public function count()
  334. {
  335. return count($this->childOutlines);
  336. }
  337. }