@@ -11,171 +11,171 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Populator extends Collection |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Create a new collection. |
|
| 16 | - * |
|
| 17 | - * @param array|Model $items |
|
| 18 | - * |
|
| 19 | - * @return void |
|
| 20 | - */ |
|
| 21 | - public function __construct($items = array()) |
|
| 22 | - { |
|
| 23 | - $this->items = $items; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - //////////////////////////////////////////////////////////////////// |
|
| 27 | - ///////////////////////// INDIVIDUAL VALUES //////////////////////// |
|
| 28 | - //////////////////////////////////////////////////////////////////// |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Get the value of a field |
|
| 32 | - * |
|
| 33 | - * @param string $field The field's name |
|
| 34 | - * |
|
| 35 | - * @return mixed |
|
| 36 | - */ |
|
| 37 | - public function get($field, $fallback = null) |
|
| 38 | - { |
|
| 39 | - // Anonymous fields should not return any value |
|
| 40 | - if ($field == null) { |
|
| 41 | - return null; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - // Plain array |
|
| 45 | - if (is_array($this->items) and !Str::contains($field, '[')) { |
|
| 46 | - return parent::get($field, $fallback); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - // Transform the name into an array |
|
| 50 | - $value = $this->items; |
|
| 51 | - $field = $this->parseFieldAsArray($field); |
|
| 52 | - |
|
| 53 | - // Dive into the model |
|
| 54 | - foreach ($field as $relationship) { |
|
| 55 | - |
|
| 56 | - // Get attribute from model |
|
| 57 | - if (!is_array($value)) { |
|
| 58 | - $value = $this->getAttributeFromModel($value, $relationship, $fallback); |
|
| 59 | - |
|
| 60 | - continue; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - // Get attribute from model |
|
| 64 | - if (array_key_exists($relationship, $value)) { |
|
| 65 | - $value = $value[$relationship]; |
|
| 66 | - } else { |
|
| 67 | - // Check array for submodels that may contain the relationship |
|
| 68 | - $inSubmodel = false; |
|
| 69 | - |
|
| 70 | - foreach ($value as $key => $submodel) { |
|
| 71 | - $value[$key] = $this->getAttributeFromModel($submodel, $relationship, $fallback); |
|
| 72 | - |
|
| 73 | - if ($value[$key] !== $fallback) { |
|
| 74 | - $inSubmodel = true; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // If no submodels contained the relationship, return the fallback, not an array of fallbacks |
|
| 79 | - if (!$inSubmodel) { |
|
| 80 | - $value = $fallback; |
|
| 81 | - break; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return $value; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - //////////////////////////////////////////////////////////////////// |
|
| 90 | - ///////////////////////////// SWAPPERS ///////////////////////////// |
|
| 91 | - //////////////////////////////////////////////////////////////////// |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Replace the items |
|
| 95 | - * |
|
| 96 | - * @param mixed $items |
|
| 97 | - * |
|
| 98 | - * @return void |
|
| 99 | - */ |
|
| 100 | - public function replace($items) |
|
| 101 | - { |
|
| 102 | - $this->items = $items; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Reset the current values array |
|
| 107 | - * |
|
| 108 | - * @return void |
|
| 109 | - */ |
|
| 110 | - public function reset() |
|
| 111 | - { |
|
| 112 | - $this->items = array(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - //////////////////////////////////////////////////////////////////// |
|
| 116 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 117 | - //////////////////////////////////////////////////////////////////// |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Parses the name of a field to a tree of fields |
|
| 121 | - * |
|
| 122 | - * @param string $field The field's name |
|
| 123 | - * |
|
| 124 | - * @return array A tree of field |
|
| 125 | - */ |
|
| 126 | - protected function parseFieldAsArray($field) |
|
| 127 | - { |
|
| 128 | - if (Str::contains($field, '[]')) { |
|
| 129 | - return (array) $field; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // Transform array notation to dot notation |
|
| 133 | - if (Str::contains($field, '[')) { |
|
| 134 | - $field = preg_replace("/[\[\]]/", '.', $field); |
|
| 135 | - $field = str_replace('..', '.', $field); |
|
| 136 | - $field = trim($field, '.'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Parse dot notation |
|
| 140 | - if (Str::contains($field, '.')) { |
|
| 141 | - $field = explode('.', $field); |
|
| 142 | - } else { |
|
| 143 | - $field = (array) $field; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return $field; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get an attribute from a model |
|
| 151 | - * |
|
| 152 | - * @param object $model The model |
|
| 153 | - * @param string $attribute The attribute's name |
|
| 154 | - * @param string $fallback Fallback value |
|
| 155 | - * |
|
| 156 | - * @return mixed |
|
| 157 | - */ |
|
| 158 | - public function getAttributeFromModel($model, $attribute, $fallback) |
|
| 159 | - { |
|
| 160 | - if ($model instanceof Model) { |
|
| 161 | - // Return fallback if attribute is null |
|
| 162 | - $value = $model->getAttribute($attribute); |
|
| 163 | - return is_null($value) ? $fallback : $value; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - if ($model instanceof Collection) { |
|
| 167 | - return $model->get($attribute, $fallback); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - if (is_object($model) && method_exists($model, 'toArray')) { |
|
| 171 | - $model = $model->toArray(); |
|
| 172 | - } else { |
|
| 173 | - $model = (array) $model; |
|
| 174 | - } |
|
| 175 | - if (array_key_exists($attribute, $model)) { |
|
| 176 | - return $model[$attribute]; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $fallback; |
|
| 180 | - } |
|
| 14 | + /** |
|
| 15 | + * Create a new collection. |
|
| 16 | + * |
|
| 17 | + * @param array|Model $items |
|
| 18 | + * |
|
| 19 | + * @return void |
|
| 20 | + */ |
|
| 21 | + public function __construct($items = array()) |
|
| 22 | + { |
|
| 23 | + $this->items = $items; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + //////////////////////////////////////////////////////////////////// |
|
| 27 | + ///////////////////////// INDIVIDUAL VALUES //////////////////////// |
|
| 28 | + //////////////////////////////////////////////////////////////////// |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Get the value of a field |
|
| 32 | + * |
|
| 33 | + * @param string $field The field's name |
|
| 34 | + * |
|
| 35 | + * @return mixed |
|
| 36 | + */ |
|
| 37 | + public function get($field, $fallback = null) |
|
| 38 | + { |
|
| 39 | + // Anonymous fields should not return any value |
|
| 40 | + if ($field == null) { |
|
| 41 | + return null; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + // Plain array |
|
| 45 | + if (is_array($this->items) and !Str::contains($field, '[')) { |
|
| 46 | + return parent::get($field, $fallback); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + // Transform the name into an array |
|
| 50 | + $value = $this->items; |
|
| 51 | + $field = $this->parseFieldAsArray($field); |
|
| 52 | + |
|
| 53 | + // Dive into the model |
|
| 54 | + foreach ($field as $relationship) { |
|
| 55 | + |
|
| 56 | + // Get attribute from model |
|
| 57 | + if (!is_array($value)) { |
|
| 58 | + $value = $this->getAttributeFromModel($value, $relationship, $fallback); |
|
| 59 | + |
|
| 60 | + continue; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + // Get attribute from model |
|
| 64 | + if (array_key_exists($relationship, $value)) { |
|
| 65 | + $value = $value[$relationship]; |
|
| 66 | + } else { |
|
| 67 | + // Check array for submodels that may contain the relationship |
|
| 68 | + $inSubmodel = false; |
|
| 69 | + |
|
| 70 | + foreach ($value as $key => $submodel) { |
|
| 71 | + $value[$key] = $this->getAttributeFromModel($submodel, $relationship, $fallback); |
|
| 72 | + |
|
| 73 | + if ($value[$key] !== $fallback) { |
|
| 74 | + $inSubmodel = true; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // If no submodels contained the relationship, return the fallback, not an array of fallbacks |
|
| 79 | + if (!$inSubmodel) { |
|
| 80 | + $value = $fallback; |
|
| 81 | + break; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return $value; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + //////////////////////////////////////////////////////////////////// |
|
| 90 | + ///////////////////////////// SWAPPERS ///////////////////////////// |
|
| 91 | + //////////////////////////////////////////////////////////////////// |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Replace the items |
|
| 95 | + * |
|
| 96 | + * @param mixed $items |
|
| 97 | + * |
|
| 98 | + * @return void |
|
| 99 | + */ |
|
| 100 | + public function replace($items) |
|
| 101 | + { |
|
| 102 | + $this->items = $items; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Reset the current values array |
|
| 107 | + * |
|
| 108 | + * @return void |
|
| 109 | + */ |
|
| 110 | + public function reset() |
|
| 111 | + { |
|
| 112 | + $this->items = array(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + //////////////////////////////////////////////////////////////////// |
|
| 116 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 117 | + //////////////////////////////////////////////////////////////////// |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Parses the name of a field to a tree of fields |
|
| 121 | + * |
|
| 122 | + * @param string $field The field's name |
|
| 123 | + * |
|
| 124 | + * @return array A tree of field |
|
| 125 | + */ |
|
| 126 | + protected function parseFieldAsArray($field) |
|
| 127 | + { |
|
| 128 | + if (Str::contains($field, '[]')) { |
|
| 129 | + return (array) $field; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // Transform array notation to dot notation |
|
| 133 | + if (Str::contains($field, '[')) { |
|
| 134 | + $field = preg_replace("/[\[\]]/", '.', $field); |
|
| 135 | + $field = str_replace('..', '.', $field); |
|
| 136 | + $field = trim($field, '.'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Parse dot notation |
|
| 140 | + if (Str::contains($field, '.')) { |
|
| 141 | + $field = explode('.', $field); |
|
| 142 | + } else { |
|
| 143 | + $field = (array) $field; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return $field; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get an attribute from a model |
|
| 151 | + * |
|
| 152 | + * @param object $model The model |
|
| 153 | + * @param string $attribute The attribute's name |
|
| 154 | + * @param string $fallback Fallback value |
|
| 155 | + * |
|
| 156 | + * @return mixed |
|
| 157 | + */ |
|
| 158 | + public function getAttributeFromModel($model, $attribute, $fallback) |
|
| 159 | + { |
|
| 160 | + if ($model instanceof Model) { |
|
| 161 | + // Return fallback if attribute is null |
|
| 162 | + $value = $model->getAttribute($attribute); |
|
| 163 | + return is_null($value) ? $fallback : $value; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + if ($model instanceof Collection) { |
|
| 167 | + return $model->get($attribute, $fallback); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + if (is_object($model) && method_exists($model, 'toArray')) { |
|
| 171 | + $model = $model->toArray(); |
|
| 172 | + } else { |
|
| 173 | + $model = (array) $model; |
|
| 174 | + } |
|
| 175 | + if (array_key_exists($attribute, $model)) { |
|
| 176 | + return $model[$attribute]; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $fallback; |
|
| 180 | + } |
|
| 181 | 181 | } |