Attribute.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /*
  3. * File: Attribute.php
  4. * Category: -
  5. * Author: M. Goldenbaum
  6. * Created: 01.01.21 20:17
  7. * Updated: -
  8. *
  9. * Description:
  10. * -
  11. */
  12. namespace Webklex\PHPIMAP;
  13. use ArrayAccess;
  14. use Carbon\Carbon;
  15. /**
  16. * Class Attribute
  17. *
  18. * @package Webklex\PHPIMAP
  19. */
  20. class Attribute implements ArrayAccess {
  21. /** @var string $name */
  22. protected $name;
  23. /**
  24. * Value holder
  25. *
  26. * @var array $values
  27. */
  28. protected $values = [];
  29. /**
  30. * Attribute constructor.
  31. * @param string $name
  32. * @param array|mixed $value
  33. */
  34. public function __construct($name, $value = null) {
  35. $this->setName($name);
  36. $this->add($value);
  37. }
  38. /**
  39. * Return the stringified attribute
  40. *
  41. * @return string
  42. */
  43. public function __toString() {
  44. return implode(", ", $this->values);
  45. }
  46. /**
  47. * Return the stringified attribute
  48. *
  49. * @return string
  50. */
  51. public function toString(){
  52. return $this->__toString();
  53. }
  54. /**
  55. * Return the serialized attribute
  56. *
  57. * @return array
  58. */
  59. public function __serialize(){
  60. return $this->values;
  61. }
  62. /**
  63. * Convert instance to array
  64. *
  65. * @return array
  66. */
  67. public function toArray(){
  68. return $this->__serialize();
  69. }
  70. /**
  71. * Convert first value to a date object
  72. *
  73. * @return Carbon|null
  74. */
  75. public function toDate(){
  76. $date = $this->first();
  77. if ($date instanceof Carbon) return $date;
  78. return Carbon::parse($date);
  79. }
  80. /**
  81. * Determine if a value exists at an offset.
  82. *
  83. * @param mixed $key
  84. * @return bool
  85. */
  86. public function offsetExists($key) {
  87. return array_key_exists($key, $this->values);
  88. }
  89. /**
  90. * Get a value at a given offset.
  91. *
  92. * @param mixed $key
  93. * @return mixed
  94. */
  95. public function offsetGet($key) {
  96. return $this->values[$key];
  97. }
  98. /**
  99. * Set the value at a given offset.
  100. *
  101. * @param mixed $key
  102. * @param mixed $value
  103. * @return void
  104. */
  105. public function offsetSet($key, $value) {
  106. if (is_null($key)) {
  107. $this->values[] = $value;
  108. } else {
  109. $this->values[$key] = $value;
  110. }
  111. }
  112. /**
  113. * Unset the value at a given offset.
  114. *
  115. * @param string $key
  116. * @return void
  117. */
  118. public function offsetUnset($key) {
  119. unset($this->values[$key]);
  120. }
  121. /**
  122. * Add one or more values to the attribute
  123. * @param array|mixed $value
  124. * @param boolean $strict
  125. *
  126. * @return Attribute
  127. */
  128. public function add($value, $strict = false) {
  129. if (is_array($value)) {
  130. return $this->merge($value, $strict);
  131. }elseif ($value !== null) {
  132. $this->attach($value, $strict);
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Merge a given array of values with the current values array
  138. * @param array $values
  139. * @param boolean $strict
  140. *
  141. * @return Attribute
  142. */
  143. public function merge($values, $strict = false) {
  144. if (is_array($values)) {
  145. foreach ($values as $value) {
  146. $this->attach($value, $strict);
  147. }
  148. }
  149. return $this;
  150. }
  151. /**
  152. * Check if the attribute contains the given value
  153. * @param mixed $value
  154. *
  155. * @return bool
  156. */
  157. public function contains($value) {
  158. foreach ($this->values as $v) {
  159. if ($v === $value) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. /**
  166. * Attach a given value to the current value array
  167. * @param $value
  168. * @param bool $strict
  169. */
  170. public function attach($value, $strict = false) {
  171. if ($strict === true) {
  172. if ($this->contains($value) === false) {
  173. $this->values[] = $value;
  174. }
  175. }else{
  176. $this->values[] = $value;
  177. }
  178. }
  179. /**
  180. * Set the attribute name
  181. * @param $name
  182. *
  183. * @return Attribute
  184. */
  185. public function setName($name){
  186. $this->name = $name;
  187. return $this;
  188. }
  189. /**
  190. * Get the attribute name
  191. *
  192. * @return string
  193. */
  194. public function getName(){
  195. return $this->name;
  196. }
  197. /**
  198. * Get all values
  199. *
  200. * @return array
  201. */
  202. public function get(){
  203. return $this->values;
  204. }
  205. /**
  206. * Alias method for self::get()
  207. *
  208. * @return array
  209. */
  210. public function all(){
  211. return $this->get();
  212. }
  213. /**
  214. * Get the first value if possible
  215. *
  216. * @return mixed|null
  217. */
  218. public function first(){
  219. if ($this->offsetExists(0)) {
  220. return $this->values[0];
  221. }
  222. return null;
  223. }
  224. /**
  225. * Get the last value if possible
  226. *
  227. * @return mixed|null
  228. */
  229. public function last(){
  230. if (($cnt = $this->count()) > 0) {
  231. return $this->values[$cnt - 1];
  232. }
  233. return null;
  234. }
  235. /**
  236. * Get the number of values
  237. *
  238. * @return int
  239. */
  240. public function count(){
  241. return count($this->values);
  242. }
  243. }