Backend for songs.zachdecook.com
validation: use bible passage parser to validate verse references
Zach DeCook 2022-08-27
parent 4f1afd6 · commit c4e645d
-rw-r--r--laravel/app/Http/Controllers/SongController.php6
-rw-r--r--laravel/app/Song.php12
-rw-r--r--laravel/composer.json3
-rw-r--r--laravel/composer.lock55
4 files changed, 72 insertions, 4 deletions
diff --git a/laravel/app/Http/Controllers/SongController.php b/laravel/app/Http/Controllers/SongController.php
index 1a0ec18..2ad3f75 100644
--- a/laravel/app/Http/Controllers/SongController.php
+++ b/laravel/app/Http/Controllers/SongController.php
@@ -263,7 +263,11 @@ class SongController extends Controller
return redirect("/s/{$song->id}/edit")->withErrors(['key' => $e->getMessage()])->withInput();
}
$song->text = $_POST['text'];//request->text strips whitespace.
- $song->verse = $request->verse;
+ try {
+ $song->verse = $request->verse;
+ } catch (\Exception $e) {
+ return redirect("/s/{$song->id}/edit")->withErrors(['verse' => $e->getMessage()])->withInput();
+ }
$song->save();
return redirect()->route( 'song.show', [
'song' => $song,
diff --git a/laravel/app/Song.php b/laravel/app/Song.php
index a6cb0c0..0f7248a 100644
--- a/laravel/app/Song.php
+++ b/laravel/app/Song.php
@@ -4,6 +4,7 @@ namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Http\Controllers\SongController;
+use TechWilk\BibleVerseParser\BiblePassageParser;
class Song extends Model
{
@@ -34,7 +35,7 @@ class Song extends Model
public function setKeyAttribute($value)
{
- if (!$value) return;
+ if (!$value) return $this->attributes['key'] = null;
if ($value[0]>="A"&&$value[0]<="G"){
$this->attributes['key'] = $value;
} else {
@@ -65,6 +66,15 @@ class Song extends Model
file_put_contents("public/text/{$this->id}.txt", $text);
}
}
+
+ public function setVerseAttribute($text)
+ {
+ if (!$text) return $this->attributes['verse'] = null;
+ $parser = new BiblePassageParser();
+ // Can throw an exception.
+ return $this->attributes['verse'] = implode("; ", $parser->parse($text));
+ }
+
public function getChordsAttribute($txt = null)
{
$txt = $txt ?? $this->text;
diff --git a/laravel/composer.json b/laravel/composer.json
index d0a9226..47d716b 100644
--- a/laravel/composer.json
+++ b/laravel/composer.json
@@ -12,7 +12,8 @@
"fideloper/proxy": "^4.0",
"laravel/framework": "^8.0",
"laravel/tinker": "^2.0",
- "laravel/ui": "^3.0"
+ "laravel/ui": "^3.0",
+ "techwilk/bible-verse-parser": "^2.2"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
diff --git a/laravel/composer.lock b/laravel/composer.lock
index 1e438ae..22b53c8 100644
--- a/laravel/composer.lock
+++ b/laravel/composer.lock
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "3b907af1a107655604e8aa0c881963c0",
+ "content-hash": "4fda3badf71ea48c224b53d18e783bf9",
"packages": [
{
"name": "brick/math",
@@ -4520,6 +4520,59 @@
"time": "2022-07-20T13:00:38+00:00"
},
{
+ "name": "techwilk/bible-verse-parser",
+ "version": "2.2.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/TechWilk/bible-verse-parser.git",
+ "reference": "9edb9a94ac44cb0e4a2815159280c7375cc91f0c"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/TechWilk/bible-verse-parser/zipball/9edb9a94ac44cb0e4a2815159280c7375cc91f0c",
+ "reference": "9edb9a94ac44cb0e4a2815159280c7375cc91f0c",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=7.4"
+ },
+ "require-dev": {
+ "php-coveralls/php-coveralls": "^2.4",
+ "phpunit/phpunit": "^9.4"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "TechWilk\\BibleVerseParser\\": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Christopher Wilkinson",
+ "email": "c@wilk.tech",
+ "homepage": "https://wilk.tech"
+ }
+ ],
+ "description": "Parse verse textual representation into book/chapter/verse ranges",
+ "homepage": "http://github.com/techwilk/bible-verse-parser",
+ "keywords": [
+ "Passage",
+ "bible",
+ "parse",
+ "scripture",
+ "verse"
+ ],
+ "support": {
+ "issues": "https://github.com/TechWilk/bible-verse-parser/issues",
+ "source": "https://github.com/TechWilk/bible-verse-parser/tree/2.2.0"
+ },
+ "time": "2022-05-15T20:03:15+00:00"
+ },
+ {
"name": "tijsverkoyen/css-to-inline-styles",
"version": "2.2.4",
"source": {