1<?php
2
3/*
4	Phoronix Test Suite
5	URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
6	Copyright (C) 2008 - 2020, Phoronix Media
7	Copyright (C) 2008 - 2020, Michael Larabel
8
9	This program is free software; you can redistribute it and/or modify
10	it under the terms of the GNU General Public License as published by
11	the Free Software Foundation; either version 3 of the License, or
12	(at your option) any later version.
13
14	This program is distributed in the hope that it will be useful,
15	but WITHOUT ANY WARRANTY; without even the implied warranty of
16	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17	GNU General Public License for more details.
18
19	You should have received a copy of the GNU General Public License
20	along with this program. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23class pts_arrays
24{
25	public static function first_element($array)
26	{
27		// Using this helper function will avoid a PHP E_STRICT warning if just using the code directly from the output of a function/object
28		return reset($array);
29	}
30	public static function last_element($array)
31	{
32		// Using this helper function will avoid a PHP E_STRICT warning if just using the code directly from the output of a function/object
33		return end($array);
34	}
35	public static function unique_push(&$array, $to_push)
36	{
37		// Only push to the array if it's a unique value
38		return !in_array($to_push, $array) && array_push($array, $to_push);
39	}
40	public static function unique_unshift(&$array, $to_push)
41	{
42		// Only push to the array if it's a unique value
43		return !in_array($to_push, $array) && array_unshift($array, $to_push);
44	}
45	public static function to_array($var)
46	{
47		return !is_array($var) ? array($var) : $var;
48	}
49	public static function json_encode_pretty_string($json)
50	{
51		return str_replace(array(',"', '{', '}'), array(",\n\t\"", " {\n\t", "\n}"), json_encode($json));
52	}
53	public static function json_decode($str)
54	{
55		return json_decode($str, true);
56	}
57	public static function duplicates_in_array($array)
58	{
59		$duplicates = array();
60		foreach(array_count_values($array) as $item => $count)
61		{
62			if($count > 1)
63			{
64				$duplicates[] = $item;
65			}
66		}
67
68		return $duplicates;
69	}
70	public static function array_to_cleansed_item_string($items)
71	{
72		$items_formatted = $items;
73		$items = array();
74
75		for($i = 0; $i < count($items_formatted); $i++)
76		{
77			if(!empty($items_formatted[$i]))
78			{
79				$times_found = 1;
80
81				for($j = ($i + 1); $j < count($items_formatted); $j++)
82				{
83					if(isset($items_formatted[$j]) && $items_formatted[$i] == $items_formatted[$j])
84					{
85						$times_found++;
86						$items_formatted[$j] = '';
87					}
88				}
89				$item = ($times_found > 1 ? $times_found . ' x '  : null) . $items_formatted[$i];
90				array_push($items, $item);
91			}
92		}
93		$items = implode(' + ', $items);
94
95		return $items;
96	}
97	public static function implode_list($r)
98	{
99		$l = null;
100		switch(count($r))
101		{
102			case 0:
103				break;
104			case 1:
105				$l = array_pop($r);
106				break;
107			case 2:
108				$l = implode(' and ', $r);
109				break;
110			default:
111				$l1 = array_pop($r);
112				$l2 = array_pop($r);
113				array_push($r, $l2 . ' and ' . $l1);
114				$l = implode(', ', $r);
115				break;
116		}
117
118		return $l;
119	}
120	public static function natural_krsort(&$array)
121	{
122		$keys = array_keys($array);
123		natsort($keys);
124		$sorted_array = array();
125
126		foreach($keys as $k)
127		{
128			$sorted_array[$k] = $array[$k];
129		}
130
131		$array = array_reverse($sorted_array, true);
132	}
133	//
134	// Popularity Tracking / Most Common Occurences
135	//
136	public static function popularity_tracker(&$popularity_array, $add_to_tracker)
137	{
138		if(!is_array($popularity_array))
139		{
140			$popularity_array = array();
141		}
142		if(empty($add_to_tracker))
143		{
144			return;
145		}
146		foreach($popularity_array as &$el)
147		{
148			if($el['value'] == $add_to_tracker)
149			{
150				$el['popularity']++;
151				return;
152			}
153		}
154		$popularity_array[] = array('value' => $add_to_tracker, 'popularity' => 1);
155	}
156	public static function get_most_popular_from_tracker(&$popularity_array, $ret = 1)
157	{
158		usort($popularity_array, array('pts_arrays', 'compare_popularity'));
159
160		if($ret == 1)
161		{
162			return $popularity_array[0]['value'];
163		}
164		else
165		{
166			$pops = array();
167			for($i = 0; $i < $ret; $i++)
168			{
169				$pops[] = $popularity_array[$i]['value'];
170			}
171			return $pops;
172		}
173	}
174	public static function compare_popularity($a, $b)
175	{
176		$a = $a['popularity'];
177		$b = $b['popularity'];
178
179		if($a == $b)
180		{
181			return 0;
182		}
183
184		return $a > $b ? -1 : 1;
185	}
186}
187
188?>
189