@@ -10,146 +10,146 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Input extends Field |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Current datalist stored |
|
| 15 | - * |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $datalist = array(); |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Properties to be injected as attributes |
|
| 22 | - * |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - protected $injectedProperties = array('type', 'name', 'value'); |
|
| 26 | - |
|
| 27 | - //////////////////////////////////////////////////////////////////// |
|
| 28 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 29 | - //////////////////////////////////////////////////////////////////// |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Build an input field |
|
| 33 | - * |
|
| 34 | - * @param Container $app The Container |
|
| 35 | - * @param string $type The input type |
|
| 36 | - * @param string $name Field name |
|
| 37 | - * @param string $label Its label |
|
| 38 | - * @param string $value Its value |
|
| 39 | - * @param array $attributes Attributes |
|
| 40 | - */ |
|
| 41 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 42 | - { |
|
| 43 | - parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 44 | - |
|
| 45 | - // Multiple models population |
|
| 46 | - if (is_array($this->value)) { |
|
| 47 | - $values = array(); |
|
| 48 | - foreach ($this->value as $value) { |
|
| 49 | - $values[] = is_object($value) ? $value->__toString() : $value; |
|
| 50 | - } |
|
| 51 | - if (isset($values)) { |
|
| 52 | - $this->value = implode(', ', $values); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Prints out the current tag |
|
| 59 | - * |
|
| 60 | - * @return string An input tag |
|
| 61 | - */ |
|
| 62 | - public function render() |
|
| 63 | - { |
|
| 64 | - // Particular case of the search element |
|
| 65 | - if ($this->isOfType('search')) { |
|
| 66 | - $this->asSearch(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $this->setId(); |
|
| 70 | - |
|
| 71 | - // Render main input |
|
| 72 | - $input = parent::render(); |
|
| 73 | - |
|
| 74 | - // If we have a datalist to append, print it out |
|
| 75 | - if ($this->datalist) { |
|
| 76 | - $input .= $this->createDatalist($this->list, $this->datalist); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return $input; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - //////////////////////////////////////////////////////////////////// |
|
| 83 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 84 | - //////////////////////////////////////////////////////////////////// |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Adds a datalist to the current field |
|
| 88 | - * |
|
| 89 | - * @param array $datalist An array to use a source |
|
| 90 | - * @param string $value The field to use as value |
|
| 91 | - * @param string $key The field to use as key |
|
| 92 | - */ |
|
| 93 | - public function useDatalist($datalist, $value = null, $key = null) |
|
| 94 | - { |
|
| 95 | - $datalist = Helpers::queryToArray($datalist, $value, $key); |
|
| 96 | - |
|
| 97 | - $list = $this->list ?: 'datalist_'.$this->name; |
|
| 98 | - |
|
| 99 | - // Create the link to the datalist |
|
| 100 | - $this->list = $list; |
|
| 101 | - $this->datalist = $datalist; |
|
| 102 | - |
|
| 103 | - return $this; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Add range to the input |
|
| 108 | - * |
|
| 109 | - * @param integer $min |
|
| 110 | - * @param integer $max |
|
| 111 | - * |
|
| 112 | - * @return self |
|
| 113 | - */ |
|
| 114 | - public function range($min, $max) |
|
| 115 | - { |
|
| 116 | - $this->min($min); |
|
| 117 | - $this->max($max); |
|
| 118 | - |
|
| 119 | - return $this; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - //////////////////////////////////////////////////////////////////// |
|
| 123 | - /////////////////////////////// HELPERS //////////////////////////// |
|
| 124 | - //////////////////////////////////////////////////////////////////// |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Render a text element as a search element |
|
| 128 | - */ |
|
| 129 | - private function asSearch() |
|
| 130 | - { |
|
| 131 | - $this->type = 'text'; |
|
| 132 | - $this->addClass('search-query'); |
|
| 133 | - |
|
| 134 | - return $this; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Renders a datalist |
|
| 139 | - * |
|
| 140 | - * @param string $id The datalist's id attribute |
|
| 141 | - * @param array $values Its values |
|
| 142 | - * |
|
| 143 | - * @return string A <datalist> tag |
|
| 144 | - */ |
|
| 145 | - private function createDatalist($id, $values) |
|
| 146 | - { |
|
| 147 | - $datalist = '<datalist id="'.$id.'">'; |
|
| 148 | - foreach ($values as $key => $value) { |
|
| 149 | - $datalist .= '<option value="'.$value.'">'.$key.'</option>'; |
|
| 150 | - } |
|
| 151 | - $datalist .= '</datalist>'; |
|
| 152 | - |
|
| 153 | - return $datalist; |
|
| 154 | - } |
|
| 13 | + /** |
|
| 14 | + * Current datalist stored |
|
| 15 | + * |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $datalist = array(); |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Properties to be injected as attributes |
|
| 22 | + * |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + protected $injectedProperties = array('type', 'name', 'value'); |
|
| 26 | + |
|
| 27 | + //////////////////////////////////////////////////////////////////// |
|
| 28 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 29 | + //////////////////////////////////////////////////////////////////// |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Build an input field |
|
| 33 | + * |
|
| 34 | + * @param Container $app The Container |
|
| 35 | + * @param string $type The input type |
|
| 36 | + * @param string $name Field name |
|
| 37 | + * @param string $label Its label |
|
| 38 | + * @param string $value Its value |
|
| 39 | + * @param array $attributes Attributes |
|
| 40 | + */ |
|
| 41 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 42 | + { |
|
| 43 | + parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 44 | + |
|
| 45 | + // Multiple models population |
|
| 46 | + if (is_array($this->value)) { |
|
| 47 | + $values = array(); |
|
| 48 | + foreach ($this->value as $value) { |
|
| 49 | + $values[] = is_object($value) ? $value->__toString() : $value; |
|
| 50 | + } |
|
| 51 | + if (isset($values)) { |
|
| 52 | + $this->value = implode(', ', $values); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Prints out the current tag |
|
| 59 | + * |
|
| 60 | + * @return string An input tag |
|
| 61 | + */ |
|
| 62 | + public function render() |
|
| 63 | + { |
|
| 64 | + // Particular case of the search element |
|
| 65 | + if ($this->isOfType('search')) { |
|
| 66 | + $this->asSearch(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $this->setId(); |
|
| 70 | + |
|
| 71 | + // Render main input |
|
| 72 | + $input = parent::render(); |
|
| 73 | + |
|
| 74 | + // If we have a datalist to append, print it out |
|
| 75 | + if ($this->datalist) { |
|
| 76 | + $input .= $this->createDatalist($this->list, $this->datalist); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return $input; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + //////////////////////////////////////////////////////////////////// |
|
| 83 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 84 | + //////////////////////////////////////////////////////////////////// |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Adds a datalist to the current field |
|
| 88 | + * |
|
| 89 | + * @param array $datalist An array to use a source |
|
| 90 | + * @param string $value The field to use as value |
|
| 91 | + * @param string $key The field to use as key |
|
| 92 | + */ |
|
| 93 | + public function useDatalist($datalist, $value = null, $key = null) |
|
| 94 | + { |
|
| 95 | + $datalist = Helpers::queryToArray($datalist, $value, $key); |
|
| 96 | + |
|
| 97 | + $list = $this->list ?: 'datalist_'.$this->name; |
|
| 98 | + |
|
| 99 | + // Create the link to the datalist |
|
| 100 | + $this->list = $list; |
|
| 101 | + $this->datalist = $datalist; |
|
| 102 | + |
|
| 103 | + return $this; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Add range to the input |
|
| 108 | + * |
|
| 109 | + * @param integer $min |
|
| 110 | + * @param integer $max |
|
| 111 | + * |
|
| 112 | + * @return self |
|
| 113 | + */ |
|
| 114 | + public function range($min, $max) |
|
| 115 | + { |
|
| 116 | + $this->min($min); |
|
| 117 | + $this->max($max); |
|
| 118 | + |
|
| 119 | + return $this; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + //////////////////////////////////////////////////////////////////// |
|
| 123 | + /////////////////////////////// HELPERS //////////////////////////// |
|
| 124 | + //////////////////////////////////////////////////////////////////// |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Render a text element as a search element |
|
| 128 | + */ |
|
| 129 | + private function asSearch() |
|
| 130 | + { |
|
| 131 | + $this->type = 'text'; |
|
| 132 | + $this->addClass('search-query'); |
|
| 133 | + |
|
| 134 | + return $this; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Renders a datalist |
|
| 139 | + * |
|
| 140 | + * @param string $id The datalist's id attribute |
|
| 141 | + * @param array $values Its values |
|
| 142 | + * |
|
| 143 | + * @return string A <datalist> tag |
|
| 144 | + */ |
|
| 145 | + private function createDatalist($id, $values) |
|
| 146 | + { |
|
| 147 | + $datalist = '<datalist id="'.$id.'">'; |
|
| 148 | + foreach ($values as $key => $value) { |
|
| 149 | + $datalist .= '<option value="'.$value.'">'.$key.'</option>'; |
|
| 150 | + } |
|
| 151 | + $datalist .= '</datalist>'; |
|
| 152 | + |
|
| 153 | + return $datalist; |
|
| 154 | + } |
|
| 155 | 155 | } |
@@ -9,21 +9,21 @@ |
||
| 9 | 9 | class Uneditable extends Field |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - //////////////////////////////////////////////////////////////////// |
|
| 13 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 14 | - //////////////////////////////////////////////////////////////////// |
|
| 12 | + //////////////////////////////////////////////////////////////////// |
|
| 13 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 14 | + //////////////////////////////////////////////////////////////////// |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Prints out the current tag |
|
| 18 | - * |
|
| 19 | - * @return string An uneditable input tag |
|
| 20 | - */ |
|
| 21 | - public function render() |
|
| 22 | - { |
|
| 23 | - $this->addClass($this->app['former.framework']->getUneditableClasses()); |
|
| 16 | + /** |
|
| 17 | + * Prints out the current tag |
|
| 18 | + * |
|
| 19 | + * @return string An uneditable input tag |
|
| 20 | + */ |
|
| 21 | + public function render() |
|
| 22 | + { |
|
| 23 | + $this->addClass($this->app['former.framework']->getUneditableClasses()); |
|
| 24 | 24 | |
| 25 | - $this->setId(); |
|
| 25 | + $this->setId(); |
|
| 26 | 26 | |
| 27 | - return $this->app['former.framework']->createDisabledField($this); |
|
| 28 | - } |
|
| 27 | + return $this->app['former.framework']->createDisabledField($this); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -8,17 +8,17 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Textarea extends Field |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The textarea's element |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $element = 'textarea'; |
|
| 11 | + /** |
|
| 12 | + * The textarea's element |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $element = 'textarea'; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The textarea's self-closing state |
|
| 20 | - * |
|
| 21 | - * @var boolean |
|
| 22 | - */ |
|
| 23 | - protected $isSelfClosing = false; |
|
| 18 | + /** |
|
| 19 | + * The textarea's self-closing state |
|
| 20 | + * |
|
| 21 | + * @var boolean |
|
| 22 | + */ |
|
| 23 | + protected $isSelfClosing = false; |
|
| 24 | 24 | } |
@@ -9,17 +9,17 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Former extends Facade |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Get the registered component. |
|
| 14 | - * |
|
| 15 | - * @return string |
|
| 16 | - */ |
|
| 17 | - protected static function getFacadeAccessor() |
|
| 18 | - { |
|
| 19 | - if (!static::$app) { |
|
| 20 | - static::$app = FormerServiceProvider::make(); |
|
| 21 | - } |
|
| 12 | + /** |
|
| 13 | + * Get the registered component. |
|
| 14 | + * |
|
| 15 | + * @return string |
|
| 16 | + */ |
|
| 17 | + protected static function getFacadeAccessor() |
|
| 18 | + { |
|
| 19 | + if (!static::$app) { |
|
| 20 | + static::$app = FormerServiceProvider::make(); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - return 'former'; |
|
| 24 | - } |
|
| 23 | + return 'former'; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -11,239 +11,239 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class MethodDispatcher |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * The IoC Container |
|
| 16 | - * |
|
| 17 | - * @var Container |
|
| 18 | - */ |
|
| 19 | - protected $app; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * An array of fields repositories |
|
| 23 | - * |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - protected $repositories = array(); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Build a new Dispatcher |
|
| 30 | - * |
|
| 31 | - * @param Container $app |
|
| 32 | - * @param array $repositories |
|
| 33 | - */ |
|
| 34 | - public function __construct(Container $app, $repositories) |
|
| 35 | - { |
|
| 36 | - $this->app = $app; |
|
| 37 | - $this->repositories = (array) $repositories; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - //////////////////////////////////////////////////////////////////// |
|
| 41 | - ///////////////////////////// REPOSITORIES ///////////////////////// |
|
| 42 | - //////////////////////////////////////////////////////////////////// |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Add a fields repository |
|
| 46 | - * |
|
| 47 | - * @param string $repository |
|
| 48 | - * |
|
| 49 | - * @return $this |
|
| 50 | - */ |
|
| 51 | - public function addRepository($repository) |
|
| 52 | - { |
|
| 53 | - array_unshift($this->repositories, $repository); |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - //////////////////////////////////////////////////////////////////// |
|
| 59 | - ///////////////////////////// DISPATCHERS ////////////////////////// |
|
| 60 | - //////////////////////////////////////////////////////////////////// |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Dispatch a call to a registered macro |
|
| 64 | - * |
|
| 65 | - * @param string $method The macro's name |
|
| 66 | - * @param array $parameters The macro's arguments |
|
| 67 | - * |
|
| 68 | - * @return mixed |
|
| 69 | - */ |
|
| 70 | - public function toMacros($method, $parameters) |
|
| 71 | - { |
|
| 72 | - if (!$this->app['former']->hasMacro($method)) { |
|
| 73 | - return false; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // Get and format macro |
|
| 77 | - $callback = $this->app['former']->getMacro($method); |
|
| 78 | - if ($callback instanceof Closure) { |
|
| 79 | - return call_user_func_array($callback, $parameters); |
|
| 80 | - } // Cancel if the macro is invalid |
|
| 81 | - elseif (!is_string($callback)) { |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // Get class and method |
|
| 86 | - list($class, $method) = explode('@', $callback); |
|
| 87 | - $this->app->instance('Illuminate\Container\Container', $this->app); |
|
| 88 | - |
|
| 89 | - return call_user_func_array(array($this->app->make($class), $method), $parameters); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Dispatch a call over to Elements |
|
| 94 | - * |
|
| 95 | - * @param string $method The method called |
|
| 96 | - * @param array $parameters Its parameters |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function toElements($method, $parameters) |
|
| 101 | - { |
|
| 102 | - // Disregards if the method isn't an element |
|
| 103 | - if (!method_exists($elements = new Form\Elements($this->app, $this->app['session']), $method)) { |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return call_user_func_array(array($elements, $method), $parameters); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Dispatch a call over to Form |
|
| 112 | - * |
|
| 113 | - * @param string $method The method called |
|
| 114 | - * @param array $parameters Its parameters |
|
| 115 | - * |
|
| 116 | - * @return Form |
|
| 117 | - */ |
|
| 118 | - public function toForm($method, $parameters) |
|
| 119 | - { |
|
| 120 | - // Disregards if the method doesn't contain 'open' |
|
| 121 | - if (!Str::contains($method, 'open') and !Str::contains($method, 'Open')) { |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $form = new Form\Form($this->app, $this->app['url'], $this->app['former.populator']); |
|
| 126 | - |
|
| 127 | - return $form->openForm($method, $parameters); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Dispatch a call over to Group |
|
| 132 | - * |
|
| 133 | - * @param string $method The method called |
|
| 134 | - * @param array $parameters Its parameters |
|
| 135 | - * |
|
| 136 | - * @return Group |
|
| 137 | - */ |
|
| 138 | - public function toGroup($method, $parameters) |
|
| 139 | - { |
|
| 140 | - // Disregards if the method isn't "group" |
|
| 141 | - if ($method != 'group') { |
|
| 142 | - return false; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // Create opener |
|
| 146 | - $group = new Form\Group( |
|
| 147 | - $this->app, |
|
| 148 | - array_get($parameters, 0, null), |
|
| 149 | - array_get($parameters, 1, null) |
|
| 150 | - ); |
|
| 151 | - |
|
| 152 | - // Set custom group as true |
|
| 153 | - Form\Group::$opened = true; |
|
| 154 | - |
|
| 155 | - // Set custom group reference |
|
| 156 | - Form\Group::$openGroup = $group; |
|
| 157 | - |
|
| 158 | - return $group; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Dispatch a call over to Actions |
|
| 163 | - * |
|
| 164 | - * @param string $method The method called |
|
| 165 | - * @param array $parameters Its parameters |
|
| 166 | - * |
|
| 167 | - * @return Actions |
|
| 168 | - */ |
|
| 169 | - public function toActions($method, $parameters) |
|
| 170 | - { |
|
| 171 | - if ($method != 'actions') { |
|
| 172 | - return false; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - return new Form\Actions($this->app, $parameters); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Dispatch a call over to the Fields |
|
| 180 | - * |
|
| 181 | - * @param string $method The method called |
|
| 182 | - * @param array $parameters Its parameters |
|
| 183 | - * |
|
| 184 | - * @return Field |
|
| 185 | - */ |
|
| 186 | - public function toFields($method, $parameters) |
|
| 187 | - { |
|
| 188 | - // Listing parameters |
|
| 189 | - $class = $this->getClassFromMethod($method); |
|
| 190 | - $field = new $class( |
|
| 191 | - $this->app, |
|
| 192 | - $method, |
|
| 193 | - array_get($parameters, 0), |
|
| 194 | - array_get($parameters, 1), |
|
| 195 | - array_get($parameters, 2), |
|
| 196 | - array_get($parameters, 3), |
|
| 197 | - array_get($parameters, 4), |
|
| 198 | - array_get($parameters, 5) |
|
| 199 | - ); |
|
| 200 | - |
|
| 201 | - return $field; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - //////////////////////////////////////////////////////////////////// |
|
| 205 | - ///////////////////////////// HELPERS ////////////////////////////// |
|
| 206 | - //////////////////////////////////////////////////////////////////// |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Get the correct class to call according to the created field |
|
| 210 | - * |
|
| 211 | - * @param string $method The field created |
|
| 212 | - * |
|
| 213 | - * @return string The correct class |
|
| 214 | - */ |
|
| 215 | - protected function getClassFromMethod($method) |
|
| 216 | - { |
|
| 217 | - // If the field's name directly match a class, call it |
|
| 218 | - $class = Str::singular(Str::title($method)); |
|
| 219 | - $studly_class = Str::singular(Str::studly($method)); |
|
| 220 | - foreach ($this->repositories as $repository) { |
|
| 221 | - if (class_exists($repository.$studly_class)) { |
|
| 222 | - return $repository.$studly_class; |
|
| 223 | - } else { |
|
| 224 | - if (class_exists($repository.$class)) { |
|
| 225 | - return $repository.$class; |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - // Else convert known fields to their classes |
|
| 231 | - switch ($method) { |
|
| 232 | - case 'submit': |
|
| 233 | - case 'link': |
|
| 234 | - case 'reset': |
|
| 235 | - $class = Former::FIELDSPACE.'Button'; |
|
| 236 | - break; |
|
| 237 | - |
|
| 238 | - case 'multiselect': |
|
| 239 | - $class = Former::FIELDSPACE.'Select'; |
|
| 240 | - break; |
|
| 241 | - |
|
| 242 | - default: |
|
| 243 | - $class = Former::FIELDSPACE.'Input'; |
|
| 244 | - break; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - return $class; |
|
| 248 | - } |
|
| 14 | + /** |
|
| 15 | + * The IoC Container |
|
| 16 | + * |
|
| 17 | + * @var Container |
|
| 18 | + */ |
|
| 19 | + protected $app; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * An array of fields repositories |
|
| 23 | + * |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + protected $repositories = array(); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Build a new Dispatcher |
|
| 30 | + * |
|
| 31 | + * @param Container $app |
|
| 32 | + * @param array $repositories |
|
| 33 | + */ |
|
| 34 | + public function __construct(Container $app, $repositories) |
|
| 35 | + { |
|
| 36 | + $this->app = $app; |
|
| 37 | + $this->repositories = (array) $repositories; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + //////////////////////////////////////////////////////////////////// |
|
| 41 | + ///////////////////////////// REPOSITORIES ///////////////////////// |
|
| 42 | + //////////////////////////////////////////////////////////////////// |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Add a fields repository |
|
| 46 | + * |
|
| 47 | + * @param string $repository |
|
| 48 | + * |
|
| 49 | + * @return $this |
|
| 50 | + */ |
|
| 51 | + public function addRepository($repository) |
|
| 52 | + { |
|
| 53 | + array_unshift($this->repositories, $repository); |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + //////////////////////////////////////////////////////////////////// |
|
| 59 | + ///////////////////////////// DISPATCHERS ////////////////////////// |
|
| 60 | + //////////////////////////////////////////////////////////////////// |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Dispatch a call to a registered macro |
|
| 64 | + * |
|
| 65 | + * @param string $method The macro's name |
|
| 66 | + * @param array $parameters The macro's arguments |
|
| 67 | + * |
|
| 68 | + * @return mixed |
|
| 69 | + */ |
|
| 70 | + public function toMacros($method, $parameters) |
|
| 71 | + { |
|
| 72 | + if (!$this->app['former']->hasMacro($method)) { |
|
| 73 | + return false; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // Get and format macro |
|
| 77 | + $callback = $this->app['former']->getMacro($method); |
|
| 78 | + if ($callback instanceof Closure) { |
|
| 79 | + return call_user_func_array($callback, $parameters); |
|
| 80 | + } // Cancel if the macro is invalid |
|
| 81 | + elseif (!is_string($callback)) { |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // Get class and method |
|
| 86 | + list($class, $method) = explode('@', $callback); |
|
| 87 | + $this->app->instance('Illuminate\Container\Container', $this->app); |
|
| 88 | + |
|
| 89 | + return call_user_func_array(array($this->app->make($class), $method), $parameters); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Dispatch a call over to Elements |
|
| 94 | + * |
|
| 95 | + * @param string $method The method called |
|
| 96 | + * @param array $parameters Its parameters |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function toElements($method, $parameters) |
|
| 101 | + { |
|
| 102 | + // Disregards if the method isn't an element |
|
| 103 | + if (!method_exists($elements = new Form\Elements($this->app, $this->app['session']), $method)) { |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return call_user_func_array(array($elements, $method), $parameters); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Dispatch a call over to Form |
|
| 112 | + * |
|
| 113 | + * @param string $method The method called |
|
| 114 | + * @param array $parameters Its parameters |
|
| 115 | + * |
|
| 116 | + * @return Form |
|
| 117 | + */ |
|
| 118 | + public function toForm($method, $parameters) |
|
| 119 | + { |
|
| 120 | + // Disregards if the method doesn't contain 'open' |
|
| 121 | + if (!Str::contains($method, 'open') and !Str::contains($method, 'Open')) { |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $form = new Form\Form($this->app, $this->app['url'], $this->app['former.populator']); |
|
| 126 | + |
|
| 127 | + return $form->openForm($method, $parameters); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Dispatch a call over to Group |
|
| 132 | + * |
|
| 133 | + * @param string $method The method called |
|
| 134 | + * @param array $parameters Its parameters |
|
| 135 | + * |
|
| 136 | + * @return Group |
|
| 137 | + */ |
|
| 138 | + public function toGroup($method, $parameters) |
|
| 139 | + { |
|
| 140 | + // Disregards if the method isn't "group" |
|
| 141 | + if ($method != 'group') { |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // Create opener |
|
| 146 | + $group = new Form\Group( |
|
| 147 | + $this->app, |
|
| 148 | + array_get($parameters, 0, null), |
|
| 149 | + array_get($parameters, 1, null) |
|
| 150 | + ); |
|
| 151 | + |
|
| 152 | + // Set custom group as true |
|
| 153 | + Form\Group::$opened = true; |
|
| 154 | + |
|
| 155 | + // Set custom group reference |
|
| 156 | + Form\Group::$openGroup = $group; |
|
| 157 | + |
|
| 158 | + return $group; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Dispatch a call over to Actions |
|
| 163 | + * |
|
| 164 | + * @param string $method The method called |
|
| 165 | + * @param array $parameters Its parameters |
|
| 166 | + * |
|
| 167 | + * @return Actions |
|
| 168 | + */ |
|
| 169 | + public function toActions($method, $parameters) |
|
| 170 | + { |
|
| 171 | + if ($method != 'actions') { |
|
| 172 | + return false; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + return new Form\Actions($this->app, $parameters); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Dispatch a call over to the Fields |
|
| 180 | + * |
|
| 181 | + * @param string $method The method called |
|
| 182 | + * @param array $parameters Its parameters |
|
| 183 | + * |
|
| 184 | + * @return Field |
|
| 185 | + */ |
|
| 186 | + public function toFields($method, $parameters) |
|
| 187 | + { |
|
| 188 | + // Listing parameters |
|
| 189 | + $class = $this->getClassFromMethod($method); |
|
| 190 | + $field = new $class( |
|
| 191 | + $this->app, |
|
| 192 | + $method, |
|
| 193 | + array_get($parameters, 0), |
|
| 194 | + array_get($parameters, 1), |
|
| 195 | + array_get($parameters, 2), |
|
| 196 | + array_get($parameters, 3), |
|
| 197 | + array_get($parameters, 4), |
|
| 198 | + array_get($parameters, 5) |
|
| 199 | + ); |
|
| 200 | + |
|
| 201 | + return $field; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + //////////////////////////////////////////////////////////////////// |
|
| 205 | + ///////////////////////////// HELPERS ////////////////////////////// |
|
| 206 | + //////////////////////////////////////////////////////////////////// |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Get the correct class to call according to the created field |
|
| 210 | + * |
|
| 211 | + * @param string $method The field created |
|
| 212 | + * |
|
| 213 | + * @return string The correct class |
|
| 214 | + */ |
|
| 215 | + protected function getClassFromMethod($method) |
|
| 216 | + { |
|
| 217 | + // If the field's name directly match a class, call it |
|
| 218 | + $class = Str::singular(Str::title($method)); |
|
| 219 | + $studly_class = Str::singular(Str::studly($method)); |
|
| 220 | + foreach ($this->repositories as $repository) { |
|
| 221 | + if (class_exists($repository.$studly_class)) { |
|
| 222 | + return $repository.$studly_class; |
|
| 223 | + } else { |
|
| 224 | + if (class_exists($repository.$class)) { |
|
| 225 | + return $repository.$class; |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + // Else convert known fields to their classes |
|
| 231 | + switch ($method) { |
|
| 232 | + case 'submit': |
|
| 233 | + case 'link': |
|
| 234 | + case 'reset': |
|
| 235 | + $class = Former::FIELDSPACE.'Button'; |
|
| 236 | + break; |
|
| 237 | + |
|
| 238 | + case 'multiselect': |
|
| 239 | + $class = Former::FIELDSPACE.'Select'; |
|
| 240 | + break; |
|
| 241 | + |
|
| 242 | + default: |
|
| 243 | + $class = Former::FIELDSPACE.'Input'; |
|
| 244 | + break; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + return $class; |
|
| 248 | + } |
|
| 249 | 249 | } |
@@ -8,141 +8,141 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | interface FrameworkInterface |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Filter buttons classes |
|
| 13 | - * |
|
| 14 | - * @param array $classes An array of classes |
|
| 15 | - * |
|
| 16 | - * @return array A filtered array |
|
| 17 | - */ |
|
| 18 | - public function filterButtonClasses($classes); |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Filter field classes |
|
| 22 | - * |
|
| 23 | - * @param array $classes An array of classes |
|
| 24 | - * |
|
| 25 | - * @return array A filtered array |
|
| 26 | - */ |
|
| 27 | - public function filterFieldClasses($classes); |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Add classes to a field |
|
| 31 | - * |
|
| 32 | - * @param Field $field |
|
| 33 | - * @param array $classes The possible classes to add |
|
| 34 | - * |
|
| 35 | - * @return Field |
|
| 36 | - */ |
|
| 37 | - public function getFieldClasses(Field $field, $classes); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Add group classes |
|
| 41 | - * |
|
| 42 | - * @return string A list of group classes |
|
| 43 | - */ |
|
| 44 | - public function getGroupClasses(); |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Add label classes |
|
| 48 | - * |
|
| 49 | - * @return array An array of attributes with the label class |
|
| 50 | - */ |
|
| 51 | - public function getLabelClasses(); |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Add uneditable field classes |
|
| 55 | - * |
|
| 56 | - * @return array An array of attributes with the uneditable class |
|
| 57 | - */ |
|
| 58 | - public function getUneditableClasses(); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Add plain text field classes |
|
| 62 | - * |
|
| 63 | - * @return array An array of attributes with the plain text class |
|
| 64 | - */ |
|
| 65 | - public function getPlainTextClasses(); |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Add form class |
|
| 69 | - * |
|
| 70 | - * @param string $type The type of form to add |
|
| 71 | - * |
|
| 72 | - * @return array |
|
| 73 | - */ |
|
| 74 | - public function getFormClasses($type); |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Add actions block class |
|
| 78 | - * |
|
| 79 | - * @return array |
|
| 80 | - */ |
|
| 81 | - public function getActionClasses(); |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Render an help text |
|
| 85 | - * |
|
| 86 | - * @param string $text |
|
| 87 | - * @param array $attributes |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function createHelp($text, $attributes = array()); |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Render a disabled field |
|
| 95 | - * |
|
| 96 | - * @param Field $field |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function createDisabledField(Field $field); |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Render a plain text field |
|
| 104 | - * |
|
| 105 | - * @param Field $field |
|
| 106 | - * |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - public function createPlainTextField(Field $field); |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Render an icon |
|
| 113 | - * |
|
| 114 | - * @param array $attributes Its attributes |
|
| 115 | - * |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - public function createIcon($iconType, $attributes = array()); |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Wrap an item to be prepended or appended to the current field |
|
| 122 | - * |
|
| 123 | - * @param string $item |
|
| 124 | - * |
|
| 125 | - * @return string A wrapped item |
|
| 126 | - */ |
|
| 127 | - public function placeAround($item); |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Wrap a field with prepended and appended items |
|
| 131 | - * |
|
| 132 | - * @param Field $field |
|
| 133 | - * @param array $prepend |
|
| 134 | - * @param array $append |
|
| 135 | - * |
|
| 136 | - * @return string A field concatented with prepended and/or appended items |
|
| 137 | - */ |
|
| 138 | - public function prependAppend($field, $prepend, $append); |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Wrap a field with potential additional tags |
|
| 142 | - * |
|
| 143 | - * @param Field $field |
|
| 144 | - * |
|
| 145 | - * @return string A wrapped field |
|
| 146 | - */ |
|
| 147 | - public function wrapField($field); |
|
| 11 | + /** |
|
| 12 | + * Filter buttons classes |
|
| 13 | + * |
|
| 14 | + * @param array $classes An array of classes |
|
| 15 | + * |
|
| 16 | + * @return array A filtered array |
|
| 17 | + */ |
|
| 18 | + public function filterButtonClasses($classes); |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Filter field classes |
|
| 22 | + * |
|
| 23 | + * @param array $classes An array of classes |
|
| 24 | + * |
|
| 25 | + * @return array A filtered array |
|
| 26 | + */ |
|
| 27 | + public function filterFieldClasses($classes); |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Add classes to a field |
|
| 31 | + * |
|
| 32 | + * @param Field $field |
|
| 33 | + * @param array $classes The possible classes to add |
|
| 34 | + * |
|
| 35 | + * @return Field |
|
| 36 | + */ |
|
| 37 | + public function getFieldClasses(Field $field, $classes); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Add group classes |
|
| 41 | + * |
|
| 42 | + * @return string A list of group classes |
|
| 43 | + */ |
|
| 44 | + public function getGroupClasses(); |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Add label classes |
|
| 48 | + * |
|
| 49 | + * @return array An array of attributes with the label class |
|
| 50 | + */ |
|
| 51 | + public function getLabelClasses(); |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Add uneditable field classes |
|
| 55 | + * |
|
| 56 | + * @return array An array of attributes with the uneditable class |
|
| 57 | + */ |
|
| 58 | + public function getUneditableClasses(); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Add plain text field classes |
|
| 62 | + * |
|
| 63 | + * @return array An array of attributes with the plain text class |
|
| 64 | + */ |
|
| 65 | + public function getPlainTextClasses(); |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Add form class |
|
| 69 | + * |
|
| 70 | + * @param string $type The type of form to add |
|
| 71 | + * |
|
| 72 | + * @return array |
|
| 73 | + */ |
|
| 74 | + public function getFormClasses($type); |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Add actions block class |
|
| 78 | + * |
|
| 79 | + * @return array |
|
| 80 | + */ |
|
| 81 | + public function getActionClasses(); |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Render an help text |
|
| 85 | + * |
|
| 86 | + * @param string $text |
|
| 87 | + * @param array $attributes |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function createHelp($text, $attributes = array()); |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Render a disabled field |
|
| 95 | + * |
|
| 96 | + * @param Field $field |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function createDisabledField(Field $field); |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Render a plain text field |
|
| 104 | + * |
|
| 105 | + * @param Field $field |
|
| 106 | + * |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + public function createPlainTextField(Field $field); |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Render an icon |
|
| 113 | + * |
|
| 114 | + * @param array $attributes Its attributes |
|
| 115 | + * |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + public function createIcon($iconType, $attributes = array()); |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Wrap an item to be prepended or appended to the current field |
|
| 122 | + * |
|
| 123 | + * @param string $item |
|
| 124 | + * |
|
| 125 | + * @return string A wrapped item |
|
| 126 | + */ |
|
| 127 | + public function placeAround($item); |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Wrap a field with prepended and appended items |
|
| 131 | + * |
|
| 132 | + * @param Field $field |
|
| 133 | + * @param array $prepend |
|
| 134 | + * @param array $append |
|
| 135 | + * |
|
| 136 | + * @return string A field concatented with prepended and/or appended items |
|
| 137 | + */ |
|
| 138 | + public function prependAppend($field, $prepend, $append); |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Wrap a field with potential additional tags |
|
| 142 | + * |
|
| 143 | + * @param Field $field |
|
| 144 | + * |
|
| 145 | + * @return string A wrapped field |
|
| 146 | + */ |
|
| 147 | + public function wrapField($field); |
|
| 148 | 148 | } |
@@ -6,10 +6,10 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | interface FieldInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Renders the field |
|
| 11 | - * |
|
| 12 | - * @return string |
|
| 13 | - */ |
|
| 14 | - public function render(); |
|
| 9 | + /** |
|
| 10 | + * Renders the field |
|
| 11 | + * |
|
| 12 | + * @return string |
|
| 13 | + */ |
|
| 14 | + public function render(); |
|
| 15 | 15 | } |
@@ -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 | } |
@@ -7,128 +7,128 @@ |
||
| 7 | 7 | |
| 8 | 8 | class File |
| 9 | 9 | { |
| 10 | - private static $mimes = array( |
|
| 11 | - 'ai' => 'application/postscript', |
|
| 12 | - 'aif' => 'audio/x-aiff', |
|
| 13 | - 'aifc' => 'audio/x-aiff', |
|
| 14 | - 'aiff' => 'audio/x-aiff', |
|
| 15 | - 'avi' => 'video/x-msvideo', |
|
| 16 | - 'bin' => 'application/macbinary', |
|
| 17 | - 'bmp' => 'image/bmp', |
|
| 18 | - 'class' => 'application/octet-stream', |
|
| 19 | - 'cpt' => 'application/mac-compactpro', |
|
| 20 | - 'css' => 'text/css', |
|
| 21 | - 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream'), |
|
| 22 | - 'dcr' => 'application/x-director', |
|
| 23 | - 'dir' => 'application/x-director', |
|
| 24 | - 'dll' => 'application/octet-stream', |
|
| 25 | - 'dms' => 'application/octet-stream', |
|
| 26 | - 'doc' => 'application/msword', |
|
| 27 | - 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
| 28 | - 'dvi' => 'application/x-dvi', |
|
| 29 | - 'dxr' => 'application/x-director', |
|
| 30 | - 'eml' => 'message/rfc822', |
|
| 31 | - 'eps' => 'application/postscript', |
|
| 32 | - 'exe' => array('application/octet-stream', 'application/x-msdownload'), |
|
| 33 | - 'gif' => 'image/gif', |
|
| 34 | - 'gtar' => 'application/x-gtar', |
|
| 35 | - 'gz' => 'application/x-gzip', |
|
| 36 | - 'hqx' => 'application/mac-binhex40', |
|
| 37 | - 'htm' => 'text/html', |
|
| 38 | - 'html' => 'text/html', |
|
| 39 | - 'jpe' => array('image/jpeg', 'image/pjpeg'), |
|
| 40 | - 'jpeg' => array('image/jpeg', 'image/pjpeg'), |
|
| 41 | - 'jpg' => array('image/jpeg', 'image/pjpeg'), |
|
| 42 | - 'js' => 'application/x-javascript', |
|
| 43 | - 'json' => array('application/json', 'text/json'), |
|
| 44 | - 'lha' => 'application/octet-stream', |
|
| 45 | - 'log' => array('text/plain', 'text/x-log'), |
|
| 46 | - 'lzh' => 'application/octet-stream', |
|
| 47 | - 'mid' => 'audio/midi', |
|
| 48 | - 'midi' => 'audio/midi', |
|
| 49 | - 'mif' => 'application/vnd.mif', |
|
| 50 | - 'mov' => 'video/quicktime', |
|
| 51 | - 'movie' => 'video/x-sgi-movie', |
|
| 52 | - 'mp2' => 'audio/mpeg', |
|
| 53 | - 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'), |
|
| 54 | - 'mpe' => 'video/mpeg', |
|
| 55 | - 'mpeg' => 'video/mpeg', |
|
| 56 | - 'mpg' => 'video/mpeg', |
|
| 57 | - 'mpga' => 'audio/mpeg', |
|
| 58 | - 'oda' => 'application/oda', |
|
| 59 | - 'odp' => 'application/vnd.oasis.opendocument.presentation', |
|
| 60 | - 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
|
| 61 | - 'odt' => 'application/vnd.oasis.opendocument.text', |
|
| 62 | - 'pdf' => array('application/pdf', 'application/x-download'), |
|
| 63 | - 'php' => array('application/x-httpd-php', 'text/x-php'), |
|
| 64 | - 'php3' => 'application/x-httpd-php', |
|
| 65 | - 'php4' => 'application/x-httpd-php', |
|
| 66 | - 'phps' => 'application/x-httpd-php-source', |
|
| 67 | - 'phtml' => 'application/x-httpd-php', |
|
| 68 | - 'png' => 'image/png', |
|
| 69 | - 'pps' => array('application/mspowerpoint', 'application/vnd.ms-powerpoint'), |
|
| 70 | - 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
|
| 71 | - 'ppt' => array('application/vnd.ms-powerpoint', 'application/powerpoint'), |
|
| 72 | - 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
| 73 | - 'ps' => 'application/postscript', |
|
| 74 | - 'psd' => 'application/x-photoshop', |
|
| 75 | - 'qt' => 'video/quicktime', |
|
| 76 | - 'ra' => 'audio/x-realaudio', |
|
| 77 | - 'ram' => 'audio/x-pn-realaudio', |
|
| 78 | - 'rm' => 'audio/x-pn-realaudio', |
|
| 79 | - 'rpm' => 'audio/x-pn-realaudio-plugin', |
|
| 80 | - 'rtf' => array('application/rtf', 'text/rtf'), |
|
| 81 | - 'rtx' => 'text/richtext', |
|
| 82 | - 'rv' => 'video/vnd.rn-realvideo', |
|
| 83 | - 'sea' => 'application/octet-stream', |
|
| 84 | - 'shtml' => 'text/html', |
|
| 85 | - 'sit' => 'application/x-stuffit', |
|
| 86 | - 'smi' => 'application/smil', |
|
| 87 | - 'smil' => 'application/smil', |
|
| 88 | - 'so' => 'application/octet-stream', |
|
| 89 | - 'swf' => 'application/x-shockwave-flash', |
|
| 90 | - 'tar' => 'application/x-tar', |
|
| 91 | - 'text' => 'text/plain', |
|
| 92 | - 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'), |
|
| 93 | - 'tif' => 'image/tiff', |
|
| 94 | - 'tiff' => 'image/tiff', |
|
| 95 | - 'txt' => 'text/plain', |
|
| 96 | - 'wav' => 'audio/x-wav', |
|
| 97 | - 'wbxml' => 'application/wbxml', |
|
| 98 | - 'wmlc' => 'application/wmlc', |
|
| 99 | - 'word' => array('application/msword', 'application/octet-stream'), |
|
| 100 | - 'xht' => 'application/xhtml+xml', |
|
| 101 | - 'xhtml' => 'application/xhtml+xml', |
|
| 102 | - 'xl' => 'application/excel', |
|
| 103 | - 'xls' => array('application/vnd.ms-excel', 'application/excel', 'application/msexcel'), |
|
| 104 | - 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
|
| 105 | - 'xml' => 'text/xml', |
|
| 106 | - 'xsl' => 'text/xml', |
|
| 107 | - 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), |
|
| 108 | - ); |
|
| 10 | + private static $mimes = array( |
|
| 11 | + 'ai' => 'application/postscript', |
|
| 12 | + 'aif' => 'audio/x-aiff', |
|
| 13 | + 'aifc' => 'audio/x-aiff', |
|
| 14 | + 'aiff' => 'audio/x-aiff', |
|
| 15 | + 'avi' => 'video/x-msvideo', |
|
| 16 | + 'bin' => 'application/macbinary', |
|
| 17 | + 'bmp' => 'image/bmp', |
|
| 18 | + 'class' => 'application/octet-stream', |
|
| 19 | + 'cpt' => 'application/mac-compactpro', |
|
| 20 | + 'css' => 'text/css', |
|
| 21 | + 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream'), |
|
| 22 | + 'dcr' => 'application/x-director', |
|
| 23 | + 'dir' => 'application/x-director', |
|
| 24 | + 'dll' => 'application/octet-stream', |
|
| 25 | + 'dms' => 'application/octet-stream', |
|
| 26 | + 'doc' => 'application/msword', |
|
| 27 | + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
| 28 | + 'dvi' => 'application/x-dvi', |
|
| 29 | + 'dxr' => 'application/x-director', |
|
| 30 | + 'eml' => 'message/rfc822', |
|
| 31 | + 'eps' => 'application/postscript', |
|
| 32 | + 'exe' => array('application/octet-stream', 'application/x-msdownload'), |
|
| 33 | + 'gif' => 'image/gif', |
|
| 34 | + 'gtar' => 'application/x-gtar', |
|
| 35 | + 'gz' => 'application/x-gzip', |
|
| 36 | + 'hqx' => 'application/mac-binhex40', |
|
| 37 | + 'htm' => 'text/html', |
|
| 38 | + 'html' => 'text/html', |
|
| 39 | + 'jpe' => array('image/jpeg', 'image/pjpeg'), |
|
| 40 | + 'jpeg' => array('image/jpeg', 'image/pjpeg'), |
|
| 41 | + 'jpg' => array('image/jpeg', 'image/pjpeg'), |
|
| 42 | + 'js' => 'application/x-javascript', |
|
| 43 | + 'json' => array('application/json', 'text/json'), |
|
| 44 | + 'lha' => 'application/octet-stream', |
|
| 45 | + 'log' => array('text/plain', 'text/x-log'), |
|
| 46 | + 'lzh' => 'application/octet-stream', |
|
| 47 | + 'mid' => 'audio/midi', |
|
| 48 | + 'midi' => 'audio/midi', |
|
| 49 | + 'mif' => 'application/vnd.mif', |
|
| 50 | + 'mov' => 'video/quicktime', |
|
| 51 | + 'movie' => 'video/x-sgi-movie', |
|
| 52 | + 'mp2' => 'audio/mpeg', |
|
| 53 | + 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'), |
|
| 54 | + 'mpe' => 'video/mpeg', |
|
| 55 | + 'mpeg' => 'video/mpeg', |
|
| 56 | + 'mpg' => 'video/mpeg', |
|
| 57 | + 'mpga' => 'audio/mpeg', |
|
| 58 | + 'oda' => 'application/oda', |
|
| 59 | + 'odp' => 'application/vnd.oasis.opendocument.presentation', |
|
| 60 | + 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
|
| 61 | + 'odt' => 'application/vnd.oasis.opendocument.text', |
|
| 62 | + 'pdf' => array('application/pdf', 'application/x-download'), |
|
| 63 | + 'php' => array('application/x-httpd-php', 'text/x-php'), |
|
| 64 | + 'php3' => 'application/x-httpd-php', |
|
| 65 | + 'php4' => 'application/x-httpd-php', |
|
| 66 | + 'phps' => 'application/x-httpd-php-source', |
|
| 67 | + 'phtml' => 'application/x-httpd-php', |
|
| 68 | + 'png' => 'image/png', |
|
| 69 | + 'pps' => array('application/mspowerpoint', 'application/vnd.ms-powerpoint'), |
|
| 70 | + 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
|
| 71 | + 'ppt' => array('application/vnd.ms-powerpoint', 'application/powerpoint'), |
|
| 72 | + 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
| 73 | + 'ps' => 'application/postscript', |
|
| 74 | + 'psd' => 'application/x-photoshop', |
|
| 75 | + 'qt' => 'video/quicktime', |
|
| 76 | + 'ra' => 'audio/x-realaudio', |
|
| 77 | + 'ram' => 'audio/x-pn-realaudio', |
|
| 78 | + 'rm' => 'audio/x-pn-realaudio', |
|
| 79 | + 'rpm' => 'audio/x-pn-realaudio-plugin', |
|
| 80 | + 'rtf' => array('application/rtf', 'text/rtf'), |
|
| 81 | + 'rtx' => 'text/richtext', |
|
| 82 | + 'rv' => 'video/vnd.rn-realvideo', |
|
| 83 | + 'sea' => 'application/octet-stream', |
|
| 84 | + 'shtml' => 'text/html', |
|
| 85 | + 'sit' => 'application/x-stuffit', |
|
| 86 | + 'smi' => 'application/smil', |
|
| 87 | + 'smil' => 'application/smil', |
|
| 88 | + 'so' => 'application/octet-stream', |
|
| 89 | + 'swf' => 'application/x-shockwave-flash', |
|
| 90 | + 'tar' => 'application/x-tar', |
|
| 91 | + 'text' => 'text/plain', |
|
| 92 | + 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'), |
|
| 93 | + 'tif' => 'image/tiff', |
|
| 94 | + 'tiff' => 'image/tiff', |
|
| 95 | + 'txt' => 'text/plain', |
|
| 96 | + 'wav' => 'audio/x-wav', |
|
| 97 | + 'wbxml' => 'application/wbxml', |
|
| 98 | + 'wmlc' => 'application/wmlc', |
|
| 99 | + 'word' => array('application/msword', 'application/octet-stream'), |
|
| 100 | + 'xht' => 'application/xhtml+xml', |
|
| 101 | + 'xhtml' => 'application/xhtml+xml', |
|
| 102 | + 'xl' => 'application/excel', |
|
| 103 | + 'xls' => array('application/vnd.ms-excel', 'application/excel', 'application/msexcel'), |
|
| 104 | + 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
|
| 105 | + 'xml' => 'text/xml', |
|
| 106 | + 'xsl' => 'text/xml', |
|
| 107 | + 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), |
|
| 108 | + ); |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Get a file MIME type by extension. |
|
| 112 | - * <code> |
|
| 113 | - * // Determine the MIME type for the .tar extension |
|
| 114 | - * $mime = File::mime('tar'); |
|
| 115 | - * // Return a default value if the MIME can't be determined |
|
| 116 | - * $mime = File::mime('ext', 'application/octet-stream'); |
|
| 117 | - * </code> |
|
| 118 | - * |
|
| 119 | - * @param string $extension |
|
| 120 | - * @param string $default |
|
| 121 | - * |
|
| 122 | - * @return string |
|
| 123 | - */ |
|
| 124 | - public static function mime($extension, $default = 'application/octet-stream') |
|
| 125 | - { |
|
| 126 | - $mimes = self::$mimes; |
|
| 110 | + /** |
|
| 111 | + * Get a file MIME type by extension. |
|
| 112 | + * <code> |
|
| 113 | + * // Determine the MIME type for the .tar extension |
|
| 114 | + * $mime = File::mime('tar'); |
|
| 115 | + * // Return a default value if the MIME can't be determined |
|
| 116 | + * $mime = File::mime('ext', 'application/octet-stream'); |
|
| 117 | + * </code> |
|
| 118 | + * |
|
| 119 | + * @param string $extension |
|
| 120 | + * @param string $default |
|
| 121 | + * |
|
| 122 | + * @return string |
|
| 123 | + */ |
|
| 124 | + public static function mime($extension, $default = 'application/octet-stream') |
|
| 125 | + { |
|
| 126 | + $mimes = self::$mimes; |
|
| 127 | 127 | |
| 128 | - if (!array_key_exists($extension, $mimes)) { |
|
| 129 | - return $default; |
|
| 130 | - } |
|
| 128 | + if (!array_key_exists($extension, $mimes)) { |
|
| 129 | + return $default; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; |
|
| 133 | - } |
|
| 132 | + return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; |
|
| 133 | + } |
|
| 134 | 134 | } |