Skip to content

Commit 204014d

Browse files
committed
Adding initial classes to output Bible text - #239
1 parent 7906e35 commit 204014d

File tree

7 files changed

+173
-5
lines changed

7 files changed

+173
-5
lines changed

src/classes/netbible/api.class.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Obadiah\NetBible;
4+
5+
use Obadiah\App;
6+
use Obadiah\Helpers\Log;
7+
8+
App::check();
9+
10+
class Api
11+
{
12+
/**
13+
* Create API object.
14+
*
15+
*/
16+
public function __construct() {}
17+
18+
/**
19+
* Get a passage from the NET Bible API.
20+
*
21+
* @param string $passage The passage to retrieve - multiple passages should be separated by a semi-colon.
22+
* @param string $type The return type - either 'text' (default) or 'json'.
23+
* @return mixed If $passage is a valid Bible reference, the NET Bible text of that passage.
24+
*/
25+
private function get(string $passage, string $type = "text"): mixed
26+
{
27+
// build URL from data
28+
$formatting = match($type) {
29+
"text" => "para",
30+
default => "plain"
31+
};
32+
$url = sprintf("https://labs.bible.org/api/?passage=%s&type=%s&formatting=%s", urlencode($passage), $type, $formatting);
33+
34+
// create curl request
35+
$handle = curl_init($url);
36+
if ($handle === false) {
37+
_l("Unable to create cURL request for %s.", $url);
38+
return null;
39+
}
40+
41+
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
42+
43+
// make request - on error log and return null
44+
$response = curl_exec($handle);
45+
if (!is_string($response)) {
46+
_l(print_r(curl_error($handle), true));
47+
return null;
48+
}
49+
50+
// if type is JSON, decode and return
51+
if ($type === "json") {
52+
$json = json_decode($response);
53+
if (!$json) {
54+
_l("Unable to decode JSON response from %s.", $url);
55+
return null;
56+
}
57+
58+
return $json;
59+
}
60+
61+
// otherwise return as text
62+
return $response;
63+
}
64+
65+
/**
66+
* Get Bible passage as JSON.
67+
*
68+
* @param string $passage The passage to retrieve - multiple passages should be separated by a semi-colon.
69+
* @return mixed If $passage is a valid Bible reference, the NET Bible text of that passage.
70+
*/
71+
public function get_json(string $passage): mixed
72+
{
73+
return $this->get($passage, "json");
74+
}
75+
76+
/**
77+
* Get Bible passage as (HTML-formatted) text.
78+
*
79+
* @param string $passage The passage to retrieve - multiple passages should be separated by a semi-colon.
80+
* @return ?string If $passage is a valid Bible reference, the NET Bible text of that passage.
81+
*/
82+
public function get_text(string $passage): ?string
83+
{
84+
return $this->get($passage, "text");
85+
}
86+
}

src/pages/about/about.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class About extends Endpoint
1212
{
1313
/**
14-
* GET: /
14+
* GET: /about
1515
*
1616
* @return View
1717
*/

src/pages/bible/bible.class.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Obadiah\Pages\Bible;
4+
5+
use Obadiah\App;
6+
use Obadiah\NetBible\Api;
7+
use Obadiah\Request\Request;
8+
use Obadiah\Response\View;
9+
use Obadiah\Router\Endpoint;
10+
11+
App::check();
12+
13+
class Bible extends Endpoint
14+
{
15+
/**
16+
* GET: /bible
17+
*
18+
* @return View
19+
*/
20+
public function index_get(): View
21+
{
22+
// get the requested passage reference
23+
$passage = Request::$get->string("passage", "");
24+
25+
// use the API to get the Bible text
26+
$netbible = new Api();
27+
$text = $netbible->get_text($passage);
28+
29+
// format the passage reference with uppercase words
30+
return new View("bible", model: new Index_Model(ucwords($passage), $text));
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Obadiah\Pages\Bible;
4+
5+
use Obadiah\App;
6+
7+
App::check();
8+
9+
class Index_Model
10+
{
11+
/**
12+
* Create Index model.
13+
*
14+
* @param @string $ref The Bible passage reference.
15+
* @param ?string $text The text of the requested Bible passage.
16+
*/
17+
public function __construct(
18+
public readonly ?string $ref,
19+
public readonly ?string $text
20+
) {}
21+
}

src/pages/bible/index-view.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Obadiah\Pages\Bible;
4+
5+
use Obadiah\App;
6+
use Obadiah\Pages\Bible\Index_Model;
7+
use Obadiah\Pages\Parts\Header\Header_Model;
8+
use Obadiah\Response\View;
9+
10+
App::check();
11+
12+
/** @var View $this */
13+
/** @var Index_Model $model */
14+
15+
// output header
16+
$this->header(new Header_Model("Bible", subtitle: "Access the Bible text using the New English Translation (NET)."));
17+
18+
?>
19+
20+
<div>
21+
<h2><?php _e($model->ref); ?></h2>
22+
<?php _h($model->text); ?>
23+
</div>
24+
25+
<?php
26+
27+
$this->footer();

src/pages/parts/header/nav-part.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"Home" => "/",
1919
"Rota" => "/rota",
2020
"Refresh" => "/refresh",
21+
"Bible" => "/bible",
2122
"About" => "/about",
2223
"Log Out" => "/auth/logout"
2324
);
@@ -27,9 +28,9 @@
2728
array("Upload" => "/upload"),
2829
array_slice($links, 1, 1),
2930
array("Prayer Calendar" => "/prayer"),
30-
array_slice($links, 2, 1),
31+
array_slice($links, 3, 1),
3132
array("Settings" => "/settings"),
32-
array_slice($links, 3),
33+
array_slice($links, 4),
3334
);
3435
}
3536
} else {

src/public/index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
Router::map_endpoint(A\Safeguarding\Safeguarding::class, uri_prefix: API_PREFIX);
1818

1919
// map pages
20+
Router::map_endpoint(P\About\About::class);
2021
Router::map_endpoint(P\Auth\Auth::class, requires_auth: false);
22+
Router::map_endpoint(P\Bible\Bible::class);
2123
Router::map_endpoint(P\Events\Events::class, requires_auth: false);
2224
Router::map_endpoint(P\Prayer\Prayer::class, requires_admin: true);
2325
Router::map_endpoint(P\Refresh\Refresh::class);
2426
Router::map_endpoint(P\Robots\Robots::class, uri_path: "robots.txt", requires_auth: false);
2527
Router::map_endpoint(P\Rota\Rota::class);
2628
Router::map_endpoint(P\Services\Services::class, requires_auth: false);
27-
Router::map_endpoint(P\Upload\Upload::class, requires_admin: true);
2829
Router::map_endpoint(P\Settings\Settings::class, requires_admin: true);
29-
Router::map_endpoint(P\About\About::class);
30+
Router::map_endpoint(P\Upload\Upload::class, requires_admin: true);
3031

3132
// get and execute page action
3233
$action = Router::get_action();

0 commit comments

Comments
 (0)