예제 #1
0
 /**
  * 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']);
 }
예제 #2
0
 /**
  * Format values based on the model's special field types for data insertion into database.
  *
  * @param  array    $values
  * @return array
  */
 public function formatValuesForTypes($values)
 {
     foreach ($this->getFieldTypes() as $field => $type) {
         $value = isset($values[$field]) ? $values[$field] : null;
         $unsetValue = false;
         switch ($type) {
             case "checkbox":
                 $value = !is_null($value) && $value != false;
                 break;
             case "date":
                 $value = !is_null($value) && $value != "" ? date('Y-m-d', strtotime($value)) : null;
                 break;
             case "date-time":
             case "datetime":
             case "timestamp":
                 $value = !is_null($value) && $value != "" ? date('Y-m-d H:i:s', strtotime($value)) : null;
                 break;
             case "date-not-null":
                 $value = !is_null($value) && $value != "" ? date('Y-m-d', strtotime($value)) : "0000-00-00";
                 break;
             case "date-time-not-null":
             case "datetime-not-null":
             case "timestamp-not-null":
                 $value = !is_null($value) && $value != "" ? date('Y-m-d H:i:s', strtotime($value)) : "0000-00-00 00:00:00";
                 break;
             case "slug":
                 $value = Format::slug($value);
                 break;
             case "unique-slug":
                 $value = Format::uniqueSlug($value, $this->table, $field, $this->id);
                 break;
         }
         // set a timestamp based on the checked status of a checkbox
         if (in_array(substr($type, 0, 18), ['checkbox-timestamp', 'checkbox-date-time', 'checkbox-datetime'])) {
             $value = !is_null($value) && $value != false;
             $typeArray = explode(':', $type);
             // set checkbox value in case checkbox field actually exists in model
             $values[$field] = (bool) $value;
             // set field name for timestamp field
             if (count($typeArray) == 1) {
                 $field .= "_at";
             } else {
                 $field = $typeArray[1];
             }
             // set timestamp based on checkbox status and previous timestamp value
             if ($value) {
                 if (is_null($this->{$field})) {
                     $value = date('Y-m-d H:i:s');
                 } else {
                     $unsetValue = true;
                 }
             } else {
                 $value = null;
             }
         }
         if (!$unsetValue) {
             $values[$field] = $value;
         } else {
             if (isset($values[$field])) {
                 unset($values[$field]);
             }
         }
     }
     return $values;
 }
예제 #3
0
 /**
  * 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;
 }
예제 #4
0
 /**
  * Separate a string with new line characters into paragraphs.
  *
  * @param  string  $string
  * @return string
  */
 function nl2p($string)
 {
     return Format::nl2p($string);
 }