PageRenderTime 22ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionArray.php

http://github.com/ryancramerdesign/ProcessWire
PHP | 203 lines | 82 code | 22 blank | 99 comment | 16 complexity | 9b6518609ea5bae7d0c6b264c7a41092 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * ProcessWire Selectable Option Array, for FieldtypeOptions
  4. *
  5. * ProcessWire 2.x
  6. * Copyright (C) 2015 by Ryan Cramer
  7. * This file licensed under Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
  8. *
  9. * https://processwire.com
  10. *
  11. */
  12. class SelectableOptionArray extends WireArray {
  13. /**
  14. * Output formatting on or off
  15. *
  16. * @var bool
  17. *
  18. */
  19. protected $of = false;
  20. /**
  21. * Page these options apply to, if applicable
  22. *
  23. * @var Page|null
  24. *
  25. */
  26. protected $page = null;
  27. /**
  28. * Field these options apply to (always applicable)
  29. *
  30. * @var null
  31. *
  32. */
  33. protected $field = null;
  34. /**
  35. * Set the these options live on
  36. *
  37. * @param Page $page
  38. *
  39. */
  40. public function setPage(Page $page) {
  41. $this->page = $page;
  42. }
  43. /**
  44. * Returns page these options are for, if applicable (NullPage otherwise)
  45. *
  46. * @return NullPage|Page
  47. *
  48. */
  49. public function getPage() {
  50. return $this->page ? $this->page : new NullPage();
  51. }
  52. /**
  53. * Set the field these options are for
  54. *
  55. * @param Field $field
  56. *
  57. */
  58. public function setField(Field $field) {
  59. $this->field = $field;
  60. }
  61. /**
  62. * Returns Field object these options are for
  63. *
  64. * @return null|Field
  65. *
  66. */
  67. public function getField() {
  68. return $this->field;
  69. }
  70. /**
  71. * Get or set output formatting mode
  72. *
  73. * @param bool|null $of Omit to retrieve mode, or specify bool to set it
  74. * @return bool Current mode. If also setting mode, returns previous mode.
  75. *
  76. */
  77. public function of($of = null) {
  78. $_of = $this->of;
  79. if(is_null($of)) return $_of;
  80. $this->of = $of ? true : false;
  81. foreach($this as $option) $option->of($this->of);
  82. return $_of; // whatever previous value was
  83. }
  84. /**
  85. * Provide a default string rendering of these selectable options
  86. *
  87. * For debugging or basic usage
  88. *
  89. * @return string
  90. *
  91. */
  92. public function render() {
  93. $of = $this->of(true);
  94. $out = '<ul><li>' . $this->implode('</li><li>', 'title') . '</li></ul>';
  95. if(!$of) $this->of(false);
  96. return $out;
  97. }
  98. /**
  99. * Return string value of these options (pipe separated IDs)
  100. *
  101. * @return string
  102. *
  103. */
  104. public function __toString() {
  105. return $this->implode('|', 'id');
  106. }
  107. /**
  108. * Enables this WireArray to behave like the first item (for getting properties)
  109. *
  110. * @param string $property
  111. * @return mixed|null
  112. *
  113. */
  114. public function getProperty($property) {
  115. if(SelectableOption::isProperty($property)) {
  116. if($this->count()) {
  117. $option = $this->first();
  118. return $option->$property;
  119. }
  120. return null;
  121. }
  122. return parent::getProperty($property);
  123. }
  124. /**
  125. * Enables this WireArray to behave like the first item (for setting properties)
  126. *
  127. * @param string $property
  128. * @param mixed $value
  129. * @return SelectableOption|SelectableOptionArray
  130. *
  131. */
  132. public function setProperty($property, $value) {
  133. if(SelectableOption::isProperty($property)) {
  134. if($this->count()) {
  135. $option = $this->first();
  136. return $option->set($property, $value);
  137. }
  138. }
  139. // note: there is no parent method for this one
  140. return $this;
  141. }
  142. public function set($key, $value) {
  143. if(SelectableOption::isProperty($key)) return $this->setProperty($key, $value);
  144. return parent::set($key, $value);
  145. }
  146. public function __set($key, $value) {
  147. // we must have both this and set() per behavior of WireArray::__set()
  148. // which throws exceptions if attempting to set a property
  149. if(SelectableOption::isProperty($key)) return $this->setProperty($key, $value);
  150. return parent::__set($key, $value);
  151. }
  152. public function isValidItem($item) {
  153. return $item instanceof SelectableOption;
  154. }
  155. public function isValidKey($key) {
  156. return is_int($key);
  157. }
  158. public function getItemKey($item) {
  159. return $item->id ? $item->id : null;
  160. }
  161. public function makeBlankItem() {
  162. return new SelectableOption();
  163. }
  164. /**
  165. * Is the given WireArray identical to this one?
  166. *
  167. * @param WireArray $items
  168. * @param bool|int $strict
  169. * @return bool
  170. *
  171. */
  172. public function isIdentical(WireArray $items, $strict = true) {
  173. $isIdentical = parent::isIdentical($items, false); // force non-strict
  174. if($isIdentical && $strict) {
  175. if($this->of() != $items->of()) $isIdentical = false;
  176. if($isIdentical && ((string) $this->getPage()) !== ((string) $items->getPage())) $isIdentical = false;
  177. if($isIdentical && ((string) $this->getField()) !== ((string) $items->getField())) $isIdentical = false;
  178. }
  179. return $isIdentical;
  180. }
  181. }