From 92b405570906ec0c45b8935b20adb31d0320afd8 Mon Sep 17 00:00:00 2001 From: oscarotero Date: Thu, 18 Aug 2016 17:12:05 +0200 Subject: [PATCH] File improvement --- README.md | 24 +++++++++++++++++++----- src/Fields/File.php | 35 ++++++++++++++++++++++++++++++++++- tests/UploadTest.php | 3 ++- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8c69c03..1433251 100644 --- a/README.md +++ b/README.md @@ -84,13 +84,23 @@ $post = $db->post; SimpleCrud load the database scheme and detects automatically all relationships between the tables using the naming conventions described above. For example the table "post" has a field called "category_id", so SimpleCrud knows that each post has one category. +**Note:** In production environment, you may want to cache the scheme in order to improve the performance. You can do it in this way: + +```php +if (!$cache->has('db_scheme')) { + $cache->save($db->getScheme()); +} else { + $db->setScheme($cache->get('db_scheme')); +} +``` + ## Using the library ### Basic CRUD: You can work directly with the tables to insert/update/delete/select data: -Use arrayAccess interface to access to the data using the `id`: +Use `ArrayAccess` interface to access to the data using the `id`: ```php //Get the post id = 3; @@ -123,7 +133,7 @@ A `Row` object represents a database row and it is used to read and modify the d //get a row $post = $db->post[34]; -//Get/set the post title +//Get/modify fields values echo $post->title; $post->title = 'New title'; @@ -263,7 +273,7 @@ $posts = $db->post ->run(); ``` -This allows make awesome (and dangerous :D) things like this: +This allows make things like this: ```php $titles = $db->post[34]->tag->post->title; @@ -274,14 +284,18 @@ $titles = $db->post[34]->tag->post->title; //And finally, the titles of all these posts ``` -You may want a filtered result of the related rows instead getting all of them. To do this, just use a method with the same name of the related table and to return the instance of `Select` that you can modify: +You may want a filtered result of the related rows instead getting all of them. To do this, just use a method with the same name of the related table and you get the of the `Select` query that you can modify: ```php $category = $db->category[34]; -//Get the posts of this category but only if the pubdate is in the future +//Magic property: Returns all posts of this category: +$posts = $category->post; + +//Magic method: Returns the query before run it $posts = $category->post() ->where('pubdate > :date', [':date' => date('Y-m-d')]) + ->limit(10) ->run(); ``` diff --git a/src/Fields/File.php b/src/Fields/File.php index 67239eb..5a1c15e 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -13,6 +13,35 @@ class File extends Field { protected $directory; + protected static $chars = [ + 'à' => 'a', 'á' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', + 'ă' => 'a', 'ằ' => 'a', 'ắ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ặ' => 'a', + 'â' => 'a', 'ầ' => 'a', 'ấ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ậ' => 'a', + 'À' => 'a', 'Á' => 'a', 'Ả' => 'a', 'Ã' => 'a', 'Ạ' => 'a', + 'Ă' => 'a', 'Ằ' => 'a', 'Ắ' => 'a', 'Ẳ' => 'a', 'Ẵ' => 'a', 'Ặ' => 'a', + 'Â' => 'a', 'Ầ' => 'a', 'Ấ' => 'a', 'Ẩ' => 'a', 'Ẫ' => 'a', 'Ậ' => 'a', + 'đ' => 'd', 'Đ' => 'd', + 'è' => 'e', 'é' => 'e', 'ẻ' => 'e', 'ẽ' => 'e', 'ẹ' => 'e', + 'ê' => 'e', 'ề' => 'e', 'ế' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ệ' => 'e', + 'È' => 'e', 'É' => 'e', 'Ẻ' => 'e', 'Ẽ' => 'e', 'Ẹ' => 'e', + 'Ê' => 'e', 'Ề' => 'e', 'Ế' => 'e', 'Ể' => 'e', 'Ễ' => 'e', 'Ệ' => 'e', + 'ì' => 'i', 'í' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i', + 'Ì' => 'i', 'Í' => 'i', 'Ỉ' => 'i', 'Ĩ' => 'i', 'Ị' => 'i', + 'ñ' => 'n', + 'Ñ' => 'n', + 'ò' => 'o', 'ó' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o', + 'ô' => 'o', 'ồ' => 'o', 'ố' => 'o', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o', + 'ơ' => 'o', 'ờ' => 'o', 'ớ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o', + 'Ò' => 'o', 'Ó' => 'o', 'Ỏ' => 'o', 'Õ' => 'o', 'Ọ' => 'o', + 'Ô' => 'o', 'Ồ' => 'o', 'Ố' => 'o', 'Ổ' => 'o', 'Ỗ' => 'o', 'Ộ' => 'o', + 'Ơ' => 'o', 'Ờ' => 'o', 'Ớ' => 'o', 'Ở' => 'o', 'Ỡ' => 'o', 'Ợ' => 'o', + 'ù' => 'u', 'ú' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u', + 'ư' => 'u', 'ừ' => 'u', 'ứ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u', + 'Ù' => 'u', 'Ú' => 'u', 'Ủ' => 'u', 'Ũ' => 'u', 'Ụ' => 'u', + 'Ư' => 'u', 'Ừ' => 'u', 'Ứ' => 'u', 'Ử' => 'u', 'Ữ' => 'u', 'Ự' => 'u', + 'ỳ' => 'y', 'ý' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y', + 'Y' => 'y', 'Ỳ' => 'y', 'Ý' => 'y', 'Ỷ' => 'y', 'Ỹ' => 'y', 'Ỵ' => 'y', + ]; /** * {@inheritdoc} @@ -23,6 +52,10 @@ public function dataToDatabase($data) return $this->upload($data); } + if ($data instanceof SplFileInfo) { + return $data->getFilename(); + } + return $data; } @@ -74,7 +107,7 @@ protected function getFilename(UploadedFileInterface $file) return uniqid(); } - return preg_replace(['/[^\w\.]/', '/[\-]{2,}/'], '-', strtolower($name)); + return preg_replace(['/[^\w\.]/', '/[\-]{2,}/'], '-', trim(strtolower(strtr($name, static::$chars)))); } /** diff --git a/tests/UploadTest.php b/tests/UploadTest.php index 0bd783c..6ec1ba8 100644 --- a/tests/UploadTest.php +++ b/tests/UploadTest.php @@ -36,7 +36,7 @@ public function testUpload() $file = $db->file->create([ 'name' => 'New file', - 'file' => new UploadedFile($stream, strlen($content), UPLOAD_ERR_OK, 'My file.txt'), + 'file' => new UploadedFile($stream, strlen($content), UPLOAD_ERR_OK, ' My fíle.txt'), ]); $file->save(); @@ -46,6 +46,7 @@ public function testUpload() $this->assertTrue($fileinfo->isFile()); $this->assertEquals(__DIR__.'/tmp/file/file/my-file.txt', $fileinfo->getPathname()); $this->assertEquals($content, $fileinfo->openFile()->fgets()); + $this->assertEquals('my-file.txt', $this->db->execute('SELECT file from file')->fetchColumn(0)); unlink(__DIR__.'/tmp/file/file/my-file.txt'); rmdir(__DIR__.'/tmp/file/file');