PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/day_23_PDO_&_Session_CRUD/Session_CRUD/vendor/league/csv/src/Config/Controls.php

https://gitlab.com/cmtsheikeshadi/first_app_development_project
PHP | 235 lines | 198 code | 4 blank | 33 comment | 0 complexity | c1f2b3db7b36fdc13fd4c5105e516322 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the League.csv library
  4. *
  5. * @license http://opensource.org/licenses/MIT
  6. * @link https://github.com/thephpleague/csv/
  7. * @version 8.1.0
  8. * @package League.csv
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. namespace League\Csv\Config;
  14. use CallbackFilterIterator;
  15. use InvalidArgumentException;
  16. use LimitIterator;
  17. use SplFileObject;
  18. /**
  19. * A trait to configure and check CSV file and content
  20. *
  21. * @package League.csv
  22. * @since 6.0.0
  23. *
  24. */
  25. trait Controls
  26. {
  27. /**
  28. * the field delimiter (one character only)
  29. *
  30. * @var string
  31. */
  32. protected $delimiter = ',';
  33. /**
  34. * the field enclosure character (one character only)
  35. *
  36. * @var string
  37. */
  38. protected $enclosure = '"';
  39. /**
  40. * the field escape character (one character only)
  41. *
  42. * @var string
  43. */
  44. protected $escape = '\\';
  45. /**
  46. * newline character
  47. *
  48. * @var string
  49. */
  50. protected $newline = "\n";
  51. /**
  52. * Sets the field delimiter
  53. *
  54. * @param string $delimiter
  55. *
  56. * @throws InvalidArgumentException If $delimiter is not a single character
  57. *
  58. * @return $this
  59. */
  60. public function setDelimiter($delimiter)
  61. {
  62. if (!$this->isValidCsvControls($delimiter)) {
  63. throw new InvalidArgumentException('The delimiter must be a single character');
  64. }
  65. $this->delimiter = $delimiter;
  66. return $this;
  67. }
  68. /**
  69. * Tell whether the submitted string is a valid CSV Control character
  70. *
  71. * @param string $str The submitted string
  72. *
  73. * @return bool
  74. */
  75. protected function isValidCsvControls($str)
  76. {
  77. return 1 == mb_strlen($str);
  78. }
  79. /**
  80. * Returns the current field delimiter
  81. *
  82. * @return string
  83. */
  84. public function getDelimiter()
  85. {
  86. return $this->delimiter;
  87. }
  88. /**
  89. * Detect Delimiters occurences in the CSV
  90. *
  91. * Returns a associative array where each key represents
  92. * a valid delimiter and each value the number of occurences
  93. *
  94. * @param string[] $delimiters the delimiters to consider
  95. * @param int $nb_rows Detection is made using $nb_rows of the CSV
  96. *
  97. * @return array
  98. */
  99. public function fetchDelimitersOccurrence(array $delimiters, $nb_rows = 1)
  100. {
  101. $nb_rows = $this->validateInteger($nb_rows, 1, 'The number of rows to consider must be a valid positive integer');
  102. $filter_row = function ($row) {
  103. return is_array($row) && count($row) > 1;
  104. };
  105. $delimiters = array_unique(array_filter($delimiters, [$this, 'isValidCsvControls']));
  106. $csv = $this->getIterator();
  107. $res = [];
  108. foreach ($delimiters as $delim) {
  109. $csv->setCsvControl($delim, $this->enclosure, $this->escape);
  110. $iterator = new CallbackFilterIterator(new LimitIterator($csv, 0, $nb_rows), $filter_row);
  111. $res[$delim] = count(iterator_to_array($iterator, false), COUNT_RECURSIVE);
  112. }
  113. arsort($res, SORT_NUMERIC);
  114. return $res;
  115. }
  116. /**
  117. * Validate an integer
  118. *
  119. * @param int $int
  120. * @param int $minValue
  121. * @param string $errorMessage
  122. *
  123. * @throws InvalidArgumentException If the value is invalid
  124. *
  125. * @return int
  126. */
  127. protected function validateInteger($int, $minValue, $errorMessage)
  128. {
  129. if (false === ($int = filter_var($int, FILTER_VALIDATE_INT, ['options' => ['min_range' => $minValue]]))) {
  130. throw new InvalidArgumentException($errorMessage);
  131. }
  132. return $int;
  133. }
  134. /**
  135. * Returns the CSV Iterator
  136. *
  137. * @return SplFileObject
  138. */
  139. abstract public function getIterator();
  140. /**
  141. * Sets the field enclosure
  142. *
  143. * @param string $enclosure
  144. *
  145. * @throws InvalidArgumentException If $enclosure is not a single character
  146. *
  147. * @return $this
  148. */
  149. public function setEnclosure($enclosure)
  150. {
  151. if (!$this->isValidCsvControls($enclosure)) {
  152. throw new InvalidArgumentException('The enclosure must be a single character');
  153. }
  154. $this->enclosure = $enclosure;
  155. return $this;
  156. }
  157. /**
  158. * Returns the current field enclosure
  159. *
  160. * @return string
  161. */
  162. public function getEnclosure()
  163. {
  164. return $this->enclosure;
  165. }
  166. /**
  167. * Sets the field escape character
  168. *
  169. * @param string $escape
  170. *
  171. * @throws InvalidArgumentException If $escape is not a single character
  172. *
  173. * @return $this
  174. */
  175. public function setEscape($escape)
  176. {
  177. if (!$this->isValidCsvControls($escape)) {
  178. throw new InvalidArgumentException('The escape character must be a single character');
  179. }
  180. $this->escape = $escape;
  181. return $this;
  182. }
  183. /**
  184. * Returns the current field escape character
  185. *
  186. * @return string
  187. */
  188. public function getEscape()
  189. {
  190. return $this->escape;
  191. }
  192. /**
  193. * Sets the newline sequence characters
  194. *
  195. * @param string $newline
  196. *
  197. * @return static
  198. */
  199. public function setNewline($newline)
  200. {
  201. $this->newline = (string) $newline;
  202. return $this;
  203. }
  204. /**
  205. * Returns the current newline sequence characters
  206. *
  207. * @return string
  208. */
  209. public function getNewline()
  210. {
  211. return $this->newline;
  212. }
  213. }