Skip to content

Commit

Permalink
File improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Aug 18, 2016
1 parent 000f245 commit 92b4055
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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();
```

Expand Down
35 changes: 34 additions & 1 deletion src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -23,6 +52,10 @@ public function dataToDatabase($data)
return $this->upload($data);
}

if ($data instanceof SplFileInfo) {
return $data->getFilename();
}

return $data;
}

Expand Down Expand Up @@ -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))));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
Expand Down

0 comments on commit 92b4055

Please sign in to comment.