/** * Prepare an options array from a set of records * for a select field, checkbox set, or radio button set. * * @param mixed $records * @param mixed $labelValueFields * @return array */ public function prepOptions($records = [], $labelValueFields = []) { $options = []; if (is_string($labelValueFields) || is_array($labelValueFields) && count($labelValueFields) > 0) { foreach ($records as $key => $record) { // turn object into array $recordArray = $record; if (is_object($record)) { if (method_exists($record, 'toArray')) { $recordArray = $record->toArray(); } else { $recordArray = (array) $record; } } // set label and value according to specified variables if (is_string($labelValueFields)) { $label = $labelValueFields; $value = $labelValueFields; } else { if (is_array($labelValueFields) && count($labelValueFields) == 1) { $label = $labelValueFields[0]; $value = $labelValueFields[0]; } else { $label = $labelValueFields[0]; $value = $labelValueFields[1]; } } if (isset($optionValue)) { unset($optionValue); } $method = Format::getMethodFromString($value); if (!is_null($method)) { $optionValue = call_user_func_array([$record, $method['name']], $method['parameters']); } else { if (isset($record[$value])) { $optionValue = $record[$value]; } } // if a label and a value are set, add it to options array if (isset($recordArray[$label]) && isset($optionValue)) { $options[$recordArray[$label]] = $optionValue; } } } return $options; }
/** * Get the result of a method for an object. * * @param object $object * @param string $method * @return mixed */ public function getMethodResult($object, $method) { $method = Format::getMethodFromString($method); if (is_null($method)) { return $method; } return call_user_func_array([$object, $method['name']], $method['parameters']); }
/** * Convert the model's attributes to an array. * * @param mixed $camelizeArrayKeys * @return array */ public function attributesToArray($camelizeArrayKeys = null) { $attributes = $this->getArrayableAttributes($camelizeArrayKeys); $visible = $this->getVisible(); $hidden = $this->getHidden(); // If an attribute is a date, we will cast it to a string after converting it // to a DateTime / Carbon instance. This is so we will get some consistent // formatting while accessing attributes vs. arraying / JSONing a model. foreach ($this->getDates() as $key) { $key = static::formatArrayKey($key, $camelizeArrayKeys); if (!isset($attributes[$key])) { continue; } $attributes[$key] = $this->serializeDate($this->asDateTime($attributes[$key])); } $mutatedAttributes = $this->getMutatedAttributes(); // We want to spin through all the mutated attributes for this model and call // the mutator for the attribute. We cache off every mutated attributes so // we don't have to constantly check on attributes that actually change. foreach ($mutatedAttributes as $key) { $key = static::formatArrayKey($key, $camelizeArrayKeys); if (!array_key_exists($key, $attributes)) { continue; } $attributes[$key] = $this->mutateAttributeForArray($key, $attributes[$key]); } // Next we will handle any casts that have been setup for this model and cast // the values to their appropriate type. If the attribute has a mutator we // will not perform the cast on those attributes to avoid any confusion. foreach ($this->getCasts() as $key => $value) { $key = static::formatArrayKey($key, $camelizeArrayKeys); if (!array_key_exists($key, $attributes) || in_array($key, $mutatedAttributes)) { continue; } $attributes[$key] = $this->castAttribute($key, $attributes[$key]); if ($attributes[$key] && ($value === 'date' || $value === 'datetime')) { $attributes[$key] = $this->serializeDate($attributes[$key]); } } // Here we will grab all of the appended, calculated attributes to this model // as these attributes are not really in the attributes array, but are run // when we need to array or JSON the model for convenience to the coder. foreach ($this->getArrayableAppends() as $key) { $key = static::formatArrayKey($key, $camelizeArrayKeys); $attributes[$key] = $this->mutateAttributeForArray($key, null); } // additionally, we will append the "array-included methods" which allows for more advanced specification foreach ($this->getArrayIncludedMethods() as $key => $includedMethod) { $keyFormatted = static::formatArrayKey($key, $camelizeArrayKeys); if (substr($includedMethod, -1) != ")") { $includedMethod .= "()"; } $method = Format::getMethodFromString($includedMethod); $add = !count($visible) && !count($hidden); if (count($visible) && (in_array($key, $visible) || in_array($keyFormatted, $visible))) { $add = true; } if (count($hidden) && !in_array($key, $visible) && !in_array($keyFormatted, $visible)) { $add = true; } foreach ($method['parameters'] as &$parameter) { if (is_string($parameter)) { if (substr($parameter, 0, 7) == "cached:") { $parameter = $this->getCached(substr($parameter, 7)); } if (substr($parameter, 0, 14) == "static-cached:") { $parameter = static::getStaticCached(substr($parameter, 14)); } } } if ($add) { $attributes[$keyFormatted] = call_user_func_array([$this, $method['name']], $method['parameters']); } } // run through them one more time in case relationships added any attributes that are not allowed foreach ($this->getRelations() as $key => $attribute) { $remove = false; if (count($visible) && !in_array($key, $visible)) { $remove = true; } if (count($hidden) && in_array($key, $visible)) { $remove = true; } if ($remove) { unset($attributes[$key]); } } return $attributes; }