/src/Pyrus/PackageFile/v2/Configureoption.php

https://github.com/CloCkWeRX/Pyrus · PHP · 252 lines · 209 code · 43 blank · 0 comment · 29 complexity · 2bee2e8eacac9d14209d87e13454bdcb MD5 · raw file

  1. <?php
  2. namespace Pyrus\PackageFile\v2;
  3. class Configureoption implements \ArrayAccess, \Iterator, \Countable
  4. {
  5. protected $parent;
  6. protected $info;
  7. protected $index;
  8. function __construct($parent, array $info, $index = null)
  9. {
  10. $this->parent = $parent;
  11. $this->info = $info;
  12. $this->index = $index;
  13. }
  14. function current()
  15. {
  16. $info = current($this->info);
  17. foreach (array('name', 'default', 'prompt') as $key) {
  18. if (!array_key_exists($key, $info)) {
  19. $info[$key] = null;
  20. }
  21. }
  22. return new Configureoption($this, $info, key($this->info));
  23. }
  24. function count()
  25. {
  26. return count($this->info);
  27. }
  28. function rewind()
  29. {
  30. reset($this->info);
  31. }
  32. function key()
  33. {
  34. $info = current($this->info);
  35. return $info['name'];
  36. }
  37. function next()
  38. {
  39. return next($this->info);
  40. }
  41. function valid()
  42. {
  43. return current($this->info);
  44. }
  45. function locateConfigureOption($name)
  46. {
  47. foreach ($this->info as $i => $dep) {
  48. if (isset($dep['name']) && $dep['name'] == $name) {
  49. return $i;
  50. }
  51. }
  52. return false;
  53. }
  54. function offsetGet($var)
  55. {
  56. if ($this->index !== null) {
  57. throw new Configureoption\Exception('use -> operator to access properties of a configureoption');
  58. }
  59. $i = $this->locateConfigureOption($var);
  60. if (false === $i) {
  61. $i = count($this->info);
  62. $info = array('name' => $var, 'default' => null, 'prompt' => null);
  63. } else {
  64. $info = $this->info[$i];
  65. foreach (array('name', 'default', 'prompt') as $key) {
  66. if (!array_key_exists($key, $this->info[$i])) {
  67. $info[$key] = null;
  68. }
  69. }
  70. }
  71. return new Configureoption($this, $info, $i);
  72. }
  73. function offsetSet($var, $value)
  74. {
  75. if ($this->index !== null) {
  76. throw new Configureoption\Exception('use -> operator to access properties of a configureoption');
  77. }
  78. if (!($value instanceof Configureoption)) {
  79. throw new Configureoption\Exception(
  80. 'Can only set configureoption to a \Pyrus\PackageFile\v2\Configureoption object');
  81. }
  82. $i = $this->locateConfigureOption($var);
  83. if (false === $i) {
  84. $i = count($this->info);
  85. }
  86. $this->info[$i] = array('name' => $var,
  87. 'default' => $value->default,
  88. 'prompt' => $value->prompt);
  89. $this->save();
  90. }
  91. function offsetExists($var)
  92. {
  93. if ($this->index !== null) {
  94. throw new Configureoption\Exception(
  95. 'use -> operator to access properties of a configureoption');
  96. }
  97. $i = $this->locateConfigureOption($var);
  98. return $i !== false;
  99. }
  100. function offsetUnset($var)
  101. {
  102. if ($this->index !== null) {
  103. throw new Configureoption\Exception(
  104. 'use -> operator to access properties of a configureoption');
  105. }
  106. $i = $this->locateConfigureOption($var);
  107. if ($i === false) {
  108. return;
  109. }
  110. unset($this->info[$i]);
  111. $this->info = array_values($this->info);
  112. $this->save();
  113. }
  114. function __unset($var)
  115. {
  116. if ($this->index === null) {
  117. throw new Configureoption\Exception(
  118. 'use [] operator to access configureoption' . 's');
  119. }
  120. if (!array_key_exists($var, $this->info)) {
  121. throw new Configureoption\Exception(
  122. 'Unknown variable ' . $var . ' requested, should be one of ' .
  123. implode(', ', array_keys($this->info))
  124. );
  125. }
  126. $this->info[$var] = null;
  127. $this->save();
  128. }
  129. function __isset($var)
  130. {
  131. if ($this->index === null) {
  132. throw new Configureoption\Exception(
  133. 'use [] operator to access configureoption' . 's');
  134. }
  135. if (!array_key_exists($var, $this->info)) {
  136. throw new Configureoption\Exception(
  137. 'Unknown variable ' . $var . ' requested, should be one of ' .
  138. implode(', ', array_keys($this->info))
  139. );
  140. }
  141. return isset($this->info[$var]);
  142. }
  143. function __set($var, $value)
  144. {
  145. return $this->__call($var, array($value));
  146. }
  147. function __call($var, $args)
  148. {
  149. if ($this->index === null) {
  150. throw new Configureoption\Exception(
  151. 'use [] operator to access configureoptions');
  152. }
  153. if (!array_key_exists($var, $this->info)) {
  154. throw new Configureoption\Exception('Unknown variable ' . $var .
  155. ', must be one of ' . implode(', ', array_keys($this->info)));
  156. }
  157. if ($args[0] === null) {
  158. $this->info[$var] = null;
  159. $this->save();
  160. return $this;
  161. }
  162. $this->info[$var] = $args[0];
  163. $this->save();
  164. return $this;
  165. }
  166. function __get($var)
  167. {
  168. if ($this->index === null) {
  169. throw new Configureoption\Exception('use [] operator to access configureoptions');
  170. }
  171. if ($var === 'type') {
  172. return 'configureoption';
  173. }
  174. if (!array_key_exists($var, $this->info)) {
  175. throw new Configureoption\Exception('Unknown variable ' . $var .
  176. ', must be one of ' . implode(', ', array_keys($this->info)));
  177. }
  178. if (!isset($this->info[$var])) {
  179. return null;
  180. }
  181. return $this->info[$var];
  182. }
  183. function getInfo()
  184. {
  185. return $this->info;
  186. }
  187. function setInfo($info, $index)
  188. {
  189. foreach (array_keys($info) as $key) {
  190. if ($info[$key] === null) {
  191. unset($info[$key]);
  192. }
  193. }
  194. $this->info[$index] = $info;
  195. }
  196. function save()
  197. {
  198. if ($this->parent instanceof self) {
  199. $this->parent->setInfo($this->info, $this->index);
  200. $this->parent->save();
  201. return;
  202. }
  203. $info = $this->info;
  204. if (count($info) == 1) {
  205. $info = $info[0];
  206. }
  207. $this->parent->rawconfigureoption = $info;
  208. }
  209. }