| 1 |
<?php |
| 2 |
/*! |
| 3 |
* Hybridauth |
| 4 |
* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth |
| 5 |
* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Hybridauth\Data; |
| 9 |
|
| 10 |
/** |
| 11 |
* A very basic Data collection. |
| 12 |
*/ |
| 13 |
final class Collection |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Data collection |
| 17 |
* |
| 18 |
* @var mixed |
| 19 |
*/ |
| 20 |
protected $collection = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* @param mixed $data |
| 24 |
*/ |
| 25 |
public function __construct($data = null) |
| 26 |
{ |
| 27 |
$this->collection = (object)$data; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Retrieves the whole collection as array |
| 32 |
* |
| 33 |
* @return mixed |
| 34 |
*/ |
| 35 |
public function toArray() |
| 36 |
{ |
| 37 |
return (array)$this->collection; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Retrieves an item |
| 42 |
* |
| 43 |
* @param $property |
| 44 |
* |
| 45 |
* @return mixed |
| 46 |
*/ |
| 47 |
public function get($property) |
| 48 |
{ |
| 49 |
if ($this->exists($property)) { |
| 50 |
return $this->collection->$property; |
| 51 |
} |
| 52 |
|
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Add or update an item |
| 58 |
* |
| 59 |
* @param $property |
| 60 |
* @param mixed $value |
| 61 |
*/ |
| 62 |
public function set($property, $value) |
| 63 |
{ |
| 64 |
if ($property) { |
| 65 |
$this->collection->$property = $value; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* .. until I come with a better name.. |
| 71 |
* |
| 72 |
* @param $property |
| 73 |
* |
| 74 |
* @return Collection |
| 75 |
*/ |
| 76 |
public function filter($property) |
| 77 |
{ |
| 78 |
if ($this->exists($property)) { |
| 79 |
$data = $this->get($property); |
| 80 |
|
| 81 |
if (!is_a($data, 'Collection')) { |
| 82 |
$data = new Collection($data); |
| 83 |
} |
| 84 |
|
| 85 |
return $data; |
| 86 |
} |
| 87 |
|
| 88 |
return new Collection([]); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Checks whether an item within the collection |
| 93 |
* |
| 94 |
* @param $property |
| 95 |
* |
| 96 |
* @return bool |
| 97 |
*/ |
| 98 |
public function exists($property) |
| 99 |
{ |
| 100 |
return property_exists($this->collection, $property); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Finds whether the collection is empty |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
*/ |
| 108 |
public function isEmpty() |
| 109 |
{ |
| 110 |
return !(bool)$this->count(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Count all items in collection |
| 115 |
* |
| 116 |
* @return int |
| 117 |
*/ |
| 118 |
public function count() |
| 119 |
{ |
| 120 |
return count($this->properties()); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Returns all items properties names |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
public function properties() |
| 129 |
{ |
| 130 |
$properties = []; |
| 131 |
|
| 132 |
foreach ($this->collection as $key => $value) { |
| 133 |
$properties[] = $key; |
| 134 |
} |
| 135 |
|
| 136 |
return $properties; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns all items values |
| 141 |
* |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
public function values() |
| 145 |
{ |
| 146 |
$values = []; |
| 147 |
|
| 148 |
foreach ($this->collection as $value) { |
| 149 |
$values[] = $value; |
| 150 |
} |
| 151 |
|
| 152 |
return $values; |
| 153 |
} |
| 154 |
} |
| 155 |
|