| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This class is responsible for Handling Array. |
| 5 |
*/ |
| 6 |
|
| 7 |
class NotificationX_Array { |
| 8 |
private $limit = 5; |
| 9 |
private $values; |
| 10 |
private $priority = []; |
| 11 |
public $sortBy = 'priority'; |
| 12 |
|
| 13 |
public function setValues( $values ) { |
| 14 |
$this->values = $values; |
| 15 |
$this->sort(); |
| 16 |
} |
| 17 |
|
| 18 |
public function values() { |
| 19 |
$this->values = array_slice( $this->values, 0, $this->limit ); |
| 20 |
return $this->values; |
| 21 |
} |
| 22 |
public function setLimit( $limit = 5 ) { |
| 23 |
$this->limit = $limit; |
| 24 |
} |
| 25 |
|
| 26 |
public function size(){ |
| 27 |
return count( $this->values ); |
| 28 |
} |
| 29 |
|
| 30 |
public function sort( $flags = SORT_DESC ) { |
| 31 |
if( empty( $this->values ) ) { |
| 32 |
return $this; |
| 33 |
} |
| 34 |
foreach( $this->values as $key => $value ) { |
| 35 |
$this->priority[ $key ] = $value[ $this->sortBy ]; |
| 36 |
} |
| 37 |
array_multisort( $this->priority, $flags, $this->values ); |
| 38 |
$this->priority = []; |
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
public function append( $value, $key = null ) { |
| 43 |
if( $this->size() == $this->limit ) { |
| 44 |
$this->sort(); |
| 45 |
array_pop( $this->values ); |
| 46 |
} |
| 47 |
if( $key === null ) { |
| 48 |
array_push( $this->values, $value ); |
| 49 |
} else { |
| 50 |
$this->values = $this->values + [ $key => $value ]; |
| 51 |
} |
| 52 |
$this->sort(); |
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
public function prepend( $value, $key = null ){ |
| 57 |
if( $this->size() == $this->limit ) { |
| 58 |
$this->sort(); |
| 59 |
array_pop( $this->values ); |
| 60 |
} |
| 61 |
|
| 62 |
if( $key === null ) { |
| 63 |
array_unshift( $this->values, $value ); |
| 64 |
} else { |
| 65 |
$this->values = [ $key => $value ] + $this->values; |
| 66 |
} |
| 67 |
$this->sort(); |
| 68 |
return $this; |
| 69 |
} |
| 70 |
} |