@@ -2,7 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace Lepton\Http\Response; |
| 4 | 4 | |
| 5 | - class JSONResponse extends SuccessResponse |
|
| 5 | + class JSONResponse extends SuccessResponse |
|
| 6 | 6 | { |
| 7 | 7 | |
| 8 | 8 | public function __construct( |
@@ -4,59 +4,59 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Field{ |
| 6 | 6 | |
| 7 | - protected array $default_error_messages = array( |
|
| 7 | + protected array $default_error_messages = array( |
|
| 8 | 8 | "invalid_choice" => "Value %value is not a valid choice.", |
| 9 | 9 | "null" => "This field cannot be null.", |
| 10 | 10 | "blank" => "This field cannot be blank.", |
| 11 | 11 | "unique" => "A %model_name with this %field_label already exists.", |
| 12 | 12 | "unique_for" => "Field %field_label must be unique for fields with same %unique_for." |
| 13 | - ); |
|
| 14 | - |
|
| 15 | - protected array $validation_errors = array(); |
|
| 16 | - |
|
| 17 | - public function __construct( |
|
| 18 | - protected bool $null = false, |
|
| 19 | - protected bool $blank = true, |
|
| 20 | - protected array $choices = array(), |
|
| 21 | - protected string $db_column = "", |
|
| 22 | - protected mixed $default = "", |
|
| 23 | - protected bool $editable = true, |
|
| 24 | - protected array $error_messages = array(), |
|
| 25 | - protected string $help_text = "", |
|
| 26 | - protected bool $primary_key = false, |
|
| 27 | - protected bool $unique = false, |
|
| 28 | - protected string $verbose_name = "", |
|
| 29 | - protected array $validators = array() |
|
| 13 | + ); |
|
| 14 | + |
|
| 15 | + protected array $validation_errors = array(); |
|
| 16 | + |
|
| 17 | + public function __construct( |
|
| 18 | + protected bool $null = false, |
|
| 19 | + protected bool $blank = true, |
|
| 20 | + protected array $choices = array(), |
|
| 21 | + protected string $db_column = "", |
|
| 22 | + protected mixed $default = "", |
|
| 23 | + protected bool $editable = true, |
|
| 24 | + protected array $error_messages = array(), |
|
| 25 | + protected string $help_text = "", |
|
| 26 | + protected bool $primary_key = false, |
|
| 27 | + protected bool $unique = false, |
|
| 28 | + protected string $verbose_name = "", |
|
| 29 | + protected array $validators = array() |
|
| 30 | 30 | ) |
| 31 | - { |
|
| 31 | + { |
|
| 32 | 32 | return; |
| 33 | - } |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - protected function validate($value){ |
|
| 36 | + protected function validate($value){ |
|
| 37 | 37 | if (is_null($value) && (!$this->null)) return false; |
| 38 | 38 | if (empty($value) && (!$this->blank)) return false; |
| 39 | 39 | if (!empty($this->choices) && !in_array($value, $this->choices)) return false; |
| 40 | 40 | if (!empty($this->validators)) return $this->validate_with_validators($value); |
| 41 | 41 | |
| 42 | 42 | return true; |
| 43 | - } |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | |
| 46 | - private function validate_with_validators($value){ |
|
| 46 | + private function validate_with_validators($value){ |
|
| 47 | 47 | foreach($this->validators as $validator){ |
| 48 | - if (!$validator($value)) return false; |
|
| 48 | + if (!$validator($value)) return false; |
|
| 49 | 49 | } |
| 50 | 50 | return true; |
| 51 | - } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | 53 | |
| 54 | 54 | |
| 55 | - public function db_column(): string{ |
|
| 55 | + public function db_column(): string{ |
|
| 56 | 56 | return $this->db_column; |
| 57 | - } |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - public function set_db_column(string $column_name): void{ |
|
| 59 | + public function set_db_column(string $column_name): void{ |
|
| 60 | 60 | $this->db_column = $column_name; |
| 61 | - } |
|
| 61 | + } |
|
| 62 | 62 | } |
| 63 | 63 | \ No newline at end of file |
@@ -6,13 +6,13 @@ |
||
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | |
| 9 | - public function __construct(private $max_length = 128, ...$options ){ |
|
| 10 | - parent::__construct(...$options); |
|
| 9 | + public function __construct(private $max_length = 128, ...$options ){ |
|
| 10 | + parent::__construct(...$options); |
|
| 11 | 11 | |
| 12 | - } |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | - public function validate($value){ |
|
| 14 | + public function validate($value){ |
|
| 15 | 15 | if(strlen($value) > $this->max_length) return false; |
| 16 | 16 | return parent::validate($value); |
| 17 | - } |
|
| 17 | + } |
|
| 18 | 18 | } |
| 19 | 19 | \ No newline at end of file |
@@ -3,13 +3,13 @@ |
||
| 3 | 3 | |
| 4 | 4 | #[\Attribute] |
| 5 | 5 | class DateField extends Field{ |
| 6 | - public function __construct(...$args){ |
|
| 6 | + public function __construct(...$args){ |
|
| 7 | 7 | parent::__construct(...$args); |
| 8 | - } |
|
| 8 | + } |
|
| 9 | 9 | |
| 10 | - public function validate($value){ |
|
| 10 | + public function validate($value){ |
|
| 11 | 11 | if (!preg_match("/\d{4}-\d{2}-\d{2}/", $value)) return false; |
| 12 | 12 | return parent::validate($value); |
| 13 | - } |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | 15 | } |
| 16 | 16 | \ No newline at end of file |
@@ -3,12 +3,12 @@ |
||
| 3 | 3 | |
| 4 | 4 | #[\Attribute] |
| 5 | 5 | class NumberField extends Field{ |
| 6 | - public function __construct(mixed ...$options){ |
|
| 6 | + public function __construct(mixed ...$options){ |
|
| 7 | 7 | parent::__construct(...$options); |
| 8 | - } |
|
| 8 | + } |
|
| 9 | 9 | |
| 10 | - public function validate($value){ |
|
| 10 | + public function validate($value){ |
|
| 11 | 11 | if(!is_numeric($value)) return false; |
| 12 | 12 | return parent::validate($value); |
| 13 | - } |
|
| 13 | + } |
|
| 14 | 14 | } |
| 15 | 15 | \ No newline at end of file |
@@ -3,12 +3,12 @@ |
||
| 3 | 3 | |
| 4 | 4 | #[\Attribute] |
| 5 | 5 | class PrimaryKey extends Field{ |
| 6 | - public function __construct(mixed ...$options){ |
|
| 6 | + public function __construct(mixed ...$options){ |
|
| 7 | 7 | $this->null = false; |
| 8 | 8 | parent::__construct(...$options); |
| 9 | - } |
|
| 9 | + } |
|
| 10 | 10 | |
| 11 | - public function validate($value){ |
|
| 11 | + public function validate($value){ |
|
| 12 | 12 | if(!is_numeric($value)) return false; |
| 13 | 13 | return parent::validate($value); |
| 14 | 14 | } |
@@ -3,12 +3,12 @@ |
||
| 3 | 3 | |
| 4 | 4 | #[\Attribute] |
| 5 | 5 | class DateTimeField extends Field{ |
| 6 | - public function __construct(...$args){ |
|
| 6 | + public function __construct(...$args){ |
|
| 7 | 7 | parent::__construct(...$args); |
| 8 | - } |
|
| 8 | + } |
|
| 9 | 9 | |
| 10 | - public function validate($value){ |
|
| 10 | + public function validate($value){ |
|
| 11 | 11 | if (!preg_match("/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/", $value)) return false; |
| 12 | 12 | return parent::validate($value); |
| 13 | - } |
|
| 13 | + } |
|
| 14 | 14 | } |
| 15 | 15 | \ No newline at end of file |
@@ -5,13 +5,13 @@ |
||
| 5 | 5 | class JSONField extends Field{ |
| 6 | 6 | |
| 7 | 7 | |
| 8 | - public function __construct(...$options ){ |
|
| 9 | - parent::__construct(...$options); |
|
| 8 | + public function __construct(...$options ){ |
|
| 9 | + parent::__construct(...$options); |
|
| 10 | 10 | |
| 11 | - } |
|
| 11 | + } |
|
| 12 | 12 | |
| 13 | - public function validate($value){ |
|
| 13 | + public function validate($value){ |
|
| 14 | 14 | if(!is_array(json_decode($value, true))) return false; |
| 15 | 15 | return parent::validate($value); |
| 16 | - } |
|
| 16 | + } |
|
| 17 | 17 | } |
| 18 | 18 | \ No newline at end of file |
@@ -81,49 +81,49 @@ discard block |
||
| 81 | 81 | public function __construct(protected string $model) |
| 82 | 82 | { |
| 83 | 83 | $this->lookup_map = array( |
| 84 | - "equals" => [ |
|
| 84 | + "equals" => [ |
|
| 85 | 85 | "operator" => "=", |
| 86 | 86 | "rhs" => fn ($x) => $x |
| 87 | - ], |
|
| 88 | - "startswith" => [ |
|
| 87 | + ], |
|
| 88 | + "startswith" => [ |
|
| 89 | 89 | "operator" => "LIKE", |
| 90 | 90 | "rhs" => fn ($x) => sprintf('%s%%', $x) |
| 91 | - ], |
|
| 91 | + ], |
|
| 92 | 92 | |
| 93 | - "endswith" => [ |
|
| 93 | + "endswith" => [ |
|
| 94 | 94 | "operator" => "LIKE", |
| 95 | 95 | "rhs" => fn ($x) => sprintf('%%%s', $x) |
| 96 | - ], |
|
| 96 | + ], |
|
| 97 | 97 | |
| 98 | - "contains" => [ |
|
| 98 | + "contains" => [ |
|
| 99 | 99 | "operator" => "LIKE", |
| 100 | 100 | "rhs" => fn ($x) => sprintf('%%%s%%', $x) |
| 101 | - ], |
|
| 101 | + ], |
|
| 102 | 102 | |
| 103 | - "lte" => [ |
|
| 103 | + "lte" => [ |
|
| 104 | 104 | "operator" => "<=", |
| 105 | 105 | "rhs" => fn ($x) => $x |
| 106 | - ], |
|
| 106 | + ], |
|
| 107 | 107 | |
| 108 | - "gte" => [ |
|
| 108 | + "gte" => [ |
|
| 109 | 109 | "operator" => ">=", |
| 110 | 110 | "rhs" => fn ($x) => $x |
| 111 | - ], |
|
| 111 | + ], |
|
| 112 | 112 | |
| 113 | - "lt" => [ |
|
| 113 | + "lt" => [ |
|
| 114 | 114 | "operator" => "<", |
| 115 | 115 | "rhs" => fn ($x) => $x |
| 116 | - ], |
|
| 116 | + ], |
|
| 117 | 117 | |
| 118 | - "gt" => [ |
|
| 118 | + "gt" => [ |
|
| 119 | 119 | "operator" => ">", |
| 120 | 120 | "rhs" => fn ($x) => $x |
| 121 | - ], |
|
| 121 | + ], |
|
| 122 | 122 | |
| 123 | - "neq" => [ |
|
| 123 | + "neq" => [ |
|
| 124 | 124 | "operator" => "<>", |
| 125 | 125 | "rhs" => fn ($x) => $x |
| 126 | - ] |
|
| 126 | + ] |
|
| 127 | 127 | ); |
| 128 | 128 | |
| 129 | 129 | $this->filters = array(); |
@@ -497,7 +497,7 @@ discard block |
||
| 497 | 497 | $query .= sprintf(" ORDER BY %s", $modifiers); |
| 498 | 498 | } |
| 499 | 499 | |
| 500 | - //if(strpos($query, "ORDER BY")) die (print_r($query)); |
|
| 500 | + //if(strpos($query, "ORDER BY")) die (print_r($query)); |
|
| 501 | 501 | return array($query, $values); |
| 502 | 502 | } |
| 503 | 503 | |
@@ -574,7 +574,7 @@ discard block |
||
| 574 | 574 | } |
| 575 | 575 | $clause = implode(", ", $conditions); |
| 576 | 576 | return [$clause, $join]; |
| 577 | - } |
|
| 577 | + } |
|
| 578 | 578 | |
| 579 | 579 | |
| 580 | 580 | private function buildWhereClause($filters) |
@@ -679,9 +679,9 @@ discard block |
||
| 679 | 679 | } |
| 680 | 680 | |
| 681 | 681 | return array( |
| 682 | - "column" => $last->getColumnFromField($column), |
|
| 683 | - "condition" => $condition, |
|
| 684 | - "join" => $join |
|
| 682 | + "column" => $last->getColumnFromField($column), |
|
| 683 | + "condition" => $condition, |
|
| 684 | + "join" => $join |
|
| 685 | 685 | ); |
| 686 | 686 | } |
| 687 | 687 | } |