Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Simplified some code #37

Closed
wants to merge 5 commits into from

2 participants

@alv-c

Simplified some code

@bauffman
Owner

Most of these 'simplifications' are done for a very distinct reason. I could go over them one by one if you'd like and explain why each piece of code was written that way :)

@bauffman bauffman closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
33 README.md
@@ -0,0 +1,33 @@
+A PHP5 library thats fast, easy to learn and fun!
+====
+[Spoon Library](http://www.spoon-library.com)
+
+###What is Spoon?
+Spoon is a [well documented][docs] PHP5 library used to build kickass web applications.
+
+###Why use Spoon?
+It's well [documented][docs], has a large set of basic components and is released under an [open source BSD license][license].
+
+###Ready to try Spoon on for size?
+Introduction to Spoon
+
+```php
+<?php
+// define location of your library
+define('PATH_LIBRARY', '/home/myUsername/library');
+
+// add this to the include path
+set_include_path(get_include_path() . PATH_SEPARATOR . PATH_LIBRARY);
+
+// require spoon
+require_once 'spoon/spoon.php';
+```
+
+[View all tutorials][tutos]
+
+###Who uses Spoon?
+Spoon is used by Fork CMS, VLM Buzz, Ancienne Belgique, Music Hall, Mobistar, Netlash, Bru, Explania, ...
+
+[docs]: http://www.spoon-library.com/docs
+[tutos]: http://www.spoon-library.com/tutorials
+[license]: http://www.spoon-library.com/license
View
35 spoon/cookie/cookie.php
@@ -43,10 +43,10 @@ public static function delete()
foreach($argument as $key)
{
// remove from array
- unset($_COOKIE[(string) $key]);
+ unset($_COOKIE[$key]);
// unset cookie
- setcookie((string) $key, null, 1);
+ setcookie( $key, null, 1);
}
}
@@ -54,10 +54,10 @@ public static function delete()
else
{
// remove from array
- unset($_COOKIE[(string) $argument]);
+ unset($_COOKIE[$argument]);
// unset cookie
- setcookie((string) $argument, null, 1);
+ setcookie($argument, null, 1);
}
}
}
@@ -80,7 +80,7 @@ public static function exists()
foreach($argument as $key)
{
// does NOT exist
- if(!isset($_COOKIE[(string) $key])) return false;
+ if(!isset($_COOKIE[$key])) return false;
}
}
@@ -88,7 +88,7 @@ public static function exists()
else
{
// does NOT exist
- if(!isset($_COOKIE[(string) $argument])) return false;
+ if(!isset($_COOKIE[($argument])) return false;
}
}
@@ -99,16 +99,20 @@ public static function exists()
/**
* Gets the value that was stored in a cookie.
*
- * @return mixed The value that was stored in the cookie.
+ * @return mixed The value that was stored in the cookie or the default value if it doesn't exists.
* @param string $key The name of the cookie that should be retrieved.
+ * @param mixed $default The default value to return if the cookie doesn't exists
*/
- public static function get($key)
+ public static function get($key, $default = false)
{
// redefine key
$key = (string) $key;
// cookie doesn't exist
- if(!self::exists($key)) return false;
+ if( ! self::exists($key) )
+ {
+ return $default;
+ }
// fetch base value
$value = (get_magic_quotes_gpc()) ? stripslashes($_COOKIE[$key]) : $_COOKIE[$key];
@@ -139,19 +143,12 @@ public static function get($key)
public static function set($key, $value, $time = 86400, $path = '/', $domain = null, $secure = false, $httpOnly = false)
{
// redefine
- $key = (string) $key;
$value = serialize($value);
- $time = time() + (int) $time;
- $path = (string) $path;
- $domain = ($domain !== null) ? (string) $domain : null;
- $secure = (bool) $secure;
- $httpOnly = (bool) $httpOnly;
+ $time += time();
// set cookie
- $cookie = setcookie($key, $value, $time, $path, $domain, $secure, $httpOnly);
-
- // problem occured
- return ($cookie === false) ? false : true;
+ // return the result (true if the cookie has been created, false otherwise)
+ return ($cookie = setcookie($key, $value, $time, $path, $domain, $secure, $httpOnly));
}
}
View
47 spoon/database/database.php
@@ -128,7 +128,7 @@ public function __construct($driver, $hostname, $username, $password, $database,
private function connect()
{
// not yet connected
- if(!$this->handler)
+ if( ! ($this->handler instanceof PDO) )
{
try
{
@@ -763,8 +763,51 @@ public function getVar($query, $parameters = array())
// fetch the var
return $statement->fetchColumn();
}
+
+ /**
+ * Inserts data to the database
+ *
+ * @param string $table The name of the table
+ * @param array $values Array with the values to insert
+ * @return array An array that contains the number of affected rows and the id of the insert
+ * @access public
+ */
+ public static function simple_insert($table, array $values)
+ {
+ if(count($values) == 0)
+ {
+ throw new SpoonDatabaseException('You need to provide values for an insert query.', 0, $this->password);
+ }
+
+ $stat = $this->handler->prepare('INSERT INTO `'.$table.'`(`'.implode('`,`', array_keys($values)).'`) VALUES(:'.implode(', :', array_keys($values)).')');
+
+ if($stat === false)
+ {
+ // get error
+ $errorInfo = $this->handler->errorInfo();
-
+ // throw exceptions
+ throw new SpoonDatabaseException($errorInfo[2]);
+ }
+
+ $arr = array();
+
+ foreach ($values as $column => $value)
+ {
+ $arr[':'.$column] = $value;
+ }
+
+ $affected = $stat->execute($arr);
+
+ if($stat->errorCode() != 0)
+ {
+ $aError = $stat->errorInfo();
+ throw new SpoonDatabaseException($aError[2]);
+ }
+
+ return array($affected, $this->handler->lastInsertId());
+
+ }
/**
* Inserts one or more records
*
View
23 spoon/file/file.php
@@ -144,20 +144,17 @@ public static function getContent($filename)
*/
public static function getExtension($filename, $lowercase = true)
{
- // init var
- $filename = ($lowercase) ? strtolower((string) $filename) : (string) $filename;
-
- // fetch extension
- $chunks = (array) explode('.', $filename);
-
- // count the chunks
- $count = count($chunks);
-
- // has an extension
- if($count != 0)
+
+ if ( strpos($filename, '.') )
{
- // extension can only have alphanumeric chars
- if(SpoonFilter::isAlphaNumeric($chunks[$count - 1])) return $chunks[$count - 1];
+ $chunks = explode('.', $filename);
+
+ $ext = end($chunks);
+
+ if ( SpoonFilter::isAlphaNumeric($ext))
+ {
+ return $ext;
+ }
}
// no extension
Something went wrong with that request. Please try again.