Skip to content

Commit

Permalink
Merge branch 'develop' into release-2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
morrelinko committed Jan 28, 2014
2 parents 28885d6 + fac1f12 commit a5aefcb
Show file tree
Hide file tree
Showing 29 changed files with 1,017 additions and 826 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
.idea/
vendor/
build/
composer.lock
83 changes: 79 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,94 @@ $dataStore = new SqliteDataStore(['database' => 'photo_app.db']);
$simplePhoto = new SimplePhoto($storageManager, $dataStore);
```

## Retrieving photos (+Transformation)
## Get photos (+Transformation)

If you want to get a re-sized photo, use the "transform" options of the second argument

```php
$photo = $simplePhoto->get($photoId, [
"transform" => [
"size" => [200, 200]
'transform' => [
'size' => [200, 200]
]
]);
```

Other transformation options will be made available...
All transformation options available...
```php
[
'size' => [100, 100]
'rotate' => [45]
]
```

## Collection of photos

```php
$photos = $simplePhoto->collection([2, 23, 15]);

$photos->get(0); // gets photo '2'
$photos->get(1); // gets photo '23'

```

PhotoCollection come with a handful of methods for manipulating its items

```php
// Creates a collection of photos
$photos = $simplePhoto->collection([2, 23, 15, 34, 21, 1, 64, 324]);

// Gets all as array
$allPhotos = $photos->all();

// Uses filter() method.
// This example creates a new photo collection containing only photos in 'local' storage
$localPhotos = $photos->filter(function($photo) {
return $photo->storage() == 'local';
});

var_dump($localPhotos);
```

## Push (in english 'push photo result into')

```php
// Probably gotten from a db
$users = [
['user_id' => 1, 'name' => 'John Doe', 'photo_id' => 2],
['user_id' => 2, 'name' => 'Mary Alice', 'photo_id' => 5]
];

$simplePhoto->push($users, array('photo_id'));

var_dump($users);

// Sample Output:
[
['user_id' => 1, 'name' => 'John Doe', 'photo_id' => 2, 'photo' => (Object SimplePhoto\PhotoResult)],
['user_id' => 2, 'name' => 'Mary Alice', 'photo_id' => 5, 'photo' => (Object SimplePhoto\PhotoResult)]
];

```

If you would like complete control on what is pushed to the array from the photo result,

you specify a callback as third argument to `push()`

```php

$simplePhoto->push($users, array('photo_id'), function(&$item, $photo, $index, $name) {
$item['photo_url'] = $photo->url();
});

var_dump($users);

// Sample Output:
[
['user_id' => 1, 'name' => 'John Doe', 'photo_id' => 2, 'photo_url' => 'http://example.com/files/2014/xxxxx.png'],
['user_id' => 2, 'name' => 'Mary Alice', 'photo_id' => 5, 'photo_url' => 'http://example.com/files/2014/xxxxx.png']
];

```

## Credits

Expand Down
1 change: 0 additions & 1 deletion build/VERSION

This file was deleted.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
}
},
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"imagine/imagine": "~0.5.0"
},
"require-dev": {
"mockery/mockery": "dev-master@dev"
Expand Down
Binary file added example/standalone/files/defaults/not_found.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 97 additions & 14 deletions example/standalone/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once '../../autoload.php';
require_once __DIR__ . '/../../vendor/autoload.php';

// (Basic) Setup (this must have been done somewhere in your script so that it can be reused throughout your app)
$storageManager = new \SimplePhoto\StorageManager();
Expand All @@ -16,7 +16,7 @@
));

$dataStore->getConnection()->exec("
CREATE TABLE IF NOT EXISTS photos (
CREATE TABLE IF NOT EXISTS photo (
photo_id INTEGER PRIMARY KEY,
storage_name TEXT NOT NULL,
file_name TEXT NOT NULL,
Expand All @@ -41,11 +41,11 @@
echo '<img src="' . $simplePhoto->get($photoId)->url() . '" />';
}

$statement = $dataStore->getConnection()->prepare('SELECT * FROM photos');
$statement = $dataStore->getConnection()->prepare('SELECT * FROM photo');
$statement->execute();

foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $photo) {
var_dump($simplePhoto->get($photo['photo_id']));
// var_dump($simplePhoto->get($photo['photo_id']));
}

// Delete Photo
Expand All @@ -56,16 +56,99 @@

// Get a photo Resized

/**
* if ($resize = $simplePhoto->get(1, array(
* 'transform' => array(
* 'size' => array(200, 200)
* )
* ))
* ) {
* echo '<img src="' . $resize->url() . '" />';
* }
* /**/
/**/
if ($resize = $simplePhoto->get(1, array(
'transform' => array(
'size' => array(100, 100),
'rotate' => array(180)
)
))
) {
echo '<img src="' . $resize->url() . '" />';
}

//var_dump($resize);
/**/

$photos = $simplePhoto->collection([3, 2, 1, 4, 5], [
'fallback' => 'not_found.png'
]);
// var_dump($photos);

$data = array(
'user_id' => 4,
'username' => 'morrelinko',
'photo_id' => 1,
'cover_photo_id' => 2
);

$data2 = array(
'user_id' => 4,
'username' => 'morrelinko',
'photo_id' => 1,
);

$data3 = array(
array(
'user_id' => 4,
'username' => 'morrelinko',
'photo_id' => 1,
),
array(
'user_id' => 4,
'username' => 'morrelinko',
'photo_id' => 1,
),
array(
'user_id' => 4,
'username' => 'morrelinko',
'photo_id' => 1,
)
);

$callback = function (&$item, $photo, $index, $name) {
if ($index == 'photo_id') {
$item['user_photo'] = $photo->url();
} elseif ($index == 'cover_photo_id') {
$item['cover_photo'] = $photo->url();
}
};

$options = array('fallback' => 'not_found.png');

$simplePhoto->push(
$data,
array('photo_id', 'cover_photo_id'),
null,
$options
);


$simplePhoto->push($data2, array(), function ($item, $photo) {
/** @var $photo SimplePhoto\PhotoResult */
$item['photo_url'] = $photo->url();
});

$simplePhoto->push($data3, array('photo_id'), null, $options);

//var_dump($data);
//var_dump($data2);
var_dump($data3);

$localPhotos = $photos->filter(function ($photo) {
/** @var $photo SimplePhoto\PhotoResult */
return $photo->storage() == 'local';
});

$notFoundPhotos = $photos->filter(function ($photo) {
/** @var $photo SimplePhoto\PhotoResult */
return $photo->storage() == \SimplePhoto\StorageManager::FALLBACK_STORAGE;
});

// var_dump($photos);

// var_dump($localPhotos);
//var_dump($notFoundPhotos);
?>

<form method="post" enctype="multipart/form-data">
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./test/bootstrap.php">
bootstrap="./tests/bootstrap.php">

<testsuites>
<testsuite name="SimplePhoto Test Suit">
<directory phpVersion="5.3.0">./test/SimplePhoto</directory>
<directory phpVersion="5.3.0">./tests/SimplePhoto</directory>
</testsuite>
</testsuites>

Expand Down
7 changes: 7 additions & 0 deletions src/SimplePhoto/DataStore/DataStoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public function addPhoto(array $values);
*/
public function getPhoto($photoId);

/**
* @param array $photoIds
*
* @return mixed
*/
public function getPhotos(array $photoIds);

/**
* @param int $photoId
*
Expand Down
60 changes: 52 additions & 8 deletions src/SimplePhoto/DataStore/PdoConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class PdoConnection
public function __construct($connection, array $options = array())
{
$this->options = array_merge(array(
'photo_table' => 'photos'
'photo_table' => 'photo'
), $options);

if (!$connection instanceof \PDO) {
Expand All @@ -61,6 +61,11 @@ public function __construct($connection, array $options = array())
$this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}

/**
* @param array $connection
*
* @return mixed
*/
abstract public function createConnection($connection);

/**
Expand All @@ -76,14 +81,18 @@ public function getConnection()
*/
public function addPhoto(array $values)
{
$statement = $this->db->prepare(sprintf('
$sql = '
INSERT INTO %s (
storage_name, file_name, file_extension, file_path, file_mime
)
VALUES (
:storageName, :fileName, :fileExtension, :filePath, :fileMime
)
', $this->options['photo_table']));
';

$statement = $this->db->prepare(
sprintf($sql, $this->options['photo_table'])
);

$statement->execute($values);

Expand All @@ -95,13 +104,17 @@ public function addPhoto(array $values)
*/
public function getPhoto($photoId)
{
$statement = $this->db->prepare(sprintf('
$sql = '
SELECT
photo_id, storage_name, file_name, file_path,
file_extension, file_mime, created_at, updated_at
FROM %s
WHERE photo_id = :photoId
', $this->options['photo_table']));
';

$statement = $this->db->prepare(
sprintf($sql, $this->options['photo_table'])
);

$statement->execute(compact('photoId'));

Expand All @@ -112,16 +125,47 @@ public function getPhoto($photoId)
return array();
}

/**
* {@inheritDocs}
*/
public function getPhotos(array $photoIds)
{
$ids = str_repeat('?, ', count($photoIds)) . '?';
$sql = '
SELECT
photo_id, storage_name, file_name, file_path,
file_extension, file_mime, created_at, updated_at
FROM %s
WHERE photo_id IN (' . $ids . ')
';

$statement = $this->db->prepare(
sprintf($sql, $this->options['photo_table'])
);

$statement->execute($photoIds);

if ($photos = $statement->fetchAll(\PDO::FETCH_ASSOC)) {
return $photos;
}

return array();
}

/**
* {@inheritDocs}
*/
public function deletePhoto($photoId)
{
// Delete from database
$statement = $this->db->prepare(sprintf('
$sql = '
DELETE FROM %s
WHERE photo_id = :photoId
', $this->options['photo_table']));
';

// Delete from database
$statement = $this->db->prepare(
sprintf($sql, $this->options['photo_table'])
);

return $statement->execute(compact('photoId'));
}
Expand Down

0 comments on commit a5aefcb

Please sign in to comment.