PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/local/php_interface/classes/vendor/fluentdom/fluentdom/src/FluentDOM/Query/Data.php

https://gitlab.com/Andrey_rus86/kruzenshtern
PHP | 199 lines | 92 code | 17 blank | 90 comment | 12 complexity | 183a0776345393f26ef4ab8e98b9e9bd MD5 | raw file
  1. <?php
  2. /**
  3. * FluentDOM\Data is used for the FluentDOM::data property and FluentDOM::data() method, providing an
  4. * interface html5 data properties of a node.
  5. *
  6. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  7. * @copyright Copyright (c) 2009-2014 Bastian Feder, Thomas Weinert
  8. */
  9. namespace FluentDOM\Query {
  10. use FluentDOM\Query as Query;
  11. /**
  12. * FluentDOM\Data is used for the FluentDOM::data property and FluentDOM::data() method, providing an
  13. * interface html5 data properties of a node.
  14. */
  15. class Data implements \IteratorAggregate, \Countable {
  16. /**
  17. * Attached element node
  18. *
  19. * @var \DOMElement
  20. */
  21. private $_node = NULL;
  22. /**
  23. * Create object with attached element node.
  24. *
  25. * @param \DOMElement $node
  26. */
  27. public function __construct(\DOMElement $node) {
  28. $this->_node = $node;
  29. }
  30. /**
  31. * Convert data attributes into an array
  32. *
  33. * @return array
  34. */
  35. public function toArray() {
  36. $result = array();
  37. foreach ($this->_node->attributes as $attribute) {
  38. if ($this->isDataProperty($attribute->name)) {
  39. $result[$this->decodeName($attribute->name)] = $this->decodeValue($attribute->value);
  40. }
  41. }
  42. return $result;
  43. }
  44. /**
  45. * IteratorAggregate Interface: allow to iterate the data attributes
  46. *
  47. * @return \Iterator
  48. */
  49. public function getIterator() {
  50. return new \ArrayIterator($this->toArray());
  51. }
  52. /**
  53. * countable Interface: return the number of data attributes
  54. *
  55. * @return integer
  56. */
  57. public function count() {
  58. $result = 0;
  59. foreach ($this->_node->attributes as $attribute) {
  60. if ($this->isDataProperty($attribute->name)) {
  61. ++$result;
  62. }
  63. }
  64. return $result;
  65. }
  66. /**
  67. * Validate if the attached node has the data attribute.
  68. *
  69. * @param string $name
  70. * @return bool
  71. */
  72. public function __isset($name) {
  73. return $this->_node->hasAttribute($this->encodeName($name));
  74. }
  75. /**
  76. * Change a data attribute on the attached node.
  77. *
  78. * @param string $name
  79. * @param mixed $value
  80. */
  81. public function __set($name, $value) {
  82. $this->_node->setAttribute($this->encodeName($name), $this->encodeValue($value));
  83. }
  84. /**
  85. * Read a data attribute from the attached node.
  86. *
  87. * @param string $name
  88. * @return mixed
  89. */
  90. public function __get($name) {
  91. $name = $this->encodeName($name);
  92. if ($this->_node->hasAttribute($name)) {
  93. return $this->decodeValue($this->_node->getAttribute($name));
  94. }
  95. return NULL;
  96. }
  97. /**
  98. * Remove a data attribute from the attached node.
  99. *
  100. * @param string $name
  101. */
  102. public function __unset($name) {
  103. $this->_node->removeAttribute($this->encodeName($name));
  104. }
  105. /**
  106. * Validate if the given attribute name is a data property name
  107. *
  108. * @param string $name
  109. * @return bool
  110. */
  111. private function isDataProperty($name) {
  112. return (0 === strpos($name, 'data-') && $name === strtolower($name));
  113. }
  114. /**
  115. * Normalize a property name from camel case to lowercase with hyphens.
  116. *
  117. * @param string $name
  118. * @return string
  119. */
  120. private function encodeName($name) {
  121. if (preg_match('(^[a-z][a-z\d]*([A-Z]+[a-z\d]*)+$)DS', $name)) {
  122. $camelCasePattern = '((?:[a-z][a-z\d]+)|(?:[A-Z][a-z\d]+)|(?:[A-Z]+(?![a-z\d])))S';
  123. if (preg_match_all($camelCasePattern, $name, $matches)) {
  124. $name = implode('-', $matches[0]);
  125. }
  126. }
  127. return 'data-'.strToLower($name);
  128. }
  129. /**
  130. * Convert the given attribute name with hyphens to camel case.
  131. *
  132. * @param string $name
  133. * @return string
  134. */
  135. private function decodeName($name) {
  136. $parts = explode('-', strToLower(substr($name, 5)));
  137. $result = array_shift($parts);
  138. foreach ($parts as $part) {
  139. $result .= ucFirst($part);
  140. }
  141. return $result;
  142. }
  143. /**
  144. * Decode the attribute value into a php variable/array/object
  145. *
  146. * @param string $value
  147. * @return mixed
  148. */
  149. private function decodeValue($value) {
  150. switch (TRUE) {
  151. case ($value === 'true') :
  152. return TRUE;
  153. case ($value === 'false') :
  154. return FALSE;
  155. case (in_array(substr($value, 0, 1), array('{', '['))) :
  156. if ($json = json_decode($value)) {
  157. return $json;
  158. } else {
  159. return NULL;
  160. }
  161. default :
  162. return $value;
  163. }
  164. }
  165. /**
  166. * Encode php variable into a string. Array or Objects will be serialized using json encoding.
  167. * Boolean use the strings yes/no.
  168. *
  169. * @param mixed $value
  170. * @return string
  171. */
  172. private function encodeValue($value) {
  173. if (is_bool($value)) {
  174. return ($value) ? 'true' : 'false';
  175. } elseif (is_object($value) || is_array($value)) {
  176. return json_encode($value);
  177. } else {
  178. return (string)$value;
  179. }
  180. }
  181. }
  182. }