summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Cornutt <enygma@phpdeveloper.org>2015-01-23 18:18:49 +0100
committerChris Cornutt <enygma@phpdeveloper.org>2015-01-23 18:18:49 +0100
commitb62dca7dc845b1ee539762cf5782b3019090672c (patch)
treec74d796c57e0871ba706aeba6824a190b89ea21d
parenta3f5f588e914beb8f364f4f0bfde78404233f7b2 (diff)
parentcada5de00d1f0046e2ff9ffa4d85b04c96707296 (diff)
downloadgatekeeper-b62dca7dc845b1ee539762cf5782b3019090672c.zip
gatekeeper-b62dca7dc845b1ee539762cf5782b3019090672c.tar.gz
gatekeeper-b62dca7dc845b1ee539762cf5782b3019090672c.tar.bz2
Merge branch 'master' of git://github.com/psecio/gatekeeper
-rw-r--r--src/Psecio/Gatekeeper/DataSource/Mysql.php26
-rw-r--r--src/Psecio/Gatekeeper/Gatekeeper.php54
-rw-r--r--src/Psecio/Gatekeeper/UserModel.php3
3 files changed, 79 insertions, 4 deletions
diff --git a/src/Psecio/Gatekeeper/DataSource/Mysql.php b/src/Psecio/Gatekeeper/DataSource/Mysql.php
index 77de1d8..09bd40f 100644
--- a/src/Psecio/Gatekeeper/DataSource/Mysql.php
+++ b/src/Psecio/Gatekeeper/DataSource/Mysql.php
@@ -184,10 +184,12 @@ class Mysql extends \Psecio\Gatekeeper\DataSource
* Find records matching the "where" data given
* All "where" options are appended via "and"
*
+ * @param \Modler\Model $model Model instance
* @param array $where Data to use in "where" statement
+ * @param boolean $multiple Force return of single/multiple
* @return array Fetched data
*/
- public function find(\Modler\Model $model, array $where = array())
+ public function find(\Modler\Model $model, array $where = array(), $multiple = false)
{
$properties = $model->getProperties();
list($columns, $bind) = $this->setup($where);
@@ -200,11 +202,29 @@ class Mysql extends \Psecio\Gatekeeper\DataSource
$update[] = $column.' = '.$name;
}
- $sql = 'select * from '.$model->getTableName().' where '.implode(' and ', $update);
+ $sql = 'select * from '.$model->getTableName();
+ if (!empty($update)) {
+ $sql .= ' where '.implode(' and ', $update);
+ }
+
$result = $this->fetch($sql, $where);
- if ($result !== false && count($result) == 1) {
+ if ($result !== false && count($result) == 1 && $multiple === false) {
$model->load($result[0]);
+ return $model;
+ } elseif (count($result) > 1){
+ // Make a collection instead
+ $modelClass = get_class($model);
+ $collectionNs = str_replace('Model', 'Collection', $modelClass);
+ if (!class_exists($collectionNs)) {
+ throw new \InvalidArgumentException('Collection "'.$collectionNs.'" is invalid!');
+ }
+ $collection = new $collectionNs($this);
+ foreach ($result as $item) {
+ $itemModel = new $modelClass($this, $item);
+ $collection->add($itemModel);
+ }
+ return $collection;
}
return $model;
}
diff --git a/src/Psecio/Gatekeeper/Gatekeeper.php b/src/Psecio/Gatekeeper/Gatekeeper.php
index b203236..c7fd202 100644
--- a/src/Psecio/Gatekeeper/Gatekeeper.php
+++ b/src/Psecio/Gatekeeper/Gatekeeper.php
@@ -75,6 +75,16 @@ class Gatekeeper
}
/**
+ * Get the current datasource
+ *
+ * @return \Psecio\Gatekeeper\DataSource instance
+ */
+ public static function getDatasource()
+ {
+ return self::$datasource;
+ }
+
+ /**
* Load the variables using the .env handling
*
* @param string $envPath Path to the .env file
@@ -271,6 +281,25 @@ class Gatekeeper
$name = str_replace($action, '', $name);
preg_match('/By(.+)/', $name, $matches);
+ if (empty($matches) && strtolower(substr($name, -1)) === 's') {
+ return self::handleFindByMultiple($name, $args, $matches);
+ } else {
+ return self::handleFindBySingle($name, $args, $matches);
+ }
+
+ return $instance;
+ }
+
+ /**
+ * Handle the "find by" when a single record is requested
+ *
+ * @param string $name Name of function called
+ * @param array $args Arguments list
+ * @param array $matches Matches from regex
+ * @return \Modler\Collection collection
+ */
+ public static function handleFindBySingle($name, $args, $matches)
+ {
$property = lcfirst($matches[1]);
$model = str_replace($matches[0], '', $name);
$data = array($property => $args[0]);
@@ -280,7 +309,7 @@ class Gatekeeper
throw new Exception\ModelNotFoundException('Model type '.$model.' could not be found');
}
$instance = new $modelNs(self::$datasource);
- $instance = self::$datasource->$action($instance, $data);
+ $instance = self::$datasource->find($instance, $data);
if ($instance->id === null) {
$exception = '\\Psecio\\Gatekeeper\\Exception\\'.$model.'NotFoundException';
@@ -291,6 +320,29 @@ class Gatekeeper
}
/**
+ * Handle the "find by" when multiple are requested
+ *
+ * @param string $name Name of function called
+ * @param array $args Arguments list
+ * @param array $matches Matches from regex
+ * @return \Modler\Collection collection
+ */
+ public static function handleFindByMultiple($name, $args, $matches)
+ {
+ $data = (isset($args[0])) ? $args[0] : array();
+ $model = substr($name, 0, strlen($name) - 1);
+ $collectionNs = '\\Psecio\\Gatekeeper\\'.$model.'Collection';
+ if (!class_exists($collectionNs)) {
+ throw new Exception\ModelNotFoundException('Collection type '.$model.' could not be found');
+ }
+ $model = self::modelFactory($model.'Model');
+ $collection = new $collectionNs(self::$datasource);
+ $collection = self::$datasource->find($model, $data, true);
+
+ return $collection;
+ }
+
+ /**
* Handle the calls to create a new instance
*
* @param string $name Function name
diff --git a/src/Psecio/Gatekeeper/UserModel.php b/src/Psecio/Gatekeeper/UserModel.php
index 9f68d4e..75607f4 100644
--- a/src/Psecio/Gatekeeper/UserModel.php
+++ b/src/Psecio/Gatekeeper/UserModel.php
@@ -287,6 +287,9 @@ class UserModel extends \Psecio\Gatekeeper\Model\Mysql
'group_id' => $groupId,
'user_id' => $this->id
));
+ if ($userGroup->id === null) {
+ return false;
+ }
return ($userGroup->id !== null && $userGroup->groupId === $groupId) ? true : false;
}