@@ -384,7 +384,7 @@ discard block |
||
384 | 384 | print '<td>'; |
385 | 385 | $s = picto_from_langcode(getDolGlobalString('MAIN_LANG_DEFAULT')); |
386 | 386 | print $s ? $s.' ' : ''; |
387 | - print(getDolGlobalString('MAIN_LANG_DEFAULT') == 'auto' ? $form->textwithpicto($langs->trans("Automatic"), $langs->trans("AutoDetectLang")) : $langs->trans("Language_" . getDolGlobalString('MAIN_LANG_DEFAULT'))); |
|
387 | + print(getDolGlobalString('MAIN_LANG_DEFAULT') == 'auto' ? $form->textwithpicto($langs->trans("Automatic"), $langs->trans("AutoDetectLang")) : $langs->trans("Language_".getDolGlobalString('MAIN_LANG_DEFAULT'))); |
|
388 | 388 | print '</td>'; |
389 | 389 | print '<td class="nowrap" width="20%"><input class="oddeven" name="check_MAIN_LANG_DEFAULT" id="check_MAIN_LANG_DEFAULT" type="checkbox" '.(!empty($object->conf->MAIN_LANG_DEFAULT) ? " checked" : ""); |
390 | 390 | print empty($dolibarr_main_demo) ? '' : ' disabled="disabled"'; // Disabled for demo |
@@ -437,7 +437,7 @@ discard block |
||
437 | 437 | |
438 | 438 | // Max size of short lists |
439 | 439 | print '<tr class="oddeven"><td>'.$langs->trans("MaxSizeShortList").'</td>'; |
440 | - print '<td>' . getDolGlobalString('MAIN_SIZE_SHORTLIST_LIMIT').'</td>'; |
|
440 | + print '<td>'.getDolGlobalString('MAIN_SIZE_SHORTLIST_LIMIT').'</td>'; |
|
441 | 441 | print '<td class="nowrap" width="20%"><input class="oddeven" name="check_MAIN_SIZE_SHORTLIST_LIMIT" id="check_MAIN_SIZE_SHORTLIST_LIMIT" type="checkbox" '.(!empty($object->conf->MAIN_SIZE_SHORTLIST_LIMIT) ? " checked" : ""); |
442 | 442 | print empty($dolibarr_main_demo) ? '' : ' disabled="disabled"'; // Disabled for demo |
443 | 443 | print '> <label for="check_MAIN_SIZE_SHORTLIST_LIMIT">'.$langs->trans("UsePersonalValue").'</label></td>'; |
@@ -506,7 +506,7 @@ discard block |
||
506 | 506 | print '<td>'; |
507 | 507 | $s = picto_from_langcode(getDolGlobalString('MAIN_LANG_DEFAULT')); |
508 | 508 | print($s ? $s.' ' : ''); |
509 | - print(getDolGlobalString('MAIN_LANG_DEFAULT') == 'auto' ? $form->textwithpicto($langs->trans("Automatic"), $langs->trans("AutoDetectLang")) : $langs->trans("Language_" . getDolGlobalString('MAIN_LANG_DEFAULT'))); |
|
509 | + print(getDolGlobalString('MAIN_LANG_DEFAULT') == 'auto' ? $form->textwithpicto($langs->trans("Automatic"), $langs->trans("AutoDetectLang")) : $langs->trans("Language_".getDolGlobalString('MAIN_LANG_DEFAULT'))); |
|
510 | 510 | print '</td>'; |
511 | 511 | print '<td class="nowrap"><input class="oddeven" type="checkbox" disabled '.(!empty($object->conf->MAIN_LANG_DEFAULT) ? " checked" : "").'> '.$langs->trans("UsePersonalValue").'</td>'; |
512 | 512 | print '<td>'; |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | |
144 | 144 | const KEY_ID = 0; |
145 | 145 | const KEY_LABEL = 1; |
146 | - const KEY_TYPE = 2; // deprecated |
|
146 | + const KEY_TYPE = 2; // deprecated |
|
147 | 147 | const KEY_DEFAULT = 3; |
148 | 148 | const KEY_FIRST_LEVEL = 4; |
149 | 149 | const KEY_SECOND_LEVEL = 5; |
@@ -723,7 +723,7 @@ discard block |
||
723 | 723 | $moduleNameInConf = 'supplier_proposal'; |
724 | 724 | } |
725 | 725 | |
726 | - unset($conf->modules[$moduleNameInConf]); // Add this module in list of enabled modules so isModEnabled() will work (conf->module->enabled must no more be used) |
|
726 | + unset($conf->modules[$moduleNameInConf]); // Add this module in list of enabled modules so isModEnabled() will work (conf->module->enabled must no more be used) |
|
727 | 727 | |
728 | 728 | return 1; |
729 | 729 | } else { |
@@ -819,7 +819,7 @@ discard block |
||
819 | 819 | $pathoffile = $this->getDescLongReadmeFound(); |
820 | 820 | |
821 | 821 | if ($pathoffile) { // Mostly for external modules |
822 | - $content = file_get_contents($pathoffile, false, null, 0, 1024 * 1024); // Max size loaded 1Mb |
|
822 | + $content = file_get_contents($pathoffile, false, null, 0, 1024 * 1024); // Max size loaded 1Mb |
|
823 | 823 | |
824 | 824 | if ((float) DOL_VERSION >= 6.0) { // @phpstan-ignore-line |
825 | 825 | @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php'; |
@@ -1583,7 +1583,7 @@ discard block |
||
1583 | 1583 | |
1584 | 1584 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def"; |
1585 | 1585 | $sql .= " WHERE file = '".$this->db->escape($file)."'"; |
1586 | - $sql .= " AND entity = ".$conf->entity; // Do not use getEntity here, we want to delete only in current company |
|
1586 | + $sql .= " AND entity = ".$conf->entity; // Do not use getEntity here, we want to delete only in current company |
|
1587 | 1587 | |
1588 | 1588 | dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG); |
1589 | 1589 | $resql = $this->db->query($sql); |
@@ -1606,9 +1606,9 @@ discard block |
||
1606 | 1606 | public function insert_cronjobs() |
1607 | 1607 | { |
1608 | 1608 | // phpcs:enable |
1609 | - include_once DOL_DOCUMENT_ROOT . '/core/class/infobox.class.php'; |
|
1610 | - include_once DOL_DOCUMENT_ROOT . '/cron/class/cronjob.class.php'; |
|
1611 | - include_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php'; |
|
1609 | + include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php'; |
|
1610 | + include_once DOL_DOCUMENT_ROOT.'/cron/class/cronjob.class.php'; |
|
1611 | + include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; |
|
1612 | 1612 | |
1613 | 1613 | global $conf, $user; |
1614 | 1614 | |
@@ -1619,7 +1619,7 @@ discard block |
||
1619 | 1619 | $err = 0; |
1620 | 1620 | |
1621 | 1621 | if (is_array($this->cronjobs)) { |
1622 | - dol_syslog(get_class($this) . "::insert_cronjobs", LOG_DEBUG); |
|
1622 | + dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG); |
|
1623 | 1623 | |
1624 | 1624 | foreach ($this->cronjobs as $key => $value) { |
1625 | 1625 | $now = dol_now(); |
@@ -1646,7 +1646,7 @@ discard block |
||
1646 | 1646 | $test = isset($value['test']) ? $value['test'] : ''; // Line must be enabled or not (so visible or not) |
1647 | 1647 | |
1648 | 1648 | // Search if cron entry already present |
1649 | - $sql = "SELECT count(*) as nb FROM " . MAIN_DB_PREFIX . "cronjob"; |
|
1649 | + $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."cronjob"; |
|
1650 | 1650 | //$sql .= " WHERE module_name = '" . $this->db->escape(empty($this->rights_class) ? strtolower($this->name) : $this->rights_class) . "'"; |
1651 | 1651 | $sql .= " WHERE label = '".$this->db->escape($label)."'"; |
1652 | 1652 | /* unique key is on label,entity so no need for this test |
@@ -1666,7 +1666,7 @@ discard block |
||
1666 | 1666 | $sql .= " AND params = '" . $this->db->escape($params) . "'"; |
1667 | 1667 | } |
1668 | 1668 | */ |
1669 | - $sql .= " AND entity = " . ((int) $entity); // Must be exact entity |
|
1669 | + $sql .= " AND entity = ".((int) $entity); // Must be exact entity |
|
1670 | 1670 | |
1671 | 1671 | $result = $this->db->query($sql); |
1672 | 1672 | if (!$result) { |
@@ -1992,9 +1992,9 @@ discard block |
||
1992 | 1992 | |
1993 | 1993 | // If the module is active |
1994 | 1994 | foreach ($this->rights as $key => $value) { |
1995 | - $r_id = $this->rights[$key][self::KEY_ID]; // permission id in llx_rights_def (not unique because primary key is couple id-entity) |
|
1995 | + $r_id = $this->rights[$key][self::KEY_ID]; // permission id in llx_rights_def (not unique because primary key is couple id-entity) |
|
1996 | 1996 | $r_label = $this->rights[$key][self::KEY_LABEL]; |
1997 | - $r_type = $this->rights[$key][self::KEY_TYPE] ?? 'w'; // TODO deprecated |
|
1997 | + $r_type = $this->rights[$key][self::KEY_TYPE] ?? 'w'; // TODO deprecated |
|
1998 | 1998 | $r_default = $this->rights[$key][self::KEY_DEFAULT] ?? 0; |
1999 | 1999 | $r_perms = $this->rights[$key][self::KEY_FIRST_LEVEL] ?? ''; |
2000 | 2000 | $r_subperms = $this->rights[$key][self::KEY_SECOND_LEVEL] ?? ''; |
@@ -2018,7 +2018,7 @@ discard block |
||
2018 | 2018 | } |
2019 | 2019 | |
2020 | 2020 | // condition to show or hide a user right (default: 1) (eg isModEnabled('anothermodule') or ($conf->global->MAIN_FEATURES_LEVEL > 0) or etc..) |
2021 | - $r_enabled = $this->rights[$key][self::KEY_ENABLED] ?? '1'; |
|
2021 | + $r_enabled = $this->rights[$key][self::KEY_ENABLED] ?? '1'; |
|
2022 | 2022 | |
2023 | 2023 | // Search if perm already present |
2024 | 2024 | $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."rights_def"; |
@@ -2035,7 +2035,7 @@ discard block |
||
2035 | 2035 | $sql .= ", libelle"; |
2036 | 2036 | $sql .= ", module"; |
2037 | 2037 | $sql .= ", module_origin"; |
2038 | - $sql .= ", type"; // TODO deprecated |
|
2038 | + $sql .= ", type"; // TODO deprecated |
|
2039 | 2039 | $sql .= ", bydefault"; |
2040 | 2040 | $sql .= ", perms"; |
2041 | 2041 | $sql .= ", subperms"; |
@@ -2046,7 +2046,7 @@ discard block |
||
2046 | 2046 | $sql .= ", '".$this->db->escape($r_label)."'"; |
2047 | 2047 | $sql .= ", '".$this->db->escape($r_module)."'"; |
2048 | 2048 | $sql .= ", '".$this->db->escape($r_module_origin)."'"; |
2049 | - $sql .= ", '".$this->db->escape($r_type)."'"; // TODO deprecated |
|
2049 | + $sql .= ", '".$this->db->escape($r_type)."'"; // TODO deprecated |
|
2050 | 2050 | $sql .= ", ".((int) $r_default); |
2051 | 2051 | $sql .= ", '".$this->db->escape($r_perms)."'"; |
2052 | 2052 | $sql .= ", '".$this->db->escape($r_subperms)."'"; |
@@ -2268,7 +2268,7 @@ discard block |
||
2268 | 2268 | |
2269 | 2269 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu"; |
2270 | 2270 | $sql .= " WHERE module = '".$this->db->escape($module)."'"; |
2271 | - $sql .= " AND menu_handler = 'all'"; // We delete only lines that were added manually or by the module activation. We keep entry added by menuhandler like 'auguria' |
|
2271 | + $sql .= " AND menu_handler = 'all'"; // We delete only lines that were added manually or by the module activation. We keep entry added by menuhandler like 'auguria' |
|
2272 | 2272 | $sql .= " AND entity IN (0, ".$conf->entity.")"; |
2273 | 2273 | |
2274 | 2274 | dol_syslog(get_class($this)."::delete_menus", LOG_DEBUG); |
@@ -2652,36 +2652,36 @@ discard block |
||
2652 | 2652 | } |
2653 | 2653 | |
2654 | 2654 | if ($this->isCoreOrExternalModule() == 'external' || preg_match('/development|experimental|deprecated/i', $version)) { |
2655 | - $versionTitle = $langs->trans("Version").' '.$this->getVersion(1); |
|
2655 | + $versionTitle = $langs->trans("Version").' '.$this->getVersion(1); |
|
2656 | 2656 | if ($this->needUpdate) { |
2657 | 2657 | $versionTitle .= '<br>'.$langs->trans('ModuleUpdateAvailable').' : '.$this->lastVersion; |
2658 | 2658 | } |
2659 | 2659 | |
2660 | - $return .= '<span class="info-box-icon-version'.($versiontrans ? ' '.$versiontrans : '').' classfortooltip" title="'.dol_escape_js($versionTitle).'" >'; |
|
2661 | - $return .= $this->getVersion(1); |
|
2662 | - $return .= '</span>'; |
|
2660 | + $return .= '<span class="info-box-icon-version'.($versiontrans ? ' '.$versiontrans : '').' classfortooltip" title="'.dol_escape_js($versionTitle).'" >'; |
|
2661 | + $return .= $this->getVersion(1); |
|
2662 | + $return .= '</span>'; |
|
2663 | 2663 | } |
2664 | 2664 | |
2665 | - $return .= '</div> |
|
2665 | + $return .= '</div> |
|
2666 | 2666 | <div class="info-box-content info-box-text-module'.(!getDolGlobalString($const_name) ? '' : ' info-box-module-enabled'.($versiontrans ? ' info-box-content-warning' : '')).'"> |
2667 | 2667 | <span class="info-box-title">'.$this->getName().'</span> |
2668 | 2668 | <span class="info-box-desc twolinesmax opacitymedium" title="'.dol_escape_htmltag($this->getDesc()).'">'.nl2br($this->getDesc()).'</span>'; |
2669 | 2669 | |
2670 | - $return .= '<div class="valignmiddle inline-block info-box-more">'; |
|
2670 | + $return .= '<div class="valignmiddle inline-block info-box-more">'; |
|
2671 | 2671 | //if ($versiontrans) print img_warning($langs->trans("Version").' '.$this->getVersion(1)).' '; |
2672 | - $return .= '<a class="valignmiddle inline-block" href="javascript:document_preview(\''.DOL_URL_ROOT.'/admin/modulehelp.php?id='.((int) $this->numero).'\',\'text/html\',\''.dol_escape_js($langs->trans("Module")).'\')">'.img_picto(($this->isCoreOrExternalModule() == 'external' ? $langs->trans("ExternalModule").' - ' : '').$langs->trans("ClickToShowDescription"), $imginfo).'</a>'; |
|
2673 | - $return .= '</div><br>'; |
|
2674 | - |
|
2675 | - $return .= '<div class="valignmiddle inline-block info-box-actions">'; |
|
2676 | - $return .= '<div class="valignmiddle inline-block info-box-setup">'; |
|
2677 | - $return .= $codetoconfig; |
|
2678 | - $return .= '</div>'; |
|
2679 | - $return .= '<div class="valignmiddle inline-block marginleftonly marginrightonly">'; |
|
2680 | - $return .= $codeenabledisable; |
|
2681 | - $return .= '</div>'; |
|
2682 | - $return .= '</div>'; |
|
2683 | - |
|
2684 | - $return .= ' |
|
2672 | + $return .= '<a class="valignmiddle inline-block" href="javascript:document_preview(\''.DOL_URL_ROOT.'/admin/modulehelp.php?id='.((int) $this->numero).'\',\'text/html\',\''.dol_escape_js($langs->trans("Module")).'\')">'.img_picto(($this->isCoreOrExternalModule() == 'external' ? $langs->trans("ExternalModule").' - ' : '').$langs->trans("ClickToShowDescription"), $imginfo).'</a>'; |
|
2673 | + $return .= '</div><br>'; |
|
2674 | + |
|
2675 | + $return .= '<div class="valignmiddle inline-block info-box-actions">'; |
|
2676 | + $return .= '<div class="valignmiddle inline-block info-box-setup">'; |
|
2677 | + $return .= $codetoconfig; |
|
2678 | + $return .= '</div>'; |
|
2679 | + $return .= '<div class="valignmiddle inline-block marginleftonly marginrightonly">'; |
|
2680 | + $return .= $codeenabledisable; |
|
2681 | + $return .= '</div>'; |
|
2682 | + $return .= '</div>'; |
|
2683 | + |
|
2684 | + $return .= ' |
|
2685 | 2685 | </div><!-- /.info-box-content --> |
2686 | 2686 | </div><!-- /.info-box --> |
2687 | 2687 | </div>'; |
@@ -2701,7 +2701,7 @@ discard block |
||
2701 | 2701 | { |
2702 | 2702 | require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php'; |
2703 | 2703 | if (!empty($this->url_last_version)) { |
2704 | - $lastVersion = getURLContent($this->url_last_version, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only |
|
2704 | + $lastVersion = getURLContent($this->url_last_version, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only |
|
2705 | 2705 | if (isset($lastVersion['content']) && strlen($lastVersion['content']) < 30) { |
2706 | 2706 | // Security warning : be careful with remote data content, the module editor could be hacked (or evil) so limit to a-z A-Z 0-9 _ . - |
2707 | 2707 | $this->lastVersion = preg_replace("/[^a-zA-Z0-9_\.\-]+/", "", $lastVersion['content']); |
@@ -2738,7 +2738,7 @@ discard block |
||
2738 | 2738 | if (empty($conf->cache['noncompliantmodules'])) { |
2739 | 2739 | require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php'; |
2740 | 2740 | |
2741 | - $result = getURLContent(self::URL_FOR_BLACKLISTED_MODULES, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only |
|
2741 | + $result = getURLContent(self::URL_FOR_BLACKLISTED_MODULES, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only |
|
2742 | 2742 | if (isset($result['content']) && $result['http_code'] == 200) { |
2743 | 2743 | $langs->load("errors"); |
2744 | 2744 |
@@ -48,1069 +48,1069 @@ |
||
48 | 48 | */ |
49 | 49 | trait EnumeratesValues |
50 | 50 | { |
51 | - /** |
|
52 | - * Indicates that the object's string representation should be escaped when __toString is invoked. |
|
53 | - * |
|
54 | - * @var bool |
|
55 | - */ |
|
56 | - protected $escapeWhenCastingToString = false; |
|
57 | - |
|
58 | - /** |
|
59 | - * The methods that can be proxied. |
|
60 | - * |
|
61 | - * @var string[] |
|
62 | - */ |
|
63 | - protected static $proxies = [ |
|
64 | - 'average', |
|
65 | - 'avg', |
|
66 | - 'contains', |
|
67 | - 'doesntContain', |
|
68 | - 'each', |
|
69 | - 'every', |
|
70 | - 'filter', |
|
71 | - 'first', |
|
72 | - 'flatMap', |
|
73 | - 'groupBy', |
|
74 | - 'keyBy', |
|
75 | - 'map', |
|
76 | - 'max', |
|
77 | - 'min', |
|
78 | - 'partition', |
|
79 | - 'reject', |
|
80 | - 'skipUntil', |
|
81 | - 'skipWhile', |
|
82 | - 'some', |
|
83 | - 'sortBy', |
|
84 | - 'sortByDesc', |
|
85 | - 'sum', |
|
86 | - 'takeUntil', |
|
87 | - 'takeWhile', |
|
88 | - 'unique', |
|
89 | - 'until', |
|
90 | - ]; |
|
91 | - |
|
92 | - /** |
|
93 | - * Create a new collection instance if the value isn't one already. |
|
94 | - * |
|
95 | - * @param mixed $items |
|
96 | - * @return static |
|
97 | - */ |
|
98 | - public static function make($items = []) |
|
99 | - { |
|
100 | - return new static($items); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Wrap the given value in a collection if applicable. |
|
105 | - * |
|
106 | - * @param mixed $value |
|
107 | - * @return static |
|
108 | - */ |
|
109 | - public static function wrap($value) |
|
110 | - { |
|
111 | - return $value instanceof Enumerable |
|
112 | - ? new static($value) |
|
113 | - : new static(Arr::wrap($value)); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Get the underlying items from the given collection if applicable. |
|
118 | - * |
|
119 | - * @param array|static $value |
|
120 | - * @return array |
|
121 | - */ |
|
122 | - public static function unwrap($value) |
|
123 | - { |
|
124 | - return $value instanceof Enumerable ? $value->all() : $value; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Create a new instance with no items. |
|
129 | - * |
|
130 | - * @return static |
|
131 | - */ |
|
132 | - public static function empty() |
|
133 | - { |
|
134 | - return new static([]); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Create a new collection by invoking the callback a given amount of times. |
|
139 | - * |
|
140 | - * @param int $number |
|
141 | - * @param callable|null $callback |
|
142 | - * @return static |
|
143 | - */ |
|
144 | - public static function times($number, callable $callback = null) |
|
145 | - { |
|
146 | - if ($number < 1) { |
|
147 | - return new static; |
|
148 | - } |
|
149 | - |
|
150 | - return static::range(1, $number) |
|
151 | - ->when($callback) |
|
152 | - ->map($callback); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Alias for the "avg" method. |
|
157 | - * |
|
158 | - * @param callable|string|null $callback |
|
159 | - * @return mixed |
|
160 | - */ |
|
161 | - public function average($callback = null) |
|
162 | - { |
|
163 | - return $this->avg($callback); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Alias for the "contains" method. |
|
168 | - * |
|
169 | - * @param mixed $key |
|
170 | - * @param mixed $operator |
|
171 | - * @param mixed $value |
|
172 | - * @return bool |
|
173 | - */ |
|
174 | - public function some($key, $operator = null, $value = null) |
|
175 | - { |
|
176 | - return $this->contains(...func_get_args()); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Determine if an item exists, using strict comparison. |
|
181 | - * |
|
182 | - * @param mixed $key |
|
183 | - * @param mixed $value |
|
184 | - * @return bool |
|
185 | - */ |
|
186 | - public function containsStrict($key, $value = null) |
|
187 | - { |
|
188 | - if (func_num_args() === 2) { |
|
189 | - return $this->contains(function ($item) use ($key, $value) { |
|
190 | - return data_get($item, $key) === $value; |
|
191 | - }); |
|
192 | - } |
|
193 | - |
|
194 | - if ($this->useAsCallable($key)) { |
|
195 | - return ! is_null($this->first($key)); |
|
196 | - } |
|
197 | - |
|
198 | - foreach ($this as $item) { |
|
199 | - if ($item === $key) { |
|
200 | - return true; |
|
201 | - } |
|
202 | - } |
|
203 | - |
|
204 | - return false; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Dump the items and end the script. |
|
209 | - * |
|
210 | - * @param mixed ...$args |
|
211 | - * @return void |
|
212 | - */ |
|
213 | - public function dd(...$args) |
|
214 | - { |
|
215 | - $this->dump(...$args); |
|
216 | - |
|
217 | - exit(1); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * Dump the items. |
|
222 | - * |
|
223 | - * @return $this |
|
224 | - */ |
|
225 | - public function dump() |
|
226 | - { |
|
227 | - (new Collection(func_get_args())) |
|
228 | - ->push($this->all()) |
|
229 | - ->each(function ($item) { |
|
230 | - VarDumper::dump($item); |
|
231 | - }); |
|
232 | - |
|
233 | - return $this; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Execute a callback over each item. |
|
238 | - * |
|
239 | - * @param callable $callback |
|
240 | - * @return $this |
|
241 | - */ |
|
242 | - public function each(callable $callback) |
|
243 | - { |
|
244 | - foreach ($this as $key => $item) { |
|
245 | - if ($callback($item, $key) === false) { |
|
246 | - break; |
|
247 | - } |
|
248 | - } |
|
249 | - |
|
250 | - return $this; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Execute a callback over each nested chunk of items. |
|
255 | - * |
|
256 | - * @param callable $callback |
|
257 | - * @return static |
|
258 | - */ |
|
259 | - public function eachSpread(callable $callback) |
|
260 | - { |
|
261 | - return $this->each(function ($chunk, $key) use ($callback) { |
|
262 | - $chunk[] = $key; |
|
263 | - |
|
264 | - return $callback(...$chunk); |
|
265 | - }); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Determine if all items pass the given truth test. |
|
270 | - * |
|
271 | - * @param string|callable $key |
|
272 | - * @param mixed $operator |
|
273 | - * @param mixed $value |
|
274 | - * @return bool |
|
275 | - */ |
|
276 | - public function every($key, $operator = null, $value = null) |
|
277 | - { |
|
278 | - if (func_num_args() === 1) { |
|
279 | - $callback = $this->valueRetriever($key); |
|
280 | - |
|
281 | - foreach ($this as $k => $v) { |
|
282 | - if (! $callback($v, $k)) { |
|
283 | - return false; |
|
284 | - } |
|
285 | - } |
|
286 | - |
|
287 | - return true; |
|
288 | - } |
|
289 | - |
|
290 | - return $this->every($this->operatorForWhere(...func_get_args())); |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * Get the first item by the given key value pair. |
|
295 | - * |
|
296 | - * @param string $key |
|
297 | - * @param mixed $operator |
|
298 | - * @param mixed $value |
|
299 | - * @return mixed |
|
300 | - */ |
|
301 | - public function firstWhere($key, $operator = null, $value = null) |
|
302 | - { |
|
303 | - return $this->first($this->operatorForWhere(...func_get_args())); |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Determine if the collection is not empty. |
|
308 | - * |
|
309 | - * @return bool |
|
310 | - */ |
|
311 | - public function isNotEmpty() |
|
312 | - { |
|
313 | - return ! $this->isEmpty(); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Run a map over each nested chunk of items. |
|
318 | - * |
|
319 | - * @param callable $callback |
|
320 | - * @return static |
|
321 | - */ |
|
322 | - public function mapSpread(callable $callback) |
|
323 | - { |
|
324 | - return $this->map(function ($chunk, $key) use ($callback) { |
|
325 | - $chunk[] = $key; |
|
326 | - |
|
327 | - return $callback(...$chunk); |
|
328 | - }); |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * Run a grouping map over the items. |
|
333 | - * |
|
334 | - * The callback should return an associative array with a single key/value pair. |
|
335 | - * |
|
336 | - * @param callable $callback |
|
337 | - * @return static |
|
338 | - */ |
|
339 | - public function mapToGroups(callable $callback) |
|
340 | - { |
|
341 | - $groups = $this->mapToDictionary($callback); |
|
342 | - |
|
343 | - return $groups->map([$this, 'make']); |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Map a collection and flatten the result by a single level. |
|
348 | - * |
|
349 | - * @param callable $callback |
|
350 | - * @return static |
|
351 | - */ |
|
352 | - public function flatMap(callable $callback) |
|
353 | - { |
|
354 | - return $this->map($callback)->collapse(); |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Map the values into a new class. |
|
359 | - * |
|
360 | - * @param string $class |
|
361 | - * @return static |
|
362 | - */ |
|
363 | - public function mapInto($class) |
|
364 | - { |
|
365 | - return $this->map(function ($value, $key) use ($class) { |
|
366 | - return new $class($value, $key); |
|
367 | - }); |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * Get the min value of a given key. |
|
372 | - * |
|
373 | - * @param callable|string|null $callback |
|
374 | - * @return mixed |
|
375 | - */ |
|
376 | - public function min($callback = null) |
|
377 | - { |
|
378 | - $callback = $this->valueRetriever($callback); |
|
379 | - |
|
380 | - return $this->map(function ($value) use ($callback) { |
|
381 | - return $callback($value); |
|
382 | - })->filter(function ($value) { |
|
383 | - return ! is_null($value); |
|
384 | - })->reduce(function ($result, $value) { |
|
385 | - return is_null($result) || $value < $result ? $value : $result; |
|
386 | - }); |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Get the max value of a given key. |
|
391 | - * |
|
392 | - * @param callable|string|null $callback |
|
393 | - * @return mixed |
|
394 | - */ |
|
395 | - public function max($callback = null) |
|
396 | - { |
|
397 | - $callback = $this->valueRetriever($callback); |
|
398 | - |
|
399 | - return $this->filter(function ($value) { |
|
400 | - return ! is_null($value); |
|
401 | - })->reduce(function ($result, $item) use ($callback) { |
|
402 | - $value = $callback($item); |
|
403 | - |
|
404 | - return is_null($result) || $value > $result ? $value : $result; |
|
405 | - }); |
|
406 | - } |
|
407 | - |
|
408 | - /** |
|
409 | - * "Paginate" the collection by slicing it into a smaller collection. |
|
410 | - * |
|
411 | - * @param int $page |
|
412 | - * @param int $perPage |
|
413 | - * @return static |
|
414 | - */ |
|
415 | - public function forPage($page, $perPage) |
|
416 | - { |
|
417 | - $offset = max(0, ($page - 1) * $perPage); |
|
418 | - |
|
419 | - return $this->slice($offset, $perPage); |
|
420 | - } |
|
421 | - |
|
422 | - /** |
|
423 | - * Partition the collection into two arrays using the given callback or key. |
|
424 | - * |
|
425 | - * @param callable|string $key |
|
426 | - * @param mixed $operator |
|
427 | - * @param mixed $value |
|
428 | - * @return static |
|
429 | - */ |
|
430 | - public function partition($key, $operator = null, $value = null) |
|
431 | - { |
|
432 | - $passed = []; |
|
433 | - $failed = []; |
|
434 | - |
|
435 | - $callback = func_num_args() === 1 |
|
436 | - ? $this->valueRetriever($key) |
|
437 | - : $this->operatorForWhere(...func_get_args()); |
|
438 | - |
|
439 | - foreach ($this as $key => $item) { |
|
440 | - if ($callback($item, $key)) { |
|
441 | - $passed[$key] = $item; |
|
442 | - } else { |
|
443 | - $failed[$key] = $item; |
|
444 | - } |
|
445 | - } |
|
446 | - |
|
447 | - return new static([new static($passed), new static($failed)]); |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Get the sum of the given values. |
|
452 | - * |
|
453 | - * @param callable|string|null $callback |
|
454 | - * @return mixed |
|
455 | - */ |
|
456 | - public function sum($callback = null) |
|
457 | - { |
|
458 | - $callback = is_null($callback) |
|
459 | - ? $this->identity() |
|
460 | - : $this->valueRetriever($callback); |
|
461 | - |
|
462 | - return $this->reduce(function ($result, $item) use ($callback) { |
|
463 | - return $result + $callback($item); |
|
464 | - }, 0); |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * Apply the callback if the value is truthy. |
|
469 | - * |
|
470 | - * @param bool|mixed $value |
|
471 | - * @param callable|null $callback |
|
472 | - * @param callable|null $default |
|
473 | - * @return static|mixed |
|
474 | - */ |
|
475 | - public function when($value, callable $callback = null, callable $default = null) |
|
476 | - { |
|
477 | - if (! $callback) { |
|
478 | - return new HigherOrderWhenProxy($this, $value); |
|
479 | - } |
|
480 | - |
|
481 | - if ($value) { |
|
482 | - return $callback($this, $value); |
|
483 | - } elseif ($default) { |
|
484 | - return $default($this, $value); |
|
485 | - } |
|
486 | - |
|
487 | - return $this; |
|
488 | - } |
|
489 | - |
|
490 | - /** |
|
491 | - * Apply the callback if the collection is empty. |
|
492 | - * |
|
493 | - * @param callable $callback |
|
494 | - * @param callable|null $default |
|
495 | - * @return static|mixed |
|
496 | - */ |
|
497 | - public function whenEmpty(callable $callback, callable $default = null) |
|
498 | - { |
|
499 | - return $this->when($this->isEmpty(), $callback, $default); |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * Apply the callback if the collection is not empty. |
|
504 | - * |
|
505 | - * @param callable $callback |
|
506 | - * @param callable|null $default |
|
507 | - * @return static|mixed |
|
508 | - */ |
|
509 | - public function whenNotEmpty(callable $callback, callable $default = null) |
|
510 | - { |
|
511 | - return $this->when($this->isNotEmpty(), $callback, $default); |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * Apply the callback if the value is falsy. |
|
516 | - * |
|
517 | - * @param bool $value |
|
518 | - * @param callable $callback |
|
519 | - * @param callable|null $default |
|
520 | - * @return static|mixed |
|
521 | - */ |
|
522 | - public function unless($value, callable $callback, callable $default = null) |
|
523 | - { |
|
524 | - return $this->when(! $value, $callback, $default); |
|
525 | - } |
|
526 | - |
|
527 | - /** |
|
528 | - * Apply the callback unless the collection is empty. |
|
529 | - * |
|
530 | - * @param callable $callback |
|
531 | - * @param callable|null $default |
|
532 | - * @return static|mixed |
|
533 | - */ |
|
534 | - public function unlessEmpty(callable $callback, callable $default = null) |
|
535 | - { |
|
536 | - return $this->whenNotEmpty($callback, $default); |
|
537 | - } |
|
538 | - |
|
539 | - /** |
|
540 | - * Apply the callback unless the collection is not empty. |
|
541 | - * |
|
542 | - * @param callable $callback |
|
543 | - * @param callable|null $default |
|
544 | - * @return static|mixed |
|
545 | - */ |
|
546 | - public function unlessNotEmpty(callable $callback, callable $default = null) |
|
547 | - { |
|
548 | - return $this->whenEmpty($callback, $default); |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * Filter items by the given key value pair. |
|
553 | - * |
|
554 | - * @param string $key |
|
555 | - * @param mixed $operator |
|
556 | - * @param mixed $value |
|
557 | - * @return static |
|
558 | - */ |
|
559 | - public function where($key, $operator = null, $value = null) |
|
560 | - { |
|
561 | - return $this->filter($this->operatorForWhere(...func_get_args())); |
|
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * Filter items where the value for the given key is null. |
|
566 | - * |
|
567 | - * @param string|null $key |
|
568 | - * @return static |
|
569 | - */ |
|
570 | - public function whereNull($key = null) |
|
571 | - { |
|
572 | - return $this->whereStrict($key, null); |
|
573 | - } |
|
574 | - |
|
575 | - /** |
|
576 | - * Filter items where the value for the given key is not null. |
|
577 | - * |
|
578 | - * @param string|null $key |
|
579 | - * @return static |
|
580 | - */ |
|
581 | - public function whereNotNull($key = null) |
|
582 | - { |
|
583 | - return $this->where($key, '!==', null); |
|
584 | - } |
|
585 | - |
|
586 | - /** |
|
587 | - * Filter items by the given key value pair using strict comparison. |
|
588 | - * |
|
589 | - * @param string $key |
|
590 | - * @param mixed $value |
|
591 | - * @return static |
|
592 | - */ |
|
593 | - public function whereStrict($key, $value) |
|
594 | - { |
|
595 | - return $this->where($key, '===', $value); |
|
596 | - } |
|
597 | - |
|
598 | - /** |
|
599 | - * Filter items by the given key value pair. |
|
600 | - * |
|
601 | - * @param string $key |
|
602 | - * @param mixed $values |
|
603 | - * @param bool $strict |
|
604 | - * @return static |
|
605 | - */ |
|
606 | - public function whereIn($key, $values, $strict = false) |
|
607 | - { |
|
608 | - $values = $this->getArrayableItems($values); |
|
609 | - |
|
610 | - return $this->filter(function ($item) use ($key, $values, $strict) { |
|
611 | - return in_array(data_get($item, $key), $values, $strict); |
|
612 | - }); |
|
613 | - } |
|
614 | - |
|
615 | - /** |
|
616 | - * Filter items by the given key value pair using strict comparison. |
|
617 | - * |
|
618 | - * @param string $key |
|
619 | - * @param mixed $values |
|
620 | - * @return static |
|
621 | - */ |
|
622 | - public function whereInStrict($key, $values) |
|
623 | - { |
|
624 | - return $this->whereIn($key, $values, true); |
|
625 | - } |
|
626 | - |
|
627 | - /** |
|
628 | - * Filter items such that the value of the given key is between the given values. |
|
629 | - * |
|
630 | - * @param string $key |
|
631 | - * @param array $values |
|
632 | - * @return static |
|
633 | - */ |
|
634 | - public function whereBetween($key, $values) |
|
635 | - { |
|
636 | - return $this->where($key, '>=', reset($values))->where($key, '<=', end($values)); |
|
637 | - } |
|
638 | - |
|
639 | - /** |
|
640 | - * Filter items such that the value of the given key is not between the given values. |
|
641 | - * |
|
642 | - * @param string $key |
|
643 | - * @param array $values |
|
644 | - * @return static |
|
645 | - */ |
|
646 | - public function whereNotBetween($key, $values) |
|
647 | - { |
|
648 | - return $this->filter(function ($item) use ($key, $values) { |
|
649 | - return data_get($item, $key) < reset($values) || data_get($item, $key) > end($values); |
|
650 | - }); |
|
651 | - } |
|
652 | - |
|
653 | - /** |
|
654 | - * Filter items by the given key value pair. |
|
655 | - * |
|
656 | - * @param string $key |
|
657 | - * @param mixed $values |
|
658 | - * @param bool $strict |
|
659 | - * @return static |
|
660 | - */ |
|
661 | - public function whereNotIn($key, $values, $strict = false) |
|
662 | - { |
|
663 | - $values = $this->getArrayableItems($values); |
|
664 | - |
|
665 | - return $this->reject(function ($item) use ($key, $values, $strict) { |
|
666 | - return in_array(data_get($item, $key), $values, $strict); |
|
667 | - }); |
|
668 | - } |
|
669 | - |
|
670 | - /** |
|
671 | - * Filter items by the given key value pair using strict comparison. |
|
672 | - * |
|
673 | - * @param string $key |
|
674 | - * @param mixed $values |
|
675 | - * @return static |
|
676 | - */ |
|
677 | - public function whereNotInStrict($key, $values) |
|
678 | - { |
|
679 | - return $this->whereNotIn($key, $values, true); |
|
680 | - } |
|
681 | - |
|
682 | - /** |
|
683 | - * Filter the items, removing any items that don't match the given type(s). |
|
684 | - * |
|
685 | - * @param string|string[] $type |
|
686 | - * @return static |
|
687 | - */ |
|
688 | - public function whereInstanceOf($type) |
|
689 | - { |
|
690 | - return $this->filter(function ($value) use ($type) { |
|
691 | - if (is_array($type)) { |
|
692 | - foreach ($type as $classType) { |
|
693 | - if ($value instanceof $classType) { |
|
694 | - return true; |
|
695 | - } |
|
696 | - } |
|
697 | - |
|
698 | - return false; |
|
699 | - } |
|
700 | - |
|
701 | - return $value instanceof $type; |
|
702 | - }); |
|
703 | - } |
|
704 | - |
|
705 | - /** |
|
706 | - * Pass the collection to the given callback and return the result. |
|
707 | - * |
|
708 | - * @param callable $callback |
|
709 | - * @return mixed |
|
710 | - */ |
|
711 | - public function pipe(callable $callback) |
|
712 | - { |
|
713 | - return $callback($this); |
|
714 | - } |
|
715 | - |
|
716 | - /** |
|
717 | - * Pass the collection into a new class. |
|
718 | - * |
|
719 | - * @param string $class |
|
720 | - * @return mixed |
|
721 | - */ |
|
722 | - public function pipeInto($class) |
|
723 | - { |
|
724 | - return new $class($this); |
|
725 | - } |
|
726 | - |
|
727 | - /** |
|
728 | - * Pass the collection through a series of callable pipes and return the result. |
|
729 | - * |
|
730 | - * @param array<callable> $pipes |
|
731 | - * @return mixed |
|
732 | - */ |
|
733 | - public function pipeThrough($pipes) |
|
734 | - { |
|
735 | - return static::make($pipes)->reduce( |
|
736 | - function ($carry, $pipe) { |
|
737 | - return $pipe($carry); |
|
738 | - }, |
|
739 | - $this |
|
740 | - ); |
|
741 | - } |
|
742 | - |
|
743 | - /** |
|
744 | - * Pass the collection to the given callback and then return it. |
|
745 | - * |
|
746 | - * @param callable $callback |
|
747 | - * @return $this |
|
748 | - */ |
|
749 | - public function tap(callable $callback) |
|
750 | - { |
|
751 | - $callback(clone $this); |
|
752 | - |
|
753 | - return $this; |
|
754 | - } |
|
755 | - |
|
756 | - /** |
|
757 | - * Reduce the collection to a single value. |
|
758 | - * |
|
759 | - * @param callable $callback |
|
760 | - * @param mixed $initial |
|
761 | - * @return mixed |
|
762 | - */ |
|
763 | - public function reduce(callable $callback, $initial = null) |
|
764 | - { |
|
765 | - $result = $initial; |
|
766 | - |
|
767 | - foreach ($this as $key => $value) { |
|
768 | - $result = $callback($result, $value, $key); |
|
769 | - } |
|
770 | - |
|
771 | - return $result; |
|
772 | - } |
|
773 | - |
|
774 | - /** |
|
775 | - * Reduce the collection to multiple aggregate values. |
|
776 | - * |
|
777 | - * @param callable $callback |
|
778 | - * @param mixed ...$initial |
|
779 | - * @return array |
|
780 | - * |
|
781 | - * @deprecated Use "reduceSpread" instead |
|
782 | - * |
|
783 | - * @throws \UnexpectedValueException |
|
784 | - */ |
|
785 | - public function reduceMany(callable $callback, ...$initial) |
|
786 | - { |
|
787 | - return $this->reduceSpread($callback, ...$initial); |
|
788 | - } |
|
789 | - |
|
790 | - /** |
|
791 | - * Reduce the collection to multiple aggregate values. |
|
792 | - * |
|
793 | - * @param callable $callback |
|
794 | - * @param mixed ...$initial |
|
795 | - * @return array |
|
796 | - * |
|
797 | - * @throws \UnexpectedValueException |
|
798 | - */ |
|
799 | - public function reduceSpread(callable $callback, ...$initial) |
|
800 | - { |
|
801 | - $result = $initial; |
|
802 | - |
|
803 | - foreach ($this as $key => $value) { |
|
804 | - $result = call_user_func_array($callback, array_merge($result, [$value, $key])); |
|
805 | - |
|
806 | - if (! is_array($result)) { |
|
807 | - throw new UnexpectedValueException(sprintf( |
|
808 | - "%s::reduceMany expects reducer to return an array, but got a '%s' instead.", |
|
809 | - class_basename(static::class), gettype($result) |
|
810 | - )); |
|
811 | - } |
|
812 | - } |
|
813 | - |
|
814 | - return $result; |
|
815 | - } |
|
816 | - |
|
817 | - /** |
|
818 | - * Reduce an associative collection to a single value. |
|
819 | - * |
|
820 | - * @param callable $callback |
|
821 | - * @param mixed $initial |
|
822 | - * @return mixed |
|
823 | - */ |
|
824 | - public function reduceWithKeys(callable $callback, $initial = null) |
|
825 | - { |
|
826 | - return $this->reduce($callback, $initial); |
|
827 | - } |
|
828 | - |
|
829 | - /** |
|
830 | - * Create a collection of all elements that do not pass a given truth test. |
|
831 | - * |
|
832 | - * @param callable|mixed $callback |
|
833 | - * @return static |
|
834 | - */ |
|
835 | - public function reject($callback = true) |
|
836 | - { |
|
837 | - $useAsCallable = $this->useAsCallable($callback); |
|
838 | - |
|
839 | - return $this->filter(function ($value, $key) use ($callback, $useAsCallable) { |
|
840 | - return $useAsCallable |
|
841 | - ? ! $callback($value, $key) |
|
842 | - : $value != $callback; |
|
843 | - }); |
|
844 | - } |
|
845 | - |
|
846 | - /** |
|
847 | - * Return only unique items from the collection array using strict comparison. |
|
848 | - * |
|
849 | - * @param string|callable|null $key |
|
850 | - * @return static |
|
851 | - */ |
|
852 | - public function uniqueStrict($key = null) |
|
853 | - { |
|
854 | - return $this->unique($key, true); |
|
855 | - } |
|
856 | - |
|
857 | - /** |
|
858 | - * Collect the values into a collection. |
|
859 | - * |
|
860 | - * @return \Illuminate\Support\Collection |
|
861 | - */ |
|
862 | - public function collect() |
|
863 | - { |
|
864 | - return new Collection($this->all()); |
|
865 | - } |
|
866 | - |
|
867 | - /** |
|
868 | - * Get the collection of items as a plain array. |
|
869 | - * |
|
870 | - * @return array |
|
871 | - */ |
|
872 | - public function toArray() |
|
873 | - { |
|
874 | - return $this->map(function ($value) { |
|
875 | - return $value instanceof Arrayable ? $value->toArray() : $value; |
|
876 | - })->all(); |
|
877 | - } |
|
878 | - |
|
879 | - /** |
|
880 | - * Convert the object into something JSON serializable. |
|
881 | - * |
|
882 | - * @return array |
|
883 | - */ |
|
884 | - #[\ReturnTypeWillChange] |
|
885 | - public function jsonSerialize() |
|
886 | - { |
|
887 | - return array_map(function ($value) { |
|
888 | - if ($value instanceof JsonSerializable) { |
|
889 | - return $value->jsonSerialize(); |
|
890 | - } elseif ($value instanceof Jsonable) { |
|
891 | - return json_decode($value->toJson(), true); |
|
892 | - } elseif ($value instanceof Arrayable) { |
|
893 | - return $value->toArray(); |
|
894 | - } |
|
895 | - |
|
896 | - return $value; |
|
897 | - }, $this->all()); |
|
898 | - } |
|
899 | - |
|
900 | - /** |
|
901 | - * Get the collection of items as JSON. |
|
902 | - * |
|
903 | - * @param int $options |
|
904 | - * @return string |
|
905 | - */ |
|
906 | - public function toJson($options = 0) |
|
907 | - { |
|
908 | - return json_encode($this->jsonSerialize(), $options); |
|
909 | - } |
|
910 | - |
|
911 | - /** |
|
912 | - * Get a CachingIterator instance. |
|
913 | - * |
|
914 | - * @param int $flags |
|
915 | - * @return \CachingIterator |
|
916 | - */ |
|
917 | - public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
918 | - { |
|
919 | - return new CachingIterator($this->getIterator(), $flags); |
|
920 | - } |
|
921 | - |
|
922 | - /** |
|
923 | - * Convert the collection to its string representation. |
|
924 | - * |
|
925 | - * @return string |
|
926 | - */ |
|
927 | - public function __toString() |
|
928 | - { |
|
929 | - return $this->escapeWhenCastingToString |
|
930 | - ? e($this->toJson()) |
|
931 | - : $this->toJson(); |
|
932 | - } |
|
933 | - |
|
934 | - /** |
|
935 | - * Indicate that the model's string representation should be escaped when __toString is invoked. |
|
936 | - * |
|
937 | - * @param bool $escape |
|
938 | - * @return $this |
|
939 | - */ |
|
940 | - public function escapeWhenCastingToString($escape = true) |
|
941 | - { |
|
942 | - $this->escapeWhenCastingToString = $escape; |
|
943 | - |
|
944 | - return $this; |
|
945 | - } |
|
946 | - |
|
947 | - /** |
|
948 | - * Add a method to the list of proxied methods. |
|
949 | - * |
|
950 | - * @param string $method |
|
951 | - * @return void |
|
952 | - */ |
|
953 | - public static function proxy($method) |
|
954 | - { |
|
955 | - static::$proxies[] = $method; |
|
956 | - } |
|
957 | - |
|
958 | - /** |
|
959 | - * Dynamically access collection proxies. |
|
960 | - * |
|
961 | - * @param string $key |
|
962 | - * @return mixed |
|
963 | - * |
|
964 | - * @throws \Exception |
|
965 | - */ |
|
966 | - public function __get($key) |
|
967 | - { |
|
968 | - if (! in_array($key, static::$proxies)) { |
|
969 | - throw new Exception("Property [{$key}] does not exist on this collection instance."); |
|
970 | - } |
|
971 | - |
|
972 | - return new HigherOrderCollectionProxy($this, $key); |
|
973 | - } |
|
974 | - |
|
975 | - /** |
|
976 | - * Results array of items from Collection or Arrayable. |
|
977 | - * |
|
978 | - * @param mixed $items |
|
979 | - * @return array |
|
980 | - */ |
|
981 | - protected function getArrayableItems($items) |
|
982 | - { |
|
983 | - if (is_array($items)) { |
|
984 | - return $items; |
|
985 | - } elseif ($items instanceof Enumerable) { |
|
986 | - return $items->all(); |
|
987 | - } elseif ($items instanceof Arrayable) { |
|
988 | - return $items->toArray(); |
|
989 | - } elseif ($items instanceof Jsonable) { |
|
990 | - return json_decode($items->toJson(), true); |
|
991 | - } elseif ($items instanceof JsonSerializable) { |
|
992 | - return (array) $items->jsonSerialize(); |
|
993 | - } elseif ($items instanceof Traversable) { |
|
994 | - return iterator_to_array($items); |
|
995 | - } elseif ($items instanceof UnitEnum) { |
|
996 | - return [$items]; |
|
997 | - } |
|
998 | - |
|
999 | - return (array) $items; |
|
1000 | - } |
|
1001 | - |
|
1002 | - /** |
|
1003 | - * Get an operator checker callback. |
|
1004 | - * |
|
1005 | - * @param string $key |
|
1006 | - * @param string|null $operator |
|
1007 | - * @param mixed $value |
|
1008 | - * @return \Closure |
|
1009 | - */ |
|
1010 | - protected function operatorForWhere($key, $operator = null, $value = null) |
|
1011 | - { |
|
1012 | - if (func_num_args() === 1) { |
|
1013 | - $value = true; |
|
1014 | - |
|
1015 | - $operator = '='; |
|
1016 | - } |
|
1017 | - |
|
1018 | - if (func_num_args() === 2) { |
|
1019 | - $value = $operator; |
|
1020 | - |
|
1021 | - $operator = '='; |
|
1022 | - } |
|
1023 | - |
|
1024 | - return function ($item) use ($key, $operator, $value) { |
|
1025 | - $retrieved = data_get($item, $key); |
|
1026 | - |
|
1027 | - $strings = array_filter([$retrieved, $value], function ($value) { |
|
1028 | - return is_string($value) || (is_object($value) && method_exists($value, '__toString')); |
|
1029 | - }); |
|
1030 | - |
|
1031 | - if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) { |
|
1032 | - return in_array($operator, ['!=', '<>', '!==']); |
|
1033 | - } |
|
1034 | - |
|
1035 | - switch ($operator) { |
|
1036 | - default: |
|
1037 | - case '=': |
|
1038 | - case '==': return $retrieved == $value; |
|
1039 | - case '!=': |
|
1040 | - case '<>': return $retrieved != $value; |
|
1041 | - case '<': return $retrieved < $value; |
|
1042 | - case '>': return $retrieved > $value; |
|
1043 | - case '<=': return $retrieved <= $value; |
|
1044 | - case '>=': return $retrieved >= $value; |
|
1045 | - case '===': return $retrieved === $value; |
|
1046 | - case '!==': return $retrieved !== $value; |
|
1047 | - } |
|
1048 | - }; |
|
1049 | - } |
|
1050 | - |
|
1051 | - /** |
|
1052 | - * Determine if the given value is callable, but not a string. |
|
1053 | - * |
|
1054 | - * @param mixed $value |
|
1055 | - * @return bool |
|
1056 | - */ |
|
1057 | - protected function useAsCallable($value) |
|
1058 | - { |
|
1059 | - return ! is_string($value) && is_callable($value); |
|
1060 | - } |
|
1061 | - |
|
1062 | - /** |
|
1063 | - * Get a value retrieving callback. |
|
1064 | - * |
|
1065 | - * @param callable|string|null $value |
|
1066 | - * @return callable |
|
1067 | - */ |
|
1068 | - protected function valueRetriever($value) |
|
1069 | - { |
|
1070 | - if ($this->useAsCallable($value)) { |
|
1071 | - return $value; |
|
1072 | - } |
|
1073 | - |
|
1074 | - return function ($item) use ($value) { |
|
1075 | - return data_get($item, $value); |
|
1076 | - }; |
|
1077 | - } |
|
1078 | - |
|
1079 | - /** |
|
1080 | - * Make a function to check an item's equality. |
|
1081 | - * |
|
1082 | - * @param mixed $value |
|
1083 | - * @return \Closure |
|
1084 | - */ |
|
1085 | - protected function equality($value) |
|
1086 | - { |
|
1087 | - return function ($item) use ($value) { |
|
1088 | - return $item === $value; |
|
1089 | - }; |
|
1090 | - } |
|
1091 | - |
|
1092 | - /** |
|
1093 | - * Make a function using another function, by negating its result. |
|
1094 | - * |
|
1095 | - * @param \Closure $callback |
|
1096 | - * @return \Closure |
|
1097 | - */ |
|
1098 | - protected function negate(Closure $callback) |
|
1099 | - { |
|
1100 | - return function (...$params) use ($callback) { |
|
1101 | - return ! $callback(...$params); |
|
1102 | - }; |
|
1103 | - } |
|
1104 | - |
|
1105 | - /** |
|
1106 | - * Make a function that returns what's passed to it. |
|
1107 | - * |
|
1108 | - * @return \Closure |
|
1109 | - */ |
|
1110 | - protected function identity() |
|
1111 | - { |
|
1112 | - return function ($value) { |
|
1113 | - return $value; |
|
1114 | - }; |
|
1115 | - } |
|
51 | + /** |
|
52 | + * Indicates that the object's string representation should be escaped when __toString is invoked. |
|
53 | + * |
|
54 | + * @var bool |
|
55 | + */ |
|
56 | + protected $escapeWhenCastingToString = false; |
|
57 | + |
|
58 | + /** |
|
59 | + * The methods that can be proxied. |
|
60 | + * |
|
61 | + * @var string[] |
|
62 | + */ |
|
63 | + protected static $proxies = [ |
|
64 | + 'average', |
|
65 | + 'avg', |
|
66 | + 'contains', |
|
67 | + 'doesntContain', |
|
68 | + 'each', |
|
69 | + 'every', |
|
70 | + 'filter', |
|
71 | + 'first', |
|
72 | + 'flatMap', |
|
73 | + 'groupBy', |
|
74 | + 'keyBy', |
|
75 | + 'map', |
|
76 | + 'max', |
|
77 | + 'min', |
|
78 | + 'partition', |
|
79 | + 'reject', |
|
80 | + 'skipUntil', |
|
81 | + 'skipWhile', |
|
82 | + 'some', |
|
83 | + 'sortBy', |
|
84 | + 'sortByDesc', |
|
85 | + 'sum', |
|
86 | + 'takeUntil', |
|
87 | + 'takeWhile', |
|
88 | + 'unique', |
|
89 | + 'until', |
|
90 | + ]; |
|
91 | + |
|
92 | + /** |
|
93 | + * Create a new collection instance if the value isn't one already. |
|
94 | + * |
|
95 | + * @param mixed $items |
|
96 | + * @return static |
|
97 | + */ |
|
98 | + public static function make($items = []) |
|
99 | + { |
|
100 | + return new static($items); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Wrap the given value in a collection if applicable. |
|
105 | + * |
|
106 | + * @param mixed $value |
|
107 | + * @return static |
|
108 | + */ |
|
109 | + public static function wrap($value) |
|
110 | + { |
|
111 | + return $value instanceof Enumerable |
|
112 | + ? new static($value) |
|
113 | + : new static(Arr::wrap($value)); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Get the underlying items from the given collection if applicable. |
|
118 | + * |
|
119 | + * @param array|static $value |
|
120 | + * @return array |
|
121 | + */ |
|
122 | + public static function unwrap($value) |
|
123 | + { |
|
124 | + return $value instanceof Enumerable ? $value->all() : $value; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Create a new instance with no items. |
|
129 | + * |
|
130 | + * @return static |
|
131 | + */ |
|
132 | + public static function empty() |
|
133 | + { |
|
134 | + return new static([]); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Create a new collection by invoking the callback a given amount of times. |
|
139 | + * |
|
140 | + * @param int $number |
|
141 | + * @param callable|null $callback |
|
142 | + * @return static |
|
143 | + */ |
|
144 | + public static function times($number, callable $callback = null) |
|
145 | + { |
|
146 | + if ($number < 1) { |
|
147 | + return new static; |
|
148 | + } |
|
149 | + |
|
150 | + return static::range(1, $number) |
|
151 | + ->when($callback) |
|
152 | + ->map($callback); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Alias for the "avg" method. |
|
157 | + * |
|
158 | + * @param callable|string|null $callback |
|
159 | + * @return mixed |
|
160 | + */ |
|
161 | + public function average($callback = null) |
|
162 | + { |
|
163 | + return $this->avg($callback); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Alias for the "contains" method. |
|
168 | + * |
|
169 | + * @param mixed $key |
|
170 | + * @param mixed $operator |
|
171 | + * @param mixed $value |
|
172 | + * @return bool |
|
173 | + */ |
|
174 | + public function some($key, $operator = null, $value = null) |
|
175 | + { |
|
176 | + return $this->contains(...func_get_args()); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Determine if an item exists, using strict comparison. |
|
181 | + * |
|
182 | + * @param mixed $key |
|
183 | + * @param mixed $value |
|
184 | + * @return bool |
|
185 | + */ |
|
186 | + public function containsStrict($key, $value = null) |
|
187 | + { |
|
188 | + if (func_num_args() === 2) { |
|
189 | + return $this->contains(function ($item) use ($key, $value) { |
|
190 | + return data_get($item, $key) === $value; |
|
191 | + }); |
|
192 | + } |
|
193 | + |
|
194 | + if ($this->useAsCallable($key)) { |
|
195 | + return ! is_null($this->first($key)); |
|
196 | + } |
|
197 | + |
|
198 | + foreach ($this as $item) { |
|
199 | + if ($item === $key) { |
|
200 | + return true; |
|
201 | + } |
|
202 | + } |
|
203 | + |
|
204 | + return false; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Dump the items and end the script. |
|
209 | + * |
|
210 | + * @param mixed ...$args |
|
211 | + * @return void |
|
212 | + */ |
|
213 | + public function dd(...$args) |
|
214 | + { |
|
215 | + $this->dump(...$args); |
|
216 | + |
|
217 | + exit(1); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * Dump the items. |
|
222 | + * |
|
223 | + * @return $this |
|
224 | + */ |
|
225 | + public function dump() |
|
226 | + { |
|
227 | + (new Collection(func_get_args())) |
|
228 | + ->push($this->all()) |
|
229 | + ->each(function ($item) { |
|
230 | + VarDumper::dump($item); |
|
231 | + }); |
|
232 | + |
|
233 | + return $this; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Execute a callback over each item. |
|
238 | + * |
|
239 | + * @param callable $callback |
|
240 | + * @return $this |
|
241 | + */ |
|
242 | + public function each(callable $callback) |
|
243 | + { |
|
244 | + foreach ($this as $key => $item) { |
|
245 | + if ($callback($item, $key) === false) { |
|
246 | + break; |
|
247 | + } |
|
248 | + } |
|
249 | + |
|
250 | + return $this; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Execute a callback over each nested chunk of items. |
|
255 | + * |
|
256 | + * @param callable $callback |
|
257 | + * @return static |
|
258 | + */ |
|
259 | + public function eachSpread(callable $callback) |
|
260 | + { |
|
261 | + return $this->each(function ($chunk, $key) use ($callback) { |
|
262 | + $chunk[] = $key; |
|
263 | + |
|
264 | + return $callback(...$chunk); |
|
265 | + }); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Determine if all items pass the given truth test. |
|
270 | + * |
|
271 | + * @param string|callable $key |
|
272 | + * @param mixed $operator |
|
273 | + * @param mixed $value |
|
274 | + * @return bool |
|
275 | + */ |
|
276 | + public function every($key, $operator = null, $value = null) |
|
277 | + { |
|
278 | + if (func_num_args() === 1) { |
|
279 | + $callback = $this->valueRetriever($key); |
|
280 | + |
|
281 | + foreach ($this as $k => $v) { |
|
282 | + if (! $callback($v, $k)) { |
|
283 | + return false; |
|
284 | + } |
|
285 | + } |
|
286 | + |
|
287 | + return true; |
|
288 | + } |
|
289 | + |
|
290 | + return $this->every($this->operatorForWhere(...func_get_args())); |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * Get the first item by the given key value pair. |
|
295 | + * |
|
296 | + * @param string $key |
|
297 | + * @param mixed $operator |
|
298 | + * @param mixed $value |
|
299 | + * @return mixed |
|
300 | + */ |
|
301 | + public function firstWhere($key, $operator = null, $value = null) |
|
302 | + { |
|
303 | + return $this->first($this->operatorForWhere(...func_get_args())); |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Determine if the collection is not empty. |
|
308 | + * |
|
309 | + * @return bool |
|
310 | + */ |
|
311 | + public function isNotEmpty() |
|
312 | + { |
|
313 | + return ! $this->isEmpty(); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Run a map over each nested chunk of items. |
|
318 | + * |
|
319 | + * @param callable $callback |
|
320 | + * @return static |
|
321 | + */ |
|
322 | + public function mapSpread(callable $callback) |
|
323 | + { |
|
324 | + return $this->map(function ($chunk, $key) use ($callback) { |
|
325 | + $chunk[] = $key; |
|
326 | + |
|
327 | + return $callback(...$chunk); |
|
328 | + }); |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * Run a grouping map over the items. |
|
333 | + * |
|
334 | + * The callback should return an associative array with a single key/value pair. |
|
335 | + * |
|
336 | + * @param callable $callback |
|
337 | + * @return static |
|
338 | + */ |
|
339 | + public function mapToGroups(callable $callback) |
|
340 | + { |
|
341 | + $groups = $this->mapToDictionary($callback); |
|
342 | + |
|
343 | + return $groups->map([$this, 'make']); |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Map a collection and flatten the result by a single level. |
|
348 | + * |
|
349 | + * @param callable $callback |
|
350 | + * @return static |
|
351 | + */ |
|
352 | + public function flatMap(callable $callback) |
|
353 | + { |
|
354 | + return $this->map($callback)->collapse(); |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Map the values into a new class. |
|
359 | + * |
|
360 | + * @param string $class |
|
361 | + * @return static |
|
362 | + */ |
|
363 | + public function mapInto($class) |
|
364 | + { |
|
365 | + return $this->map(function ($value, $key) use ($class) { |
|
366 | + return new $class($value, $key); |
|
367 | + }); |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * Get the min value of a given key. |
|
372 | + * |
|
373 | + * @param callable|string|null $callback |
|
374 | + * @return mixed |
|
375 | + */ |
|
376 | + public function min($callback = null) |
|
377 | + { |
|
378 | + $callback = $this->valueRetriever($callback); |
|
379 | + |
|
380 | + return $this->map(function ($value) use ($callback) { |
|
381 | + return $callback($value); |
|
382 | + })->filter(function ($value) { |
|
383 | + return ! is_null($value); |
|
384 | + })->reduce(function ($result, $value) { |
|
385 | + return is_null($result) || $value < $result ? $value : $result; |
|
386 | + }); |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Get the max value of a given key. |
|
391 | + * |
|
392 | + * @param callable|string|null $callback |
|
393 | + * @return mixed |
|
394 | + */ |
|
395 | + public function max($callback = null) |
|
396 | + { |
|
397 | + $callback = $this->valueRetriever($callback); |
|
398 | + |
|
399 | + return $this->filter(function ($value) { |
|
400 | + return ! is_null($value); |
|
401 | + })->reduce(function ($result, $item) use ($callback) { |
|
402 | + $value = $callback($item); |
|
403 | + |
|
404 | + return is_null($result) || $value > $result ? $value : $result; |
|
405 | + }); |
|
406 | + } |
|
407 | + |
|
408 | + /** |
|
409 | + * "Paginate" the collection by slicing it into a smaller collection. |
|
410 | + * |
|
411 | + * @param int $page |
|
412 | + * @param int $perPage |
|
413 | + * @return static |
|
414 | + */ |
|
415 | + public function forPage($page, $perPage) |
|
416 | + { |
|
417 | + $offset = max(0, ($page - 1) * $perPage); |
|
418 | + |
|
419 | + return $this->slice($offset, $perPage); |
|
420 | + } |
|
421 | + |
|
422 | + /** |
|
423 | + * Partition the collection into two arrays using the given callback or key. |
|
424 | + * |
|
425 | + * @param callable|string $key |
|
426 | + * @param mixed $operator |
|
427 | + * @param mixed $value |
|
428 | + * @return static |
|
429 | + */ |
|
430 | + public function partition($key, $operator = null, $value = null) |
|
431 | + { |
|
432 | + $passed = []; |
|
433 | + $failed = []; |
|
434 | + |
|
435 | + $callback = func_num_args() === 1 |
|
436 | + ? $this->valueRetriever($key) |
|
437 | + : $this->operatorForWhere(...func_get_args()); |
|
438 | + |
|
439 | + foreach ($this as $key => $item) { |
|
440 | + if ($callback($item, $key)) { |
|
441 | + $passed[$key] = $item; |
|
442 | + } else { |
|
443 | + $failed[$key] = $item; |
|
444 | + } |
|
445 | + } |
|
446 | + |
|
447 | + return new static([new static($passed), new static($failed)]); |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Get the sum of the given values. |
|
452 | + * |
|
453 | + * @param callable|string|null $callback |
|
454 | + * @return mixed |
|
455 | + */ |
|
456 | + public function sum($callback = null) |
|
457 | + { |
|
458 | + $callback = is_null($callback) |
|
459 | + ? $this->identity() |
|
460 | + : $this->valueRetriever($callback); |
|
461 | + |
|
462 | + return $this->reduce(function ($result, $item) use ($callback) { |
|
463 | + return $result + $callback($item); |
|
464 | + }, 0); |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * Apply the callback if the value is truthy. |
|
469 | + * |
|
470 | + * @param bool|mixed $value |
|
471 | + * @param callable|null $callback |
|
472 | + * @param callable|null $default |
|
473 | + * @return static|mixed |
|
474 | + */ |
|
475 | + public function when($value, callable $callback = null, callable $default = null) |
|
476 | + { |
|
477 | + if (! $callback) { |
|
478 | + return new HigherOrderWhenProxy($this, $value); |
|
479 | + } |
|
480 | + |
|
481 | + if ($value) { |
|
482 | + return $callback($this, $value); |
|
483 | + } elseif ($default) { |
|
484 | + return $default($this, $value); |
|
485 | + } |
|
486 | + |
|
487 | + return $this; |
|
488 | + } |
|
489 | + |
|
490 | + /** |
|
491 | + * Apply the callback if the collection is empty. |
|
492 | + * |
|
493 | + * @param callable $callback |
|
494 | + * @param callable|null $default |
|
495 | + * @return static|mixed |
|
496 | + */ |
|
497 | + public function whenEmpty(callable $callback, callable $default = null) |
|
498 | + { |
|
499 | + return $this->when($this->isEmpty(), $callback, $default); |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * Apply the callback if the collection is not empty. |
|
504 | + * |
|
505 | + * @param callable $callback |
|
506 | + * @param callable|null $default |
|
507 | + * @return static|mixed |
|
508 | + */ |
|
509 | + public function whenNotEmpty(callable $callback, callable $default = null) |
|
510 | + { |
|
511 | + return $this->when($this->isNotEmpty(), $callback, $default); |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * Apply the callback if the value is falsy. |
|
516 | + * |
|
517 | + * @param bool $value |
|
518 | + * @param callable $callback |
|
519 | + * @param callable|null $default |
|
520 | + * @return static|mixed |
|
521 | + */ |
|
522 | + public function unless($value, callable $callback, callable $default = null) |
|
523 | + { |
|
524 | + return $this->when(! $value, $callback, $default); |
|
525 | + } |
|
526 | + |
|
527 | + /** |
|
528 | + * Apply the callback unless the collection is empty. |
|
529 | + * |
|
530 | + * @param callable $callback |
|
531 | + * @param callable|null $default |
|
532 | + * @return static|mixed |
|
533 | + */ |
|
534 | + public function unlessEmpty(callable $callback, callable $default = null) |
|
535 | + { |
|
536 | + return $this->whenNotEmpty($callback, $default); |
|
537 | + } |
|
538 | + |
|
539 | + /** |
|
540 | + * Apply the callback unless the collection is not empty. |
|
541 | + * |
|
542 | + * @param callable $callback |
|
543 | + * @param callable|null $default |
|
544 | + * @return static|mixed |
|
545 | + */ |
|
546 | + public function unlessNotEmpty(callable $callback, callable $default = null) |
|
547 | + { |
|
548 | + return $this->whenEmpty($callback, $default); |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * Filter items by the given key value pair. |
|
553 | + * |
|
554 | + * @param string $key |
|
555 | + * @param mixed $operator |
|
556 | + * @param mixed $value |
|
557 | + * @return static |
|
558 | + */ |
|
559 | + public function where($key, $operator = null, $value = null) |
|
560 | + { |
|
561 | + return $this->filter($this->operatorForWhere(...func_get_args())); |
|
562 | + } |
|
563 | + |
|
564 | + /** |
|
565 | + * Filter items where the value for the given key is null. |
|
566 | + * |
|
567 | + * @param string|null $key |
|
568 | + * @return static |
|
569 | + */ |
|
570 | + public function whereNull($key = null) |
|
571 | + { |
|
572 | + return $this->whereStrict($key, null); |
|
573 | + } |
|
574 | + |
|
575 | + /** |
|
576 | + * Filter items where the value for the given key is not null. |
|
577 | + * |
|
578 | + * @param string|null $key |
|
579 | + * @return static |
|
580 | + */ |
|
581 | + public function whereNotNull($key = null) |
|
582 | + { |
|
583 | + return $this->where($key, '!==', null); |
|
584 | + } |
|
585 | + |
|
586 | + /** |
|
587 | + * Filter items by the given key value pair using strict comparison. |
|
588 | + * |
|
589 | + * @param string $key |
|
590 | + * @param mixed $value |
|
591 | + * @return static |
|
592 | + */ |
|
593 | + public function whereStrict($key, $value) |
|
594 | + { |
|
595 | + return $this->where($key, '===', $value); |
|
596 | + } |
|
597 | + |
|
598 | + /** |
|
599 | + * Filter items by the given key value pair. |
|
600 | + * |
|
601 | + * @param string $key |
|
602 | + * @param mixed $values |
|
603 | + * @param bool $strict |
|
604 | + * @return static |
|
605 | + */ |
|
606 | + public function whereIn($key, $values, $strict = false) |
|
607 | + { |
|
608 | + $values = $this->getArrayableItems($values); |
|
609 | + |
|
610 | + return $this->filter(function ($item) use ($key, $values, $strict) { |
|
611 | + return in_array(data_get($item, $key), $values, $strict); |
|
612 | + }); |
|
613 | + } |
|
614 | + |
|
615 | + /** |
|
616 | + * Filter items by the given key value pair using strict comparison. |
|
617 | + * |
|
618 | + * @param string $key |
|
619 | + * @param mixed $values |
|
620 | + * @return static |
|
621 | + */ |
|
622 | + public function whereInStrict($key, $values) |
|
623 | + { |
|
624 | + return $this->whereIn($key, $values, true); |
|
625 | + } |
|
626 | + |
|
627 | + /** |
|
628 | + * Filter items such that the value of the given key is between the given values. |
|
629 | + * |
|
630 | + * @param string $key |
|
631 | + * @param array $values |
|
632 | + * @return static |
|
633 | + */ |
|
634 | + public function whereBetween($key, $values) |
|
635 | + { |
|
636 | + return $this->where($key, '>=', reset($values))->where($key, '<=', end($values)); |
|
637 | + } |
|
638 | + |
|
639 | + /** |
|
640 | + * Filter items such that the value of the given key is not between the given values. |
|
641 | + * |
|
642 | + * @param string $key |
|
643 | + * @param array $values |
|
644 | + * @return static |
|
645 | + */ |
|
646 | + public function whereNotBetween($key, $values) |
|
647 | + { |
|
648 | + return $this->filter(function ($item) use ($key, $values) { |
|
649 | + return data_get($item, $key) < reset($values) || data_get($item, $key) > end($values); |
|
650 | + }); |
|
651 | + } |
|
652 | + |
|
653 | + /** |
|
654 | + * Filter items by the given key value pair. |
|
655 | + * |
|
656 | + * @param string $key |
|
657 | + * @param mixed $values |
|
658 | + * @param bool $strict |
|
659 | + * @return static |
|
660 | + */ |
|
661 | + public function whereNotIn($key, $values, $strict = false) |
|
662 | + { |
|
663 | + $values = $this->getArrayableItems($values); |
|
664 | + |
|
665 | + return $this->reject(function ($item) use ($key, $values, $strict) { |
|
666 | + return in_array(data_get($item, $key), $values, $strict); |
|
667 | + }); |
|
668 | + } |
|
669 | + |
|
670 | + /** |
|
671 | + * Filter items by the given key value pair using strict comparison. |
|
672 | + * |
|
673 | + * @param string $key |
|
674 | + * @param mixed $values |
|
675 | + * @return static |
|
676 | + */ |
|
677 | + public function whereNotInStrict($key, $values) |
|
678 | + { |
|
679 | + return $this->whereNotIn($key, $values, true); |
|
680 | + } |
|
681 | + |
|
682 | + /** |
|
683 | + * Filter the items, removing any items that don't match the given type(s). |
|
684 | + * |
|
685 | + * @param string|string[] $type |
|
686 | + * @return static |
|
687 | + */ |
|
688 | + public function whereInstanceOf($type) |
|
689 | + { |
|
690 | + return $this->filter(function ($value) use ($type) { |
|
691 | + if (is_array($type)) { |
|
692 | + foreach ($type as $classType) { |
|
693 | + if ($value instanceof $classType) { |
|
694 | + return true; |
|
695 | + } |
|
696 | + } |
|
697 | + |
|
698 | + return false; |
|
699 | + } |
|
700 | + |
|
701 | + return $value instanceof $type; |
|
702 | + }); |
|
703 | + } |
|
704 | + |
|
705 | + /** |
|
706 | + * Pass the collection to the given callback and return the result. |
|
707 | + * |
|
708 | + * @param callable $callback |
|
709 | + * @return mixed |
|
710 | + */ |
|
711 | + public function pipe(callable $callback) |
|
712 | + { |
|
713 | + return $callback($this); |
|
714 | + } |
|
715 | + |
|
716 | + /** |
|
717 | + * Pass the collection into a new class. |
|
718 | + * |
|
719 | + * @param string $class |
|
720 | + * @return mixed |
|
721 | + */ |
|
722 | + public function pipeInto($class) |
|
723 | + { |
|
724 | + return new $class($this); |
|
725 | + } |
|
726 | + |
|
727 | + /** |
|
728 | + * Pass the collection through a series of callable pipes and return the result. |
|
729 | + * |
|
730 | + * @param array<callable> $pipes |
|
731 | + * @return mixed |
|
732 | + */ |
|
733 | + public function pipeThrough($pipes) |
|
734 | + { |
|
735 | + return static::make($pipes)->reduce( |
|
736 | + function ($carry, $pipe) { |
|
737 | + return $pipe($carry); |
|
738 | + }, |
|
739 | + $this |
|
740 | + ); |
|
741 | + } |
|
742 | + |
|
743 | + /** |
|
744 | + * Pass the collection to the given callback and then return it. |
|
745 | + * |
|
746 | + * @param callable $callback |
|
747 | + * @return $this |
|
748 | + */ |
|
749 | + public function tap(callable $callback) |
|
750 | + { |
|
751 | + $callback(clone $this); |
|
752 | + |
|
753 | + return $this; |
|
754 | + } |
|
755 | + |
|
756 | + /** |
|
757 | + * Reduce the collection to a single value. |
|
758 | + * |
|
759 | + * @param callable $callback |
|
760 | + * @param mixed $initial |
|
761 | + * @return mixed |
|
762 | + */ |
|
763 | + public function reduce(callable $callback, $initial = null) |
|
764 | + { |
|
765 | + $result = $initial; |
|
766 | + |
|
767 | + foreach ($this as $key => $value) { |
|
768 | + $result = $callback($result, $value, $key); |
|
769 | + } |
|
770 | + |
|
771 | + return $result; |
|
772 | + } |
|
773 | + |
|
774 | + /** |
|
775 | + * Reduce the collection to multiple aggregate values. |
|
776 | + * |
|
777 | + * @param callable $callback |
|
778 | + * @param mixed ...$initial |
|
779 | + * @return array |
|
780 | + * |
|
781 | + * @deprecated Use "reduceSpread" instead |
|
782 | + * |
|
783 | + * @throws \UnexpectedValueException |
|
784 | + */ |
|
785 | + public function reduceMany(callable $callback, ...$initial) |
|
786 | + { |
|
787 | + return $this->reduceSpread($callback, ...$initial); |
|
788 | + } |
|
789 | + |
|
790 | + /** |
|
791 | + * Reduce the collection to multiple aggregate values. |
|
792 | + * |
|
793 | + * @param callable $callback |
|
794 | + * @param mixed ...$initial |
|
795 | + * @return array |
|
796 | + * |
|
797 | + * @throws \UnexpectedValueException |
|
798 | + */ |
|
799 | + public function reduceSpread(callable $callback, ...$initial) |
|
800 | + { |
|
801 | + $result = $initial; |
|
802 | + |
|
803 | + foreach ($this as $key => $value) { |
|
804 | + $result = call_user_func_array($callback, array_merge($result, [$value, $key])); |
|
805 | + |
|
806 | + if (! is_array($result)) { |
|
807 | + throw new UnexpectedValueException(sprintf( |
|
808 | + "%s::reduceMany expects reducer to return an array, but got a '%s' instead.", |
|
809 | + class_basename(static::class), gettype($result) |
|
810 | + )); |
|
811 | + } |
|
812 | + } |
|
813 | + |
|
814 | + return $result; |
|
815 | + } |
|
816 | + |
|
817 | + /** |
|
818 | + * Reduce an associative collection to a single value. |
|
819 | + * |
|
820 | + * @param callable $callback |
|
821 | + * @param mixed $initial |
|
822 | + * @return mixed |
|
823 | + */ |
|
824 | + public function reduceWithKeys(callable $callback, $initial = null) |
|
825 | + { |
|
826 | + return $this->reduce($callback, $initial); |
|
827 | + } |
|
828 | + |
|
829 | + /** |
|
830 | + * Create a collection of all elements that do not pass a given truth test. |
|
831 | + * |
|
832 | + * @param callable|mixed $callback |
|
833 | + * @return static |
|
834 | + */ |
|
835 | + public function reject($callback = true) |
|
836 | + { |
|
837 | + $useAsCallable = $this->useAsCallable($callback); |
|
838 | + |
|
839 | + return $this->filter(function ($value, $key) use ($callback, $useAsCallable) { |
|
840 | + return $useAsCallable |
|
841 | + ? ! $callback($value, $key) |
|
842 | + : $value != $callback; |
|
843 | + }); |
|
844 | + } |
|
845 | + |
|
846 | + /** |
|
847 | + * Return only unique items from the collection array using strict comparison. |
|
848 | + * |
|
849 | + * @param string|callable|null $key |
|
850 | + * @return static |
|
851 | + */ |
|
852 | + public function uniqueStrict($key = null) |
|
853 | + { |
|
854 | + return $this->unique($key, true); |
|
855 | + } |
|
856 | + |
|
857 | + /** |
|
858 | + * Collect the values into a collection. |
|
859 | + * |
|
860 | + * @return \Illuminate\Support\Collection |
|
861 | + */ |
|
862 | + public function collect() |
|
863 | + { |
|
864 | + return new Collection($this->all()); |
|
865 | + } |
|
866 | + |
|
867 | + /** |
|
868 | + * Get the collection of items as a plain array. |
|
869 | + * |
|
870 | + * @return array |
|
871 | + */ |
|
872 | + public function toArray() |
|
873 | + { |
|
874 | + return $this->map(function ($value) { |
|
875 | + return $value instanceof Arrayable ? $value->toArray() : $value; |
|
876 | + })->all(); |
|
877 | + } |
|
878 | + |
|
879 | + /** |
|
880 | + * Convert the object into something JSON serializable. |
|
881 | + * |
|
882 | + * @return array |
|
883 | + */ |
|
884 | + #[\ReturnTypeWillChange] |
|
885 | + public function jsonSerialize() |
|
886 | + { |
|
887 | + return array_map(function ($value) { |
|
888 | + if ($value instanceof JsonSerializable) { |
|
889 | + return $value->jsonSerialize(); |
|
890 | + } elseif ($value instanceof Jsonable) { |
|
891 | + return json_decode($value->toJson(), true); |
|
892 | + } elseif ($value instanceof Arrayable) { |
|
893 | + return $value->toArray(); |
|
894 | + } |
|
895 | + |
|
896 | + return $value; |
|
897 | + }, $this->all()); |
|
898 | + } |
|
899 | + |
|
900 | + /** |
|
901 | + * Get the collection of items as JSON. |
|
902 | + * |
|
903 | + * @param int $options |
|
904 | + * @return string |
|
905 | + */ |
|
906 | + public function toJson($options = 0) |
|
907 | + { |
|
908 | + return json_encode($this->jsonSerialize(), $options); |
|
909 | + } |
|
910 | + |
|
911 | + /** |
|
912 | + * Get a CachingIterator instance. |
|
913 | + * |
|
914 | + * @param int $flags |
|
915 | + * @return \CachingIterator |
|
916 | + */ |
|
917 | + public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
918 | + { |
|
919 | + return new CachingIterator($this->getIterator(), $flags); |
|
920 | + } |
|
921 | + |
|
922 | + /** |
|
923 | + * Convert the collection to its string representation. |
|
924 | + * |
|
925 | + * @return string |
|
926 | + */ |
|
927 | + public function __toString() |
|
928 | + { |
|
929 | + return $this->escapeWhenCastingToString |
|
930 | + ? e($this->toJson()) |
|
931 | + : $this->toJson(); |
|
932 | + } |
|
933 | + |
|
934 | + /** |
|
935 | + * Indicate that the model's string representation should be escaped when __toString is invoked. |
|
936 | + * |
|
937 | + * @param bool $escape |
|
938 | + * @return $this |
|
939 | + */ |
|
940 | + public function escapeWhenCastingToString($escape = true) |
|
941 | + { |
|
942 | + $this->escapeWhenCastingToString = $escape; |
|
943 | + |
|
944 | + return $this; |
|
945 | + } |
|
946 | + |
|
947 | + /** |
|
948 | + * Add a method to the list of proxied methods. |
|
949 | + * |
|
950 | + * @param string $method |
|
951 | + * @return void |
|
952 | + */ |
|
953 | + public static function proxy($method) |
|
954 | + { |
|
955 | + static::$proxies[] = $method; |
|
956 | + } |
|
957 | + |
|
958 | + /** |
|
959 | + * Dynamically access collection proxies. |
|
960 | + * |
|
961 | + * @param string $key |
|
962 | + * @return mixed |
|
963 | + * |
|
964 | + * @throws \Exception |
|
965 | + */ |
|
966 | + public function __get($key) |
|
967 | + { |
|
968 | + if (! in_array($key, static::$proxies)) { |
|
969 | + throw new Exception("Property [{$key}] does not exist on this collection instance."); |
|
970 | + } |
|
971 | + |
|
972 | + return new HigherOrderCollectionProxy($this, $key); |
|
973 | + } |
|
974 | + |
|
975 | + /** |
|
976 | + * Results array of items from Collection or Arrayable. |
|
977 | + * |
|
978 | + * @param mixed $items |
|
979 | + * @return array |
|
980 | + */ |
|
981 | + protected function getArrayableItems($items) |
|
982 | + { |
|
983 | + if (is_array($items)) { |
|
984 | + return $items; |
|
985 | + } elseif ($items instanceof Enumerable) { |
|
986 | + return $items->all(); |
|
987 | + } elseif ($items instanceof Arrayable) { |
|
988 | + return $items->toArray(); |
|
989 | + } elseif ($items instanceof Jsonable) { |
|
990 | + return json_decode($items->toJson(), true); |
|
991 | + } elseif ($items instanceof JsonSerializable) { |
|
992 | + return (array) $items->jsonSerialize(); |
|
993 | + } elseif ($items instanceof Traversable) { |
|
994 | + return iterator_to_array($items); |
|
995 | + } elseif ($items instanceof UnitEnum) { |
|
996 | + return [$items]; |
|
997 | + } |
|
998 | + |
|
999 | + return (array) $items; |
|
1000 | + } |
|
1001 | + |
|
1002 | + /** |
|
1003 | + * Get an operator checker callback. |
|
1004 | + * |
|
1005 | + * @param string $key |
|
1006 | + * @param string|null $operator |
|
1007 | + * @param mixed $value |
|
1008 | + * @return \Closure |
|
1009 | + */ |
|
1010 | + protected function operatorForWhere($key, $operator = null, $value = null) |
|
1011 | + { |
|
1012 | + if (func_num_args() === 1) { |
|
1013 | + $value = true; |
|
1014 | + |
|
1015 | + $operator = '='; |
|
1016 | + } |
|
1017 | + |
|
1018 | + if (func_num_args() === 2) { |
|
1019 | + $value = $operator; |
|
1020 | + |
|
1021 | + $operator = '='; |
|
1022 | + } |
|
1023 | + |
|
1024 | + return function ($item) use ($key, $operator, $value) { |
|
1025 | + $retrieved = data_get($item, $key); |
|
1026 | + |
|
1027 | + $strings = array_filter([$retrieved, $value], function ($value) { |
|
1028 | + return is_string($value) || (is_object($value) && method_exists($value, '__toString')); |
|
1029 | + }); |
|
1030 | + |
|
1031 | + if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) { |
|
1032 | + return in_array($operator, ['!=', '<>', '!==']); |
|
1033 | + } |
|
1034 | + |
|
1035 | + switch ($operator) { |
|
1036 | + default: |
|
1037 | + case '=': |
|
1038 | + case '==': return $retrieved == $value; |
|
1039 | + case '!=': |
|
1040 | + case '<>': return $retrieved != $value; |
|
1041 | + case '<': return $retrieved < $value; |
|
1042 | + case '>': return $retrieved > $value; |
|
1043 | + case '<=': return $retrieved <= $value; |
|
1044 | + case '>=': return $retrieved >= $value; |
|
1045 | + case '===': return $retrieved === $value; |
|
1046 | + case '!==': return $retrieved !== $value; |
|
1047 | + } |
|
1048 | + }; |
|
1049 | + } |
|
1050 | + |
|
1051 | + /** |
|
1052 | + * Determine if the given value is callable, but not a string. |
|
1053 | + * |
|
1054 | + * @param mixed $value |
|
1055 | + * @return bool |
|
1056 | + */ |
|
1057 | + protected function useAsCallable($value) |
|
1058 | + { |
|
1059 | + return ! is_string($value) && is_callable($value); |
|
1060 | + } |
|
1061 | + |
|
1062 | + /** |
|
1063 | + * Get a value retrieving callback. |
|
1064 | + * |
|
1065 | + * @param callable|string|null $value |
|
1066 | + * @return callable |
|
1067 | + */ |
|
1068 | + protected function valueRetriever($value) |
|
1069 | + { |
|
1070 | + if ($this->useAsCallable($value)) { |
|
1071 | + return $value; |
|
1072 | + } |
|
1073 | + |
|
1074 | + return function ($item) use ($value) { |
|
1075 | + return data_get($item, $value); |
|
1076 | + }; |
|
1077 | + } |
|
1078 | + |
|
1079 | + /** |
|
1080 | + * Make a function to check an item's equality. |
|
1081 | + * |
|
1082 | + * @param mixed $value |
|
1083 | + * @return \Closure |
|
1084 | + */ |
|
1085 | + protected function equality($value) |
|
1086 | + { |
|
1087 | + return function ($item) use ($value) { |
|
1088 | + return $item === $value; |
|
1089 | + }; |
|
1090 | + } |
|
1091 | + |
|
1092 | + /** |
|
1093 | + * Make a function using another function, by negating its result. |
|
1094 | + * |
|
1095 | + * @param \Closure $callback |
|
1096 | + * @return \Closure |
|
1097 | + */ |
|
1098 | + protected function negate(Closure $callback) |
|
1099 | + { |
|
1100 | + return function (...$params) use ($callback) { |
|
1101 | + return ! $callback(...$params); |
|
1102 | + }; |
|
1103 | + } |
|
1104 | + |
|
1105 | + /** |
|
1106 | + * Make a function that returns what's passed to it. |
|
1107 | + * |
|
1108 | + * @return \Closure |
|
1109 | + */ |
|
1110 | + protected function identity() |
|
1111 | + { |
|
1112 | + return function ($value) { |
|
1113 | + return $value; |
|
1114 | + }; |
|
1115 | + } |
|
1116 | 1116 | } |
@@ -186,13 +186,13 @@ discard block |
||
186 | 186 | public function containsStrict($key, $value = null) |
187 | 187 | { |
188 | 188 | if (func_num_args() === 2) { |
189 | - return $this->contains(function ($item) use ($key, $value) { |
|
189 | + return $this->contains(function($item) use ($key, $value) { |
|
190 | 190 | return data_get($item, $key) === $value; |
191 | 191 | }); |
192 | 192 | } |
193 | 193 | |
194 | 194 | if ($this->useAsCallable($key)) { |
195 | - return ! is_null($this->first($key)); |
|
195 | + return !is_null($this->first($key)); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | foreach ($this as $item) { |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | { |
227 | 227 | (new Collection(func_get_args())) |
228 | 228 | ->push($this->all()) |
229 | - ->each(function ($item) { |
|
229 | + ->each(function($item) { |
|
230 | 230 | VarDumper::dump($item); |
231 | 231 | }); |
232 | 232 | |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | */ |
259 | 259 | public function eachSpread(callable $callback) |
260 | 260 | { |
261 | - return $this->each(function ($chunk, $key) use ($callback) { |
|
261 | + return $this->each(function($chunk, $key) use ($callback) { |
|
262 | 262 | $chunk[] = $key; |
263 | 263 | |
264 | 264 | return $callback(...$chunk); |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | $callback = $this->valueRetriever($key); |
280 | 280 | |
281 | 281 | foreach ($this as $k => $v) { |
282 | - if (! $callback($v, $k)) { |
|
282 | + if (!$callback($v, $k)) { |
|
283 | 283 | return false; |
284 | 284 | } |
285 | 285 | } |
@@ -310,7 +310,7 @@ discard block |
||
310 | 310 | */ |
311 | 311 | public function isNotEmpty() |
312 | 312 | { |
313 | - return ! $this->isEmpty(); |
|
313 | + return !$this->isEmpty(); |
|
314 | 314 | } |
315 | 315 | |
316 | 316 | /** |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | */ |
322 | 322 | public function mapSpread(callable $callback) |
323 | 323 | { |
324 | - return $this->map(function ($chunk, $key) use ($callback) { |
|
324 | + return $this->map(function($chunk, $key) use ($callback) { |
|
325 | 325 | $chunk[] = $key; |
326 | 326 | |
327 | 327 | return $callback(...$chunk); |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | */ |
363 | 363 | public function mapInto($class) |
364 | 364 | { |
365 | - return $this->map(function ($value, $key) use ($class) { |
|
365 | + return $this->map(function($value, $key) use ($class) { |
|
366 | 366 | return new $class($value, $key); |
367 | 367 | }); |
368 | 368 | } |
@@ -377,11 +377,11 @@ discard block |
||
377 | 377 | { |
378 | 378 | $callback = $this->valueRetriever($callback); |
379 | 379 | |
380 | - return $this->map(function ($value) use ($callback) { |
|
380 | + return $this->map(function($value) use ($callback) { |
|
381 | 381 | return $callback($value); |
382 | - })->filter(function ($value) { |
|
383 | - return ! is_null($value); |
|
384 | - })->reduce(function ($result, $value) { |
|
382 | + })->filter(function($value) { |
|
383 | + return !is_null($value); |
|
384 | + })->reduce(function($result, $value) { |
|
385 | 385 | return is_null($result) || $value < $result ? $value : $result; |
386 | 386 | }); |
387 | 387 | } |
@@ -396,9 +396,9 @@ discard block |
||
396 | 396 | { |
397 | 397 | $callback = $this->valueRetriever($callback); |
398 | 398 | |
399 | - return $this->filter(function ($value) { |
|
400 | - return ! is_null($value); |
|
401 | - })->reduce(function ($result, $item) use ($callback) { |
|
399 | + return $this->filter(function($value) { |
|
400 | + return !is_null($value); |
|
401 | + })->reduce(function($result, $item) use ($callback) { |
|
402 | 402 | $value = $callback($item); |
403 | 403 | |
404 | 404 | return is_null($result) || $value > $result ? $value : $result; |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | ? $this->identity() |
460 | 460 | : $this->valueRetriever($callback); |
461 | 461 | |
462 | - return $this->reduce(function ($result, $item) use ($callback) { |
|
462 | + return $this->reduce(function($result, $item) use ($callback) { |
|
463 | 463 | return $result + $callback($item); |
464 | 464 | }, 0); |
465 | 465 | } |
@@ -474,7 +474,7 @@ discard block |
||
474 | 474 | */ |
475 | 475 | public function when($value, callable $callback = null, callable $default = null) |
476 | 476 | { |
477 | - if (! $callback) { |
|
477 | + if (!$callback) { |
|
478 | 478 | return new HigherOrderWhenProxy($this, $value); |
479 | 479 | } |
480 | 480 | |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | */ |
522 | 522 | public function unless($value, callable $callback, callable $default = null) |
523 | 523 | { |
524 | - return $this->when(! $value, $callback, $default); |
|
524 | + return $this->when(!$value, $callback, $default); |
|
525 | 525 | } |
526 | 526 | |
527 | 527 | /** |
@@ -607,7 +607,7 @@ discard block |
||
607 | 607 | { |
608 | 608 | $values = $this->getArrayableItems($values); |
609 | 609 | |
610 | - return $this->filter(function ($item) use ($key, $values, $strict) { |
|
610 | + return $this->filter(function($item) use ($key, $values, $strict) { |
|
611 | 611 | return in_array(data_get($item, $key), $values, $strict); |
612 | 612 | }); |
613 | 613 | } |
@@ -645,7 +645,7 @@ discard block |
||
645 | 645 | */ |
646 | 646 | public function whereNotBetween($key, $values) |
647 | 647 | { |
648 | - return $this->filter(function ($item) use ($key, $values) { |
|
648 | + return $this->filter(function($item) use ($key, $values) { |
|
649 | 649 | return data_get($item, $key) < reset($values) || data_get($item, $key) > end($values); |
650 | 650 | }); |
651 | 651 | } |
@@ -662,7 +662,7 @@ discard block |
||
662 | 662 | { |
663 | 663 | $values = $this->getArrayableItems($values); |
664 | 664 | |
665 | - return $this->reject(function ($item) use ($key, $values, $strict) { |
|
665 | + return $this->reject(function($item) use ($key, $values, $strict) { |
|
666 | 666 | return in_array(data_get($item, $key), $values, $strict); |
667 | 667 | }); |
668 | 668 | } |
@@ -687,7 +687,7 @@ discard block |
||
687 | 687 | */ |
688 | 688 | public function whereInstanceOf($type) |
689 | 689 | { |
690 | - return $this->filter(function ($value) use ($type) { |
|
690 | + return $this->filter(function($value) use ($type) { |
|
691 | 691 | if (is_array($type)) { |
692 | 692 | foreach ($type as $classType) { |
693 | 693 | if ($value instanceof $classType) { |
@@ -733,7 +733,7 @@ discard block |
||
733 | 733 | public function pipeThrough($pipes) |
734 | 734 | { |
735 | 735 | return static::make($pipes)->reduce( |
736 | - function ($carry, $pipe) { |
|
736 | + function($carry, $pipe) { |
|
737 | 737 | return $pipe($carry); |
738 | 738 | }, |
739 | 739 | $this |
@@ -803,7 +803,7 @@ discard block |
||
803 | 803 | foreach ($this as $key => $value) { |
804 | 804 | $result = call_user_func_array($callback, array_merge($result, [$value, $key])); |
805 | 805 | |
806 | - if (! is_array($result)) { |
|
806 | + if (!is_array($result)) { |
|
807 | 807 | throw new UnexpectedValueException(sprintf( |
808 | 808 | "%s::reduceMany expects reducer to return an array, but got a '%s' instead.", |
809 | 809 | class_basename(static::class), gettype($result) |
@@ -836,9 +836,9 @@ discard block |
||
836 | 836 | { |
837 | 837 | $useAsCallable = $this->useAsCallable($callback); |
838 | 838 | |
839 | - return $this->filter(function ($value, $key) use ($callback, $useAsCallable) { |
|
839 | + return $this->filter(function($value, $key) use ($callback, $useAsCallable) { |
|
840 | 840 | return $useAsCallable |
841 | - ? ! $callback($value, $key) |
|
841 | + ? !$callback($value, $key) |
|
842 | 842 | : $value != $callback; |
843 | 843 | }); |
844 | 844 | } |
@@ -871,7 +871,7 @@ discard block |
||
871 | 871 | */ |
872 | 872 | public function toArray() |
873 | 873 | { |
874 | - return $this->map(function ($value) { |
|
874 | + return $this->map(function($value) { |
|
875 | 875 | return $value instanceof Arrayable ? $value->toArray() : $value; |
876 | 876 | })->all(); |
877 | 877 | } |
@@ -884,7 +884,7 @@ discard block |
||
884 | 884 | #[\ReturnTypeWillChange] |
885 | 885 | public function jsonSerialize() |
886 | 886 | { |
887 | - return array_map(function ($value) { |
|
887 | + return array_map(function($value) { |
|
888 | 888 | if ($value instanceof JsonSerializable) { |
889 | 889 | return $value->jsonSerialize(); |
890 | 890 | } elseif ($value instanceof Jsonable) { |
@@ -965,7 +965,7 @@ discard block |
||
965 | 965 | */ |
966 | 966 | public function __get($key) |
967 | 967 | { |
968 | - if (! in_array($key, static::$proxies)) { |
|
968 | + if (!in_array($key, static::$proxies)) { |
|
969 | 969 | throw new Exception("Property [{$key}] does not exist on this collection instance."); |
970 | 970 | } |
971 | 971 | |
@@ -1021,10 +1021,10 @@ discard block |
||
1021 | 1021 | $operator = '='; |
1022 | 1022 | } |
1023 | 1023 | |
1024 | - return function ($item) use ($key, $operator, $value) { |
|
1024 | + return function($item) use ($key, $operator, $value) { |
|
1025 | 1025 | $retrieved = data_get($item, $key); |
1026 | 1026 | |
1027 | - $strings = array_filter([$retrieved, $value], function ($value) { |
|
1027 | + $strings = array_filter([$retrieved, $value], function($value) { |
|
1028 | 1028 | return is_string($value) || (is_object($value) && method_exists($value, '__toString')); |
1029 | 1029 | }); |
1030 | 1030 | |
@@ -1056,7 +1056,7 @@ discard block |
||
1056 | 1056 | */ |
1057 | 1057 | protected function useAsCallable($value) |
1058 | 1058 | { |
1059 | - return ! is_string($value) && is_callable($value); |
|
1059 | + return !is_string($value) && is_callable($value); |
|
1060 | 1060 | } |
1061 | 1061 | |
1062 | 1062 | /** |
@@ -1071,7 +1071,7 @@ discard block |
||
1071 | 1071 | return $value; |
1072 | 1072 | } |
1073 | 1073 | |
1074 | - return function ($item) use ($value) { |
|
1074 | + return function($item) use ($value) { |
|
1075 | 1075 | return data_get($item, $value); |
1076 | 1076 | }; |
1077 | 1077 | } |
@@ -1084,7 +1084,7 @@ discard block |
||
1084 | 1084 | */ |
1085 | 1085 | protected function equality($value) |
1086 | 1086 | { |
1087 | - return function ($item) use ($value) { |
|
1087 | + return function($item) use ($value) { |
|
1088 | 1088 | return $item === $value; |
1089 | 1089 | }; |
1090 | 1090 | } |
@@ -1097,8 +1097,8 @@ discard block |
||
1097 | 1097 | */ |
1098 | 1098 | protected function negate(Closure $callback) |
1099 | 1099 | { |
1100 | - return function (...$params) use ($callback) { |
|
1101 | - return ! $callback(...$params); |
|
1100 | + return function(...$params) use ($callback) { |
|
1101 | + return !$callback(...$params); |
|
1102 | 1102 | }; |
1103 | 1103 | } |
1104 | 1104 | |
@@ -1109,7 +1109,7 @@ discard block |
||
1109 | 1109 | */ |
1110 | 1110 | protected function identity() |
1111 | 1111 | { |
1112 | - return function ($value) { |
|
1112 | + return function($value) { |
|
1113 | 1113 | return $value; |
1114 | 1114 | }; |
1115 | 1115 | } |
@@ -49,8 +49,8 @@ discard block |
||
49 | 49 | require_once DOL_DOCUMENT_ROOT."/core/class/html.formcontract.class.php"; |
50 | 50 | require_once DOL_DOCUMENT_ROOT."/contrat/class/contrat.class.php"; |
51 | 51 | } |
52 | -if (getDolGlobalString('FICHEINTER_ADDON') && is_readable(DOL_DOCUMENT_ROOT."/core/modules/fichinter/mod_" . getDolGlobalString('FICHEINTER_ADDON').".php")) { |
|
53 | - require_once DOL_DOCUMENT_ROOT."/core/modules/fichinter/mod_" . getDolGlobalString('FICHEINTER_ADDON').'.php'; |
|
52 | +if (getDolGlobalString('FICHEINTER_ADDON') && is_readable(DOL_DOCUMENT_ROOT."/core/modules/fichinter/mod_".getDolGlobalString('FICHEINTER_ADDON').".php")) { |
|
53 | + require_once DOL_DOCUMENT_ROOT."/core/modules/fichinter/mod_".getDolGlobalString('FICHEINTER_ADDON').'.php'; |
|
54 | 54 | } |
55 | 55 | require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; |
56 | 56 | require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | |
69 | 69 | $id = GETPOSTINT('id'); |
70 | 70 | $ref = GETPOST('ref', 'alpha'); |
71 | -$ref_client = GETPOST('ref_client', 'alpha'); |
|
71 | +$ref_client = GETPOST('ref_client', 'alpha'); |
|
72 | 72 | $socid = GETPOSTINT('socid'); |
73 | 73 | $contratid = GETPOSTINT('contratid'); |
74 | 74 | $action = GETPOST('action', 'alpha'); |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | $result = fichinter_create($db, $object, (!GETPOST('model', 'alpha')) ? $object->model_pdf : GETPOST('model', 'alpha'), $outputlangs); |
231 | 231 | } |
232 | 232 | |
233 | - header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object->id); |
|
233 | + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); |
|
234 | 234 | exit; |
235 | 235 | } else { |
236 | 236 | $mesg = $object->error; |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | $result = fichinter_create($db, $object, (!GETPOST('model', 'alpha')) ? $object->model_pdf : GETPOST('model', 'alpha'), $outputlangs); |
256 | 256 | } |
257 | 257 | |
258 | - header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object->id); |
|
258 | + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); |
|
259 | 259 | exit; |
260 | 260 | } else { |
261 | 261 | $mesg = $object->error; |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | $result = fichinter_create($db, $object, (!GETPOST('model', 'alpha')) ? $object->model_pdf : GETPOST('model', 'alpha'), $outputlangs); |
281 | 281 | } |
282 | 282 | |
283 | - header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object->id); |
|
283 | + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); |
|
284 | 284 | exit; |
285 | 285 | } else { |
286 | 286 | $mesg = $object->error; |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | |
377 | 377 | $classname = ucfirst($subelement); |
378 | 378 | $srcobject = new $classname($db); |
379 | - '@phan-var-force Commande|Propal|Contrat $srcobject'; // Can be other class, but CommonObject is too generic |
|
379 | + '@phan-var-force Commande|Propal|Contrat $srcobject'; // Can be other class, but CommonObject is too generic |
|
380 | 380 | /** @var Commande|Propal|Contrat $srcobject */ |
381 | 381 | |
382 | 382 | dol_syslog("Try to find source object origin=".$object->origin." originid=".$object->origin_id." to add lines"); |
@@ -1136,7 +1136,7 @@ discard block |
||
1136 | 1136 | } else { |
1137 | 1137 | print '<form name="fichinter" action="'.$_SERVER['PHP_SELF'].'" method="POST">'; |
1138 | 1138 | print '<input type="hidden" name="token" value="'.newToken().'">'; |
1139 | - print '<input type="hidden" name="action" value="create">'; // We go back to create action |
|
1139 | + print '<input type="hidden" name="action" value="create">'; // We go back to create action |
|
1140 | 1140 | print '<input type="hidden" name="backtopage" value="'.$backtopage.'">'; |
1141 | 1141 | |
1142 | 1142 | print dol_get_fiche_head([]); |
@@ -1831,9 +1831,9 @@ discard block |
||
1831 | 1831 | // Sign |
1832 | 1832 | if ($object->statut > Fichinter::STATUS_DRAFT) { |
1833 | 1833 | if ($object->signed_status != Fichinter::$SIGNED_STATUSES['STATUS_SIGNED_ALL']) { |
1834 | - print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=sign&token=' . newToken() . '">' . $langs->trans("InterventionSign") . '</a></div>'; |
|
1834 | + print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=sign&token='.newToken().'">'.$langs->trans("InterventionSign").'</a></div>'; |
|
1835 | 1835 | } else { |
1836 | - print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=unsign&token=' . newToken() . '">' . $langs->trans("InterventionUnsign") . '</a></div>'; |
|
1836 | + print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=unsign&token='.newToken().'">'.$langs->trans("InterventionUnsign").'</a></div>'; |
|
1837 | 1837 | } |
1838 | 1838 | } |
1839 | 1839 |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | $company_static->name = $x_coll[$my_coll_thirdpartyid]['company_name'][$id]; |
270 | 270 | $company_static->name_alias = $x_coll[$my_coll_thirdpartyid]['company_alias'][$id]; |
271 | 271 | $company_static->email = $x_coll[$my_coll_thirdpartyid]['company_email'][$id]; |
272 | - $company_static->tva_intra = isset($x_coll[$my_coll_thirdpartyid]['tva_intra'][$id]) ? $x_coll[$my_coll_thirdpartyid]['tva_intra'][$id] : '0'; // @phan-suppress-current-line PhanTypeInvalidDimOffset |
|
272 | + $company_static->tva_intra = isset($x_coll[$my_coll_thirdpartyid]['tva_intra'][$id]) ? $x_coll[$my_coll_thirdpartyid]['tva_intra'][$id] : '0'; // @phan-suppress-current-line PhanTypeInvalidDimOffset |
|
273 | 273 | $company_static->client = $x_coll[$my_coll_thirdpartyid]['company_client'][$id]; |
274 | 274 | $company_static->fournisseur = $x_coll[$my_coll_thirdpartyid]['company_fournisseur'][$id]; |
275 | 275 | $company_static->status = $x_coll[$my_coll_thirdpartyid]['company_status'][$id]; |
@@ -285,8 +285,8 @@ discard block |
||
285 | 285 | 'pid' => $x_coll[$my_coll_thirdpartyid]['pid'][$id], |
286 | 286 | 'pref' => isset($x_coll[$my_coll_thirdpartyid]['pref'][$id]) ? $x_coll[$my_coll_thirdpartyid]['pref'][$id] : '', |
287 | 287 | 'ptype' => $x_coll[$my_coll_thirdpartyid]['ptype'][$id], |
288 | - 'pstatus' => isset($x_paye[$my_coll_thirdpartyid]['pstatus'][$id]) ? $x_paye[$my_coll_thirdpartyid]['pstatus'][$id] : '', // @phan-suppress-current-line PhanTypeInvalidDimOffset |
|
289 | - 'pstatusbuy' => isset($x_paye[$my_coll_thirdpartyid]['pstatusbuy'][$id]) ? $x_paye[$my_coll_thirdpartyid]['pstatusbuy'][$id] : '', // @phan-suppress-current-line PhanTypeInvalidDimOffset |
|
288 | + 'pstatus' => isset($x_paye[$my_coll_thirdpartyid]['pstatus'][$id]) ? $x_paye[$my_coll_thirdpartyid]['pstatus'][$id] : '', // @phan-suppress-current-line PhanTypeInvalidDimOffset |
|
289 | + 'pstatusbuy' => isset($x_paye[$my_coll_thirdpartyid]['pstatusbuy'][$id]) ? $x_paye[$my_coll_thirdpartyid]['pstatusbuy'][$id] : '', // @phan-suppress-current-line PhanTypeInvalidDimOffset |
|
290 | 290 | |
291 | 291 | 'payment_id' => $x_coll[$my_coll_thirdpartyid]['payment_id'][$id], |
292 | 292 | 'payment_ref' => isset($x_coll[$my_coll_thirdpartyid]['payment_ref'][$id]) ? $x_coll[$my_coll_thirdpartyid]['payment_ref'][$id] : '', |
@@ -677,15 +677,15 @@ discard block |
||
677 | 677 | $multicurrency_alreadypayed = price2num($multicurrency_payment + $multicurrency_creditnotes + $multicurrency_deposits, 'MT'); |
678 | 678 | $multicurrency_remaintopay = price2num($invoice->multicurrency_total_ttc - $multicurrency_payment - $multicurrency_creditnotes - $multicurrency_deposits, 'MT'); |
679 | 679 | // Multicurrency full amount tooltip |
680 | - $tooltiponmulticurrencyfullamount = $langs->trans('AmountHT') . ": " . price($objp->multicurrency_total_ht, 0, $langs, 0, -1, -1, $objp->multicurrency_code) . "<br>"; |
|
681 | - $tooltiponmulticurrencyfullamount .= $langs->trans('AmountVAT') . ": " . price($objp->multicurrency_total_tva, 0, $langs, 0, -1, -1, $objp->multicurrency_code) . "<br>"; |
|
682 | - $tooltiponmulticurrencyfullamount .= $langs->trans('AmountTTC') . ": " . price($objp->multicurrency_total_ttc, 0, $langs, 0, -1, -1, $objp->multicurrency_code) . "<br>"; |
|
680 | + $tooltiponmulticurrencyfullamount = $langs->trans('AmountHT').": ".price($objp->multicurrency_total_ht, 0, $langs, 0, -1, -1, $objp->multicurrency_code)."<br>"; |
|
681 | + $tooltiponmulticurrencyfullamount .= $langs->trans('AmountVAT').": ".price($objp->multicurrency_total_tva, 0, $langs, 0, -1, -1, $objp->multicurrency_code)."<br>"; |
|
682 | + $tooltiponmulticurrencyfullamount .= $langs->trans('AmountTTC').": ".price($objp->multicurrency_total_ttc, 0, $langs, 0, -1, -1, $objp->multicurrency_code)."<br>"; |
|
683 | 683 | } |
684 | 684 | |
685 | 685 | // Full amount tooltip |
686 | - $tooltiponfullamount = $langs->trans('AmountHT') . ": " . price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency) . "<br>"; |
|
687 | - $tooltiponfullamount .= $langs->trans('AmountVAT') . ": " . price($objp->total_tva, 0, $langs, 0, -1, -1, $conf->currency) . "<br>"; |
|
688 | - $tooltiponfullamount .= $langs->trans('AmountTTC') . ": " . price($objp->total_ttc, 0, $langs, 0, -1, -1, $conf->currency) . "<br>"; |
|
686 | + $tooltiponfullamount = $langs->trans('AmountHT').": ".price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency)."<br>"; |
|
687 | + $tooltiponfullamount .= $langs->trans('AmountVAT').": ".price($objp->total_tva, 0, $langs, 0, -1, -1, $conf->currency)."<br>"; |
|
688 | + $tooltiponfullamount .= $langs->trans('AmountTTC').": ".price($objp->total_ttc, 0, $langs, 0, -1, -1, $conf->currency)."<br>"; |
|
689 | 689 | |
690 | 690 | print '<tr class="oddeven'.(($invoice->id == $facid) ? ' highlight' : '').'">'; |
691 | 691 | |
@@ -722,7 +722,7 @@ discard block |
||
722 | 722 | if (isModEnabled('multicurrency')) { |
723 | 723 | print '<td class="right">'; |
724 | 724 | if ($objp->multicurrency_code && $objp->multicurrency_code != $conf->currency) { |
725 | - print '<span class="amount classfortooltip" title="'.$tooltiponmulticurrencyfullamount.'">' . price($sign * $objp->multicurrency_total_ttc); |
|
725 | + print '<span class="amount classfortooltip" title="'.$tooltiponmulticurrencyfullamount.'">'.price($sign * $objp->multicurrency_total_ttc); |
|
726 | 726 | } |
727 | 727 | print '</span></td>'; |
728 | 728 |
@@ -209,8 +209,8 @@ |
||
209 | 209 | print '<div class="div-table-responsive">'; |
210 | 210 | print '<table class="liste noborder centpercent">'; |
211 | 211 | |
212 | -$x_coll_sum = 0; // Initialize value |
|
213 | -$x_paye_sum = 0; // Initialize value |
|
212 | +$x_coll_sum = 0; // Initialize value |
|
213 | +$x_paye_sum = 0; // Initialize value |
|
214 | 214 | |
215 | 215 | // IRPF that the customer has retained me |
216 | 216 | if ($calc == 0 || $calc == 2) { |