Example #1
0
 /**
  * @covers \convert\to_array_like
  * @covers \convert\to_array
  * @covers \convert\to_traversable
  * @dataProvider provider_args
  */
 public function test_convert($var, bool $empty_value_to_empty_array, $expected_to_array_like, $expected_to_array, $expected_to_traversable)
 {
     $actual = to_array_like($var, $empty_value_to_empty_array);
     $this->assertEquals($expected_to_array_like, $actual);
     $actual = to_array($var, $empty_value_to_empty_array);
     $this->assertEquals($expected_to_array, $actual);
     $actual = to_traversable($var);
     $this->assertEquals($expected_to_traversable, $actual);
 }
Example #2
0
 /**
  * Calculate statistics for a traversable.
  * 
  * @param array $traversable the traversable
  * @param string $flags_or_comparator either one of SORT_* constants
  * or a comparator function
  */
 public function __construct($traversable, $flags_or_comparator = SORT_REGULAR)
 {
     $values = traversable_sort($flags_or_comparator, array_values(to_array($traversable)));
     if (empty($values)) {
         return;
     }
     $this->count = count($values);
     $this->min = (double) $values[0];
     $this->max = (double) $values[$this->count - 1];
     $this->sum = (double) array_sum($values);
     $this->average = (double) ($this->sum / $this->count);
     $this->count_is_odd = (bool) ($this->count % 2);
     $this->count_is_even = !$this->count_is_odd;
     $median_floor_key = (int) floor($this->count / 2) - 1 + (int) $this->count_is_odd;
     $median_ceil_key = $median_floor_key + (int) $this->count_is_even;
     $this->median_floor = (double) $values[$median_floor_key];
     $this->median_ceil = (double) $values[$median_ceil_key];
     $this->median_average = $this->count_is_odd ? $this->median_floor : (double) (($this->median_floor + $this->median_ceil) / 2);
 }
Example #3
0
 protected function preprocess($value)
 {
     return to_array($value);
 }