Skip to content

Commit cc40d12

Browse files
committed
fixed responsewithcode bug and added base apicontroller
1 parent 2ae8ea0 commit cc40d12

3 files changed

Lines changed: 111 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
### v1.4 - 1st November, 2019
33
#### Added
44
- Added base Leaf Controller `Leaf\Core\Controller`
5+
- Added base controller for APIs: `Leaf\Core\ApiController`
56
- Added base Leaf Model `Leaf\Core\Model`
67
- Added support for full MVC app
78
- Added [Leaf Veins](https://github.com/leafsphp/veins) in default Leaf package
@@ -13,6 +14,7 @@
1314

1415
#### Fixed
1516
- Fixed bug with `Response::renderHtmlPage()`
17+
- Fixed the HTTP code rendering in the browser from `Response::respondWithCode`
1618

1719

1820
#### Changed

src/core/Http/response.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
22
namespace Leaf\Core\Http;
3-
3+
/**
4+
* Leaf PHP Response
5+
* --------------------------
6+
* Handles responses from Leaf App
7+
*/
48
class Response {
59
public function respond($data) {
610
header('Content-Type: application/json');
711
echo json_encode($data);
812
}
913

1014
public function respondWithCode($data, $code = 200) {
11-
header('Content-Type: application/json');
15+
header('Content-Type: application/json', true, $code);
1216
$dataToPrint = array('data' => $data, 'code' => $code);
1317
echo json_encode($dataToPrint);
1418
}

src/core/apiController.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
namespace Leaf\Core;
3+
use Leaf\Core\Http\Response;
4+
/**
5+
* Leaf PHP base controller
6+
* --------------------------
7+
* Loads the model and views
8+
*/
9+
class Controller {
10+
public $response;
11+
public function __construct() {
12+
$this->$response = new Response;
13+
}
14+
15+
/**
16+
* Submit JSON encoded data to user
17+
*
18+
* @param array $data: An array of data to be displayed to the user
19+
*
20+
* @return void
21+
*/
22+
public function respond($data) {
23+
$this->response->respond($data);
24+
}
25+
26+
/**
27+
* Submit JSON encoded data to user with an http code
28+
*
29+
* @param array $data: An array of data to be displayed to the user
30+
* @param integer $code: An array of data to be displayed to the user
31+
*
32+
* @return void
33+
*/
34+
public function respondWithCode($data, $code) {
35+
$this->response->respondWithCode($data, $code);
36+
}
37+
38+
public function file_upload($path, $file, $file_category = "image"): Array {
39+
// get file details
40+
$name = strtolower(strtotime(date("Y-m-d H:i:s")).'_'.str_replace(" ", "",$file["name"]));
41+
$temp = $file["tmp_name"];
42+
$size = $file["size"];
43+
44+
$target_dir = $path; // destination path
45+
$target_file = $target_dir . basename($name); // destination file
46+
$upload_ok = true; // upload checker
47+
$file_type = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); // file type
48+
49+
50+
if (!file_exists($path)):
51+
mkdir($path, 0777, true);
52+
endif;
53+
54+
// Check if file already exists
55+
if (file_exists($target_file)) {
56+
return [true, $name];
57+
}
58+
// Check file size
59+
if ($size > 2000000) {
60+
return [false, "file too big"];
61+
}
62+
// Allow certain file formats
63+
switch ($file_category) {
64+
case 'image':
65+
$extensions = ['jpg', 'jpeg', 'png', 'gif'];
66+
break;
67+
68+
case 'audio':
69+
$extensions = ['wav', 'mp3', 'ogg', 'wav', 'm4a'];
70+
break;
71+
}
72+
73+
if (!in_array($file_type, $extensions)) {
74+
return [false, $file['name']." format not acceptable"];
75+
}
76+
// Check if $upload_ok is set to 0 by an error
77+
if (move_uploaded_file($temp, $target_file)) {
78+
return [true, $name];
79+
} else {
80+
return [false, "Wasn't able to upload {$file_category}"];
81+
}
82+
}
83+
84+
function startsWith($haystack, $needle): Bool {
85+
$length = strlen($needle);
86+
return (substr($haystack, 0, $length) === $needle);
87+
}
88+
89+
public function objectify($array): Object {
90+
return \json_decode(\json_encode($array));
91+
}
92+
93+
public function permit($array, $permitables): Array {
94+
$permits = [];
95+
96+
foreach ($permitables as $key => $permitable) {
97+
$permits[$permitable] = $array[$permitable];
98+
}
99+
100+
return $permits;
101+
}
102+
}
103+

0 commit comments

Comments
 (0)