/** * Makes sure the parameter list and return value are cloned. */ public function __clone() { Arrays::cloneObjects($this->parameters); $this->returnValue = clone $this->returnValue; }
/** * Returns a list of actions on the given resource. * * @history * 2014.04.15: * (AT) Initial implementation * * @version 2014.04.15 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @param string $resource_id * Resource name * @throws \Cougar\Exceptions\NotFoundException * @return \Cougar\RestService\Models\Action[] * List of actions on resource */ public function getResourceActions($resource_id) { // Make sure the resource exists if (!array_key_exists($resource_id, $this->resources)) { throw new NotFoundException("Resource not found"); } $resource = $this->resources[$resource_id]; // Get a copy of the actions $actions = Arrays::cloneObjectArray($resource->actions); // Set the view to list Arrays::setModelView($actions, "list"); // Return the actions return $actions; }
/** * @covers \Cougar\Util\Arrays::cloneObjectArray */ public function testCloneObjectArrayMixedArray() { $object1 = new stdClass(); $object1->value = "Object 1"; $object2 = new stdClass(); $object2->value = "Object 2"; $objects = array($object1, $object2, "stuff"); $cloned_objects = Arrays::cloneObjectArray($objects); $cloned_objects[0]->value = "Cloned Object 1"; $cloned_objects[1]->value = "Cloned Object 2"; $this->assertNotEquals($object1->value, $cloned_objects[0]->value); $this->assertNotEquals($object2->value, $cloned_objects[1]->value); $this->assertEquals("stuff", $objects[2]); $this->assertEquals($object1->value, $objects[0]->value); $this->assertEquals($object2->value, $objects[1]->value); }
/** * Renames the keys in an array of associative arrays with extended * functionality. This is much like the renameKeys() method except that * it is recursive and the key map does not have to be complete. * * For example, if you wish to keep a key name the same, you do not need to * specify it in the map. Additionally, if you wish to remove a specific * array member, you may specify this by leaving the key's value empty in * the key map. * * @history * 2014.08.20 * (AT) Initial release * * @version 2014.08.20 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @param array $data Array on which to rename the keys * @param array $key_map Assoc. array mapping old key names to the new ones * @return array Re-keyed array */ public static function renameKeysExtended(array $data, array $key_map) { // Create the new array $array = array(); // Go through each element in the array foreach ($data as $key => &$value) { // See if the key is in the key map if (array_key_exists($key, $key_map)) { $key = $key_map[$key]; // See if the key will be skipped if (!$key) { continue; } } // See if the value is another array if (is_array($value)) { // Save the value with its new key name and its renamed keys $array[$key] = Arrays::renameKeysExtended($value, $key_map); } else { // Save the value with the new key $array[$key] = $value; } } // Return the new array return $array; }
/** * Returns a list of records with the given values * * @history * 2013.09.30: * (AT) Initial release * 2013.10.25: * (AT) Improve thrown exceptions * 2013.11.25: * (AT) Add support for _limit and _offset query parameters * (AT) Set default limit to 10,000 rows * 2014.02.27: * (AT) Fix bad logic when setting limit and offset on OCI * 2014.03.04: * (AT) Split function into a query-generating function and a query * execution part to make it easier to extend query functionality * 2014.03.05: * (AT) Handle queryUnique flag * 2014.03.06: * (AT) Rename array keys when using the OCI driver * 2014.03.18: * (AT) Make sure the method is still persistent * * @version 2014.03.18 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @param array $parameters * List of query parameters * @param string $class_name * Use array to return list as an array, or class name to return objects * @param array $ctorargs * Constructor arguments if returning objects * @return array Record list * @throws \Cougar\Exceptions\Exception; * @throws \Cougar\Exceptions\AccessDeniedException; */ public function query(array $parameters = array(), $class_name = "array", array $ctorargs = array()) { # Make sure we are still persistent if (!$this->__persistent) { throw new Exception("Querying is no longer allowed on this model"); } # See if querying is allowed if (!$this->__allowQuery) { throw new AccessDeniedException("This model does not support querying"); } # Set the view (if it hasn't been changed) if ($this->__currentView == "__default__" && $this->__queryView !== "__default__") { $this->__setView($this->__queryView); } # Extract the columns and aliases for the columns we can query $query_aliases = array_intersect($this->__alias, $this->__queryProperties); $columns = array(); $key_map = array(); foreach ($this->__queryProperties as $property) { if ($this->__visible[$property]) { if ($this->__exportAlias[$property] == $this->__columnMap[$property]) { $columns[$property] = $this->__columnMap[$property]; $key_map[$this->__columnMap[$property]] = $this->__columnMap[$property]; } else { $columns[$property] = $this->__columnMap[$property] . " AS " . $this->__exportAlias[$property]; $key_map[$this->__exportAlias[$property]] = $this->__exportAlias[$property]; } } } # Recursively iterate through the query parameters $this->iterateQueryParameters($parameters, $query_aliases, $columns, $key_map); # Prepare the array that will hold the parameter values $values = array(); # Set the default limit to 10,000 rows $limit = 10000; $offset = 0; $used_parameters = array(); # Prepare the query and execute the statement if ($this->__queryUnique) { $query = "SELECT DISTINCT "; } else { $query = "SELECT "; } $query .= implode(", ", $columns) . " FROM " . $this->__table . " " . implode(" ", $this->__joins); $where_clause = QueryParameter::toSql($parameters, $this->__columnMap, $query_aliases, $this->__caseInsensitive, $values, $used_parameters, $limit, $offset); if ($where_clause) { $query .= " WHERE " . $where_clause; } # Set the limit and offset $limit = (int) $limit; $offset = (int) $offset; if ($this->__pdo->getAttribute(PDO::ATTR_DRIVER_NAME) == "oci") { if ($where_clause) { $query .= " AND "; } else { $query .= " WHERE "; } $query .= "ROWNUM > " . $offset . " AND ROWNUM <= " . ($offset + $limit); } else { $query .= " LIMIT " . $limit . " OFFSET " . $offset; } # Execute the query $results = $this->executeQuery($query, $values, $class_name, $ctorargs); # Oracle will turn all column names as uppercase. This will rename them # if we are returning an array and are using OCI if ($class_name == "array" && count($results) > 0 && $this->__pdo->getAttribute(PDO::ATTR_DRIVER_NAME) == "oci") { # Change the keys in the key map to uppercase $key_map = array_change_key_case($key_map, CASE_UPPER); # Rename the keys $results = Arrays::renameKeys($results, $key_map); } # Return the result return $results; }
/** * Makes sure the resource list is cloned. */ public function __clone() { Arrays::cloneObjects($this->resources); }
/** * Makes sure the action list is cloned. */ public function __clone() { Arrays::cloneObjects($this->actions); }