@@ -30,6 +30,9 @@ discard block |
||
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | + /** |
|
| 34 | + * @param string $class |
|
| 35 | + */ |
|
| 33 | 36 | public static function getNamespace($class) |
| 34 | 37 | { |
| 35 | 38 | try { |
@@ -45,6 +48,10 @@ discard block |
||
| 45 | 48 | } |
| 46 | 49 | } |
| 47 | 50 | |
| 51 | + /** |
|
| 52 | + * @param \Helmut\Forms\Field $class |
|
| 53 | + * @param string $method |
|
| 54 | + */ |
|
| 48 | 55 | public static function getParameters($class, $method) |
| 49 | 56 | { |
| 50 | 57 | $params = []; |
@@ -3,8 +3,8 @@ |
||
| 3 | 3 | namespace Helmut\Forms\Utility; |
| 4 | 4 | |
| 5 | 5 | use ReflectionClass; |
| 6 | -use ReflectionMethod; |
|
| 7 | 6 | use ReflectionException; |
| 7 | +use ReflectionMethod; |
|
| 8 | 8 | |
| 9 | 9 | class Reflect { |
| 10 | 10 | |
@@ -53,7 +53,7 @@ |
||
| 53 | 53 | |
| 54 | 54 | $method = new ReflectionMethod($class, $method); |
| 55 | 55 | |
| 56 | - foreach($method->getParameters() as $param) { |
|
| 56 | + foreach ($method->getParameters() as $param) { |
|
| 57 | 57 | $params[] = $param->getName(); |
| 58 | 58 | } |
| 59 | 59 | |
@@ -4,73 +4,73 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Validate { |
| 6 | 6 | |
| 7 | - public static function required($var) |
|
| 8 | - { |
|
| 9 | - if (is_null($var)) return false; |
|
| 7 | + public static function required($var) |
|
| 8 | + { |
|
| 9 | + if (is_null($var)) return false; |
|
| 10 | 10 | else if (is_string($var) && trim($var) === '') return false; |
| 11 | 11 | return true; |
| 12 | - } |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | - public static function stringMin($var, $min) |
|
| 15 | - { |
|
| 16 | - return strlen($var) >= $min; |
|
| 17 | - } |
|
| 14 | + public static function stringMin($var, $min) |
|
| 15 | + { |
|
| 16 | + return strlen($var) >= $min; |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - public static function stringMax($var, $max) |
|
| 20 | - { |
|
| 21 | - return strlen($var) <= $max; |
|
| 22 | - } |
|
| 19 | + public static function stringMax($var, $max) |
|
| 20 | + { |
|
| 21 | + return strlen($var) <= $max; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public static function alpha($var) |
|
| 25 | - { |
|
| 26 | - return is_string($var) && preg_match('/^[\pL\pM]+$/u', $var); |
|
| 27 | - } |
|
| 24 | + public static function alpha($var) |
|
| 25 | + { |
|
| 26 | + return is_string($var) && preg_match('/^[\pL\pM]+$/u', $var); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - public static function alphaNum($var) |
|
| 30 | - { |
|
| 31 | - if (! is_string($var) && ! is_numeric($var)) return false; |
|
| 29 | + public static function alphaNum($var) |
|
| 30 | + { |
|
| 31 | + if (! is_string($var) && ! is_numeric($var)) return false; |
|
| 32 | 32 | return preg_match('/^[\pL\pM\pN]+$/u', $var); |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - public static function alphaDash($var) |
|
| 36 | - { |
|
| 37 | - if (! is_string($var) && ! is_numeric($var)) return false; |
|
| 35 | + public static function alphaDash($var) |
|
| 36 | + { |
|
| 37 | + if (! is_string($var) && ! is_numeric($var)) return false; |
|
| 38 | 38 | return preg_match('/^[\pL\pM\pN_-]+$/u', $var); |
| 39 | - } |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - public static function in($var, $array) |
|
| 42 | - { |
|
| 43 | - return in_array($var, $array); |
|
| 44 | - } |
|
| 41 | + public static function in($var, $array) |
|
| 42 | + { |
|
| 43 | + return in_array($var, $array); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public static function notIn($var, $array) |
|
| 47 | - { |
|
| 48 | - return ! in_array($var, $array); |
|
| 49 | - } |
|
| 46 | + public static function notIn($var, $array) |
|
| 47 | + { |
|
| 48 | + return ! in_array($var, $array); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - public static function email($var) |
|
| 52 | - { |
|
| 53 | - return filter_var($var, FILTER_VALIDATE_EMAIL) !== false; |
|
| 54 | - } |
|
| 51 | + public static function email($var) |
|
| 52 | + { |
|
| 53 | + return filter_var($var, FILTER_VALIDATE_EMAIL) !== false; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - public static function numeric($var) |
|
| 57 | - { |
|
| 58 | - return is_numeric($var); |
|
| 59 | - } |
|
| 56 | + public static function numeric($var) |
|
| 57 | + { |
|
| 58 | + return is_numeric($var); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - public static function numericMin($var, $min) |
|
| 62 | - { |
|
| 63 | - return $var >= $min; |
|
| 64 | - } |
|
| 61 | + public static function numericMin($var, $min) |
|
| 62 | + { |
|
| 63 | + return $var >= $min; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - public static function numericMax($var, $max) |
|
| 67 | - { |
|
| 68 | - return $var <= $max; |
|
| 69 | - } |
|
| 66 | + public static function numericMax($var, $max) |
|
| 67 | + { |
|
| 68 | + return $var <= $max; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - public static function integer($var) |
|
| 72 | - { |
|
| 73 | - return filter_var($var, FILTER_VALIDATE_INT) !== false; |
|
| 74 | - } |
|
| 71 | + public static function integer($var) |
|
| 72 | + { |
|
| 73 | + return filter_var($var, FILTER_VALIDATE_INT) !== false; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | 76 | } |
@@ -4,18 +4,18 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Str { |
| 6 | 6 | |
| 7 | - public static function snake($var) |
|
| 8 | - { |
|
| 9 | - if ( ! ctype_lower($var)) { |
|
| 10 | - $var = preg_replace('/\s+/', '', $var); |
|
| 11 | - $var = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.'_', $var)); |
|
| 12 | - } |
|
| 13 | - return $var; |
|
| 14 | - } |
|
| 7 | + public static function snake($var) |
|
| 8 | + { |
|
| 9 | + if ( ! ctype_lower($var)) { |
|
| 10 | + $var = preg_replace('/\s+/', '', $var); |
|
| 11 | + $var = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.'_', $var)); |
|
| 12 | + } |
|
| 13 | + return $var; |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - public static function studly($var) |
|
| 17 | - { |
|
| 18 | - return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $var))); |
|
| 19 | - } |
|
| 16 | + public static function studly($var) |
|
| 17 | + { |
|
| 18 | + return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $var))); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | 21 | } |
@@ -12,50 +12,50 @@ discard block |
||
| 12 | 12 | * |
| 13 | 13 | * @var string |
| 14 | 14 | */ |
| 15 | - protected $path; |
|
| 15 | + protected $path; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 17 | + /** |
|
| 18 | 18 | * Create a new plugin instance. |
| 19 | 19 | * |
| 20 | 20 | */ |
| 21 | - public function __construct() |
|
| 22 | - { |
|
| 23 | - $this->setPath(); |
|
| 24 | - } |
|
| 21 | + public function __construct() |
|
| 22 | + { |
|
| 23 | + $this->setPath(); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 26 | + /** |
|
| 27 | 27 | * Set the plugin autoload path. |
| 28 | 28 | * |
| 29 | 29 | * @return void |
| 30 | 30 | */ |
| 31 | - public function setPath() |
|
| 32 | - { |
|
| 31 | + public function setPath() |
|
| 32 | + { |
|
| 33 | 33 | $this->path = Reflect::getDirectory($this); |
| 34 | - } |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | + /** |
|
| 37 | 37 | * Get autoload path. |
| 38 | 38 | * |
| 39 | 39 | * @return string |
| 40 | 40 | */ |
| 41 | - public function path($append = null) |
|
| 42 | - { |
|
| 43 | - return is_null($append) ? $this->path |
|
| 44 | - : $this->path.'/'.ltrim(rtrim($append, '/'), '/').'/'; |
|
| 45 | - } |
|
| 41 | + public function path($append = null) |
|
| 42 | + { |
|
| 43 | + return is_null($append) ? $this->path |
|
| 44 | + : $this->path.'/'.ltrim(rtrim($append, '/'), '/').'/'; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | + /** |
|
| 48 | 48 | * Get paths to templates. |
| 49 | 49 | * |
| 50 | 50 | * @return string |
| 51 | 51 | */ |
| 52 | - public function templatePaths() |
|
| 53 | - { |
|
| 54 | - $path = $this->path('templates'); |
|
| 55 | - return [$path]; |
|
| 56 | - } |
|
| 52 | + public function templatePaths() |
|
| 53 | + { |
|
| 54 | + $path = $this->path('templates'); |
|
| 55 | + return [$path]; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 58 | + /** |
|
| 59 | 59 | * Trigger an event callback. |
| 60 | 60 | * - onLoad |
| 61 | 61 | * - onDefine |
@@ -67,19 +67,19 @@ discard block |
||
| 67 | 67 | * - onValid |
| 68 | 68 | * - onInvalid |
| 69 | 69 | * |
| 70 | - * @param \Helmut\Forms\Form $form |
|
| 71 | - * @param string $name |
|
| 72 | - * @param array $params |
|
| 70 | + * @param \Helmut\Forms\Form $form |
|
| 71 | + * @param string $name |
|
| 72 | + * @param array $params |
|
| 73 | 73 | * @return mixed |
| 74 | 74 | */ |
| 75 | - public function event($form, $name, $params = []) |
|
| 76 | - { |
|
| 77 | - $name = Str::studly($name); |
|
| 75 | + public function event($form, $name, $params = []) |
|
| 76 | + { |
|
| 77 | + $name = Str::studly($name); |
|
| 78 | 78 | |
| 79 | - if (method_exists($this, 'on'.$name)) { |
|
| 80 | - return call_user_func_array(array($this, 'on'.$name), [$form, $params]); |
|
| 81 | - } |
|
| 79 | + if (method_exists($this, 'on'.$name)) { |
|
| 80 | + return call_user_func_array(array($this, 'on'.$name), [$form, $params]); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - } |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | 85 | } |
@@ -253,7 +253,9 @@ discard block |
||
| 253 | 253 | */ |
| 254 | 254 | public function fields($names = null) |
| 255 | 255 | { |
| 256 | - if (is_null($names)) return $this->fields; |
|
| 256 | + if (is_null($names)) { |
|
| 257 | + return $this->fields; |
|
| 258 | + } |
|
| 257 | 259 | |
| 258 | 260 | return array_filter($this->fields, function($field) use ($names) { |
| 259 | 261 | return array_key_exists($field->name, $names); |
@@ -473,7 +475,9 @@ discard block |
||
| 473 | 475 | $class = '\\Fields\\'.ucwords($class).'\\'.ucwords($class); |
| 474 | 476 | |
| 475 | 477 | foreach ($this->namespaces as $namespace) { |
| 476 | - if (class_exists($namespace.$class)) return $namespace.$class; |
|
| 478 | + if (class_exists($namespace.$class)) { |
|
| 479 | + return $namespace.$class; |
|
| 480 | + } |
|
| 477 | 481 | } |
| 478 | 482 | } |
| 479 | 483 | |
@@ -486,9 +490,13 @@ discard block |
||
| 486 | 490 | */ |
| 487 | 491 | public function errors($name = null) |
| 488 | 492 | { |
| 489 | - if ($this->submitted()) $this->validate(); |
|
| 493 | + if ($this->submitted()) { |
|
| 494 | + $this->validate(); |
|
| 495 | + } |
|
| 490 | 496 | |
| 491 | - if ( ! is_null($name)) return $this->field($name)->errors(); |
|
| 497 | + if ( ! is_null($name)) { |
|
| 498 | + return $this->field($name)->errors(); |
|
| 499 | + } |
|
| 492 | 500 | |
| 493 | 501 | $errors = []; |
| 494 | 502 | |
@@ -680,8 +688,12 @@ discard block |
||
| 680 | 688 | $message = $this->translate($error, $field); |
| 681 | 689 | |
| 682 | 690 | foreach($parameters as $key => $value) { |
| 683 | - if (is_object($value) && method_exists($value, '__toString')) $value = (string) $value; |
|
| 684 | - if (is_array($value)) $value = implode(', ', $value); |
|
| 691 | + if (is_object($value) && method_exists($value, '__toString')) { |
|
| 692 | + $value = (string) $value; |
|
| 693 | + } |
|
| 694 | + if (is_array($value)) { |
|
| 695 | + $value = implode(', ', $value); |
|
| 696 | + } |
|
| 685 | 697 | $message = str_replace('['.$key.']', $value, $message); |
| 686 | 698 | } |
| 687 | 699 | $properties['errors'][] = ['error' => $message]; |
@@ -701,7 +713,9 @@ discard block |
||
| 701 | 713 | */ |
| 702 | 714 | public function renderField($field, $properties = null) |
| 703 | 715 | { |
| 704 | - if (is_null($properties)) $properties = $this->fieldProperties($field); |
|
| 716 | + if (is_null($properties)) { |
|
| 717 | + $properties = $this->fieldProperties($field); |
|
| 718 | + } |
|
| 705 | 719 | |
| 706 | 720 | return $this->renderTemplate($field->type, $properties, $field); |
| 707 | 721 | } |
@@ -715,7 +729,9 @@ discard block |
||
| 715 | 729 | */ |
| 716 | 730 | public function renderFieldErrors($field, $properties = null) |
| 717 | 731 | { |
| 718 | - if (is_null($properties)) $properties = $this->fieldProperties($field); |
|
| 732 | + if (is_null($properties)) { |
|
| 733 | + $properties = $this->fieldProperties($field); |
|
| 734 | + } |
|
| 719 | 735 | |
| 720 | 736 | return $this->renderTemplate('errors', $properties, $field); |
| 721 | 737 | } |
@@ -732,9 +748,13 @@ discard block |
||
| 732 | 748 | |
| 733 | 749 | $this->setValues(); |
| 734 | 750 | |
| 735 | - if ($this->submitted()) $this->validate(); |
|
| 751 | + if ($this->submitted()) { |
|
| 752 | + $this->validate(); |
|
| 753 | + } |
|
| 736 | 754 | |
| 737 | - if ( ! is_null($template)) $this->setTemplate($template); |
|
| 755 | + if ( ! is_null($template)) { |
|
| 756 | + $this->setTemplate($template); |
|
| 757 | + } |
|
| 738 | 758 | |
| 739 | 759 | $properties = []; |
| 740 | 760 | $properties['id'] = $this->id; |