public function store($object_name) { $type = Input::get('type'); $required = Input::has('required') ? 1 : 0; if ($type == 'checkboxes') { self::addJoiningTable($object_name, Input::get('related_object_id')); } else { $field_name = Str::slug(Input::get('title'), '_'); //add _id suffix to foreign key columns (convention, also relationship eg hasOne() conflict) if (in_array($type, ['select', 'image', 'user']) && !Str::endsWith($field_name, '_id')) { $field_name .= '_id'; } //checkboxes can't be 'required' (or it's always required) if ($type == 'checkbox') { $required = false; } //add new column self::addColumn($object_name, $field_name, $type, $required); //set existing default values for required dates to today, better than 0000-00-00 if (in_array($type, ['date', 'datetime']) && $required) { DB::table($object_name)->update([$field_name => new DateTime()]); } } //save field info to fields table $object_id = DB::table(DB_OBJECTS)->where('name', $object_name)->pluck('id'); $field_id = DB::table(DB_FIELDS)->insertGetId(['title' => Input::get('title'), 'name' => $field_name, 'type' => $type, 'object_id' => $object_id, 'visibility' => Input::get('visibility'), 'width' => Input::has('width') ? Input::get('width') : null, 'height' => Input::has('height') ? Input::get('height') : null, 'related_field_id' => Input::has('related_field_id') ? Input::get('related_field_id') : null, 'related_object_id' => Input::has('related_object_id') ? Input::get('related_object_id') : null, 'required' => $required, 'precedence' => DB::table(DB_FIELDS)->where('object_id', $object_id)->max('precedence') + 1, 'updated_by' => Auth::user()->id, 'updated_at' => new DateTime()]); self::organizeTable($object_name); ObjectController::saveSchema(); return Redirect::action('FieldController@index', $object_name)->with('field_id', $field_id); }
/** * Gets the value of an environment variable. Supports boolean, empty and null. * * @param $key string * @param $default mixed * @return mixed */ function env($key, $default = null) { $value = getenv($key); if ($value === false) { return value($default); } switch (strtolower($value)) { case 'true': case '(true)': return true; break; case 'false': case '(false)': return false; break; case 'empty': case '(empty)': return ''; break; case 'null': case '(null)': return; break; default: break; } if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) { return substr($value, 1, -1); } return $value; }
public static function add($url, $attributes = array()) { if (Str::endsWith($url, '.js')) { $url .= '?v=' . rand(0, 999); $asset = \HTML::script($url, $attributes); $nscript = array('url' => $url, 'html' => $asset); $am = Session::get('asset_manager_js', array()); foreach ($am as $script) { if ($url == $script['url']) { return false; } } $am[] = $nscript; Session::put('asset_manager_js', $am); } elseif (Str::endsWith($url, '.css')) { $url .= '?v=' . rand(0, 999); $asset = \HTML::style($url, $attributes); $nstyle = array('url' => $url, 'html' => $asset); $am = Session::get('asset_manager_css', array()); foreach ($am as $style) { if ($url == $style['url']) { return false; } } $am[] = $nstyle; Session::put('asset_manager_css', $am); return true; } }
/** * Determine if a given string ends with a given substring. * * @param string $haystack * @param string|array $needles * @return bool */ function ends_with($haystack, $needles) { return Str::endsWith($haystack, $needles); }
/** * Parse an option expression. * * @param string $token * * @return \Symfony\Component\Console\Input\InputOption */ protected static function parseOption($token) { $description = null; if (Str::contains($token, ' : ')) { list($token, $description) = explode(' : ', $token); $token = trim($token); $description = trim($description); } $shortcut = null; $matches = preg_split('/\\s*\\|\\s*/', $token, 2); if (isset($matches[1])) { $shortcut = $matches[0]; $token = $matches[1]; } switch (true) { case Str::endsWith($token, '='): return new InputOption(trim($token, '='), $shortcut, InputOption::VALUE_OPTIONAL, $description); case Str::endsWith($token, '=*'): return new InputOption(trim($token, '=*'), $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description); case preg_match('/(.+)\\=(.+)/', $token, $matches): return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL, $description, $matches[2]); default: return new InputOption($token, $shortcut, InputOption::VALUE_NONE, $description); } }
/** * @param \Illuminate\Database\Eloquent\Model $model */ protected function getPropertiesFromMethods($model) { $methods = get_class_methods($model); if ($methods) { foreach ($methods as $method) { if (\Str::startsWith($method, 'get') && \Str::endsWith($method, 'Attribute') && $method !== 'setAttribute') { //Magic get<name>Attribute $name = \Str::snake(substr($method, 3, -9)); if (!empty($name)) { $this->setProperty($name, null, true, null); } } elseif (\Str::startsWith($method, 'set') && \Str::endsWith($method, 'Attribute') && $method !== 'setAttribute') { //Magic set<name>Attribute $name = \Str::snake(substr($method, 3, -9)); if (!empty($name)) { $this->setProperty($name, null, null, true); } } elseif (\Str::startsWith($method, 'scope') && $method !== 'scopeQuery') { //Magic set<name>Attribute $name = \Str::camel(substr($method, 5)); if (!empty($name)) { $reflection = new \ReflectionMethod($model, $method); $args = $this->getParameters($reflection); //Remove the first ($query) argument array_shift($args); $this->setMethod($name, $reflection->class, $args); } } elseif (!method_exists('Eloquent', $method) && !\Str::startsWith($method, 'get')) { //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php $reflection = new \ReflectionMethod($model, $method); $file = new \SplFileObject($reflection->getFileName()); $file->seek($reflection->getStartLine() - 1); $code = ''; while ($file->key() < $reflection->getEndLine()) { $code .= $file->current(); $file->next(); } $begin = strpos($code, 'function('); $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); $begin = stripos($code, 'return $this->'); $parts = explode("'", substr($code, $begin + 14), 3); //"return $this->" is 14 chars if (isset($parts[2])) { list($relation, $returnModel, $rest) = $parts; $returnModel = "\\" . ltrim($returnModel, "\\"); $relation = trim($relation, ' ('); if ($relation === "belongsTo" or $relation === 'hasOne') { //Single model is returned $this->setProperty($method, $returnModel, true, null); } elseif ($relation === "belongsToMany" or $relation === 'hasMany') { //Collection or array of models (because Collection is Arrayable) $this->setProperty($method, '\\Illuminate\\Database\\Eloquent\\Collection|' . $returnModel . '[]', true, null); } } else { //Not a relation } } } } }
public function testEndsWithIsFalse() { $this->assertFalse(Str::endsWith('abcdefghij', 'ehh')); }