/** * Validate that the provided value is a valid integer. * * @since 1.0 */ public function testIsInteger() { $this->assertTrue(Validator::isInteger(100)); $this->assertTrue(Validator::isInteger(-100)); $this->assertTrue(Validator::isInteger(0)); $this->assertTrue(Validator::isInteger(00)); $this->assertTrue(Validator::isInteger('00000000008')); $this->assertTrue(Validator::isInteger('100')); $this->assertFalse(Validator::isInteger('1.1')); $this->assertFalse(Validator::isInteger(1.1)); $this->assertFalse(Validator::isInteger('twenty')); }
/** * Handle GET requests. * * @param Alpha\Util\Http\Request $request * * @since 1.0 * * @throws Alpha\Exception\ResourceNotFoundException */ public function doGET($request) { self::$logger->debug('>>doGET($request=[' . var_export($request, true) . '])'); $config = ConfigProvider::getInstance(); $params = $request->getParams(); try { if (isset($params['articleOID']) && isset($params['filename'])) { if (!Validator::isInteger($params['articleOID'])) { throw new IllegalArguementException('The articleOID [' . $params['articleOID'] . '] provided is invalid'); } $article = new Article(); $article->setOID($params['articleOID']); $filePath = $article->getAttachmentsLocation() . '/' . $params['filename']; if (file_exists($filePath)) { self::$logger->info('Downloading the file [' . $params['filename'] . '] from the folder [' . $article->getAttachmentsLocation() . ']'); $pathParts = pathinfo($filePath); $mimeType = FileUtils::getMIMETypeByExtension($pathParts['extension']); $response = new Response(200, file_get_contents($filePath)); $response->setHeader('Content-Type', $mimeType); $response->setHeader('Content-Disposition', 'attachment; filename="' . $pathParts['basename'] . '"'); $response->setHeader('Content-Length', filesize($filePath)); self::$logger->debug('<<doGET'); return $response; } else { self::$logger->error('Could not access article attachment file [' . $filePath . '] as it does not exist!'); throw new IllegalArguementException('File not found'); } } else { self::$logger->error('Could not access article attachment as articleOID and/or filename were not provided!'); throw new IllegalArguementException('File not found'); } } catch (IllegalArguementException $e) { self::$logger->error($e->getMessage()); throw new ResourceNotFoundException($e->getMessage()); } self::$logger->debug('<<doGET'); }
/** * Handle GET requests. * * @param Alpha\Util\Http\Request $request * * @throws Alpha\Exception\ResourceNotFoundException * @throws Alpha\Exception\IllegalArguementException * * @return Alpha\Util\Http\Response * * @since 2.0 */ public function doGET($request) { self::$logger->debug('>>doGET(request=[' . var_export($request, true) . '])'); $config = ConfigProvider::getInstance(); $params = $request->getParams(); $accept = $request->getAccept(); $body = ''; try { // get a single record if (isset($params['ActiveRecordType']) && isset($params['ActiveRecordOID'])) { if (!Validator::isInteger($params['ActiveRecordOID'])) { throw new IllegalArguementException('Invalid oid [' . $params['ActiveRecordOID'] . '] provided on the request!'); } $ActiveRecordType = urldecode($params['ActiveRecordType']); if (class_exists($ActiveRecordType)) { $record = new $ActiveRecordType(); } else { throw new IllegalArguementException('No ActiveRecord available to view!'); } // set up the title and meta details if (isset($params['view']) && $params['view'] == 'edit') { if (!isset($this->title)) { $this->setTitle('Editing a ' . $record->getFriendlyClassName()); } if (!isset($this->description)) { $this->setDescription('Page to edit a ' . $record->getFriendlyClassName() . '.'); } if (!isset($this->keywords)) { $this->setKeywords('edit,' . $record->getFriendlyClassName()); } } else { if (!isset($this->title)) { $this->setTitle('Viewing a ' . $record->getFriendlyClassName()); } if (!isset($this->description)) { $this->setDescription('Page to view a ' . $record->getFriendlyClassName() . '.'); } if (!isset($this->keywords)) { $this->setKeywords('view,' . $record->getFriendlyClassName()); } } $record->load($params['ActiveRecordOID']); ActiveRecord::disconnect(); $view = View::getInstance($record, false, $accept); $body .= View::displayPageHead($this); $message = $this->getStatusMessage(); if (!empty($message)) { $body .= $message; } $body .= View::renderDeleteForm($request->getURI()); if (isset($params['view']) && $params['view'] == 'edit') { $fields = array('formAction' => $this->request->getURI()); $body .= $view->editView($fields); } else { $body .= $view->detailedView(); } } elseif (isset($params['ActiveRecordType']) && isset($params['start'])) { // list all records of this type $ActiveRecordType = urldecode($params['ActiveRecordType']); if (class_exists($ActiveRecordType)) { $record = new $ActiveRecordType(); } else { throw new IllegalArguementException('No ActiveRecord available to view!'); } // set up the title and meta details if (!isset($this->title)) { $this->setTitle('Listing all ' . $record->getFriendlyClassName()); } if (!isset($this->description)) { $this->setDescription('Listing all ' . $record->getFriendlyClassName()); } if (!isset($this->keywords)) { $this->setKeywords('list,all,' . $record->getFriendlyClassName()); } if (isset($this->filterField) && isset($this->filterValue)) { if (isset($this->sort) && isset($this->order)) { $records = $record->loadAllByAttribute($this->filterField, $this->filterValue, $params['start'], $params['limit'], $this->sort, $this->order); } else { $records = $record->loadAllByAttribute($this->filterField, $this->filterValue, $params['start'], $params['limit']); } $this->recordCount = $record->getCount(array($this->filterField), array($this->filterValue)); } else { if (isset($this->sort) && isset($this->order)) { $records = $record->loadAll($params['start'], $params['limit'], $this->sort, $this->order); } else { $records = $record->loadAll($params['start'], $params['limit']); } $this->recordCount = $record->getCount(); } ActiveRecord::disconnect(); $view = View::getInstance($record, false, $accept); $body .= View::displayPageHead($this); $message = $this->getStatusMessage(); if (!empty($message)) { $body .= $message; } $body .= View::renderDeleteForm($this->request->getURI()); foreach ($records as $record) { $view = View::getInstance($record, false, $accept); $fields = array('formAction' => $this->request->getURI()); $body .= $view->listView($fields); } if ($accept == 'application/json') { $body = rtrim($body, ','); } } elseif (isset($params['ActiveRecordType'])) { // create a new record of this type $ActiveRecordType = urldecode($params['ActiveRecordType']); if (class_exists($ActiveRecordType)) { $record = new $ActiveRecordType(); } else { throw new IllegalArguementException('No ActiveRecord available to create!'); } // set up the title and meta details if (!isset($this->title)) { $this->setTitle('Create a new ' . $record->getFriendlyClassName()); } if (!isset($this->description)) { $this->setDescription('Create a new ' . $record->getFriendlyClassName() . '.'); } if (!isset($this->keywords)) { $this->setKeywords('create,new,' . $record->getFriendlyClassName()); } $view = View::getInstance($record, false, $accept); $body .= View::displayPageHead($this); $fields = array('formAction' => $this->request->getURI()); $body .= $view->createView($fields); } else { throw new IllegalArguementException('No ActiveRecord available to display!'); } } catch (IllegalArguementException $e) { self::$logger->warn($e->getMessage()); throw new ResourceNotFoundException('The record that you have requested cannot be found!'); } catch (RecordNotFoundException $e) { self::$logger->warn($e->getMessage()); throw new ResourceNotFoundException('The record that you have requested cannot be found!'); } $body .= View::displayPageFoot($this); self::$logger->debug('<<doGET'); return new Response(200, $body, array('Content-Type' => $accept == 'application/json' ? 'application/json' : 'text/html')); }
/** * Setter for the value (OID of related object) of this relation. * * @param int $val * * @since 1.0 * * @throws Alpha\Exception\IllegalArguementException */ public function setValue($val) { if (empty($val)) { $this->value = null; } else { if (!Validator::isInteger($val)) { throw new IllegalArguementException("[{$val}]" . $this->helper); } if (mb_strlen($val) <= $this->size) { $this->value = str_pad($val, 11, '0', STR_PAD_LEFT); } else { throw new IllegalArguementException("[{$val}]" . $this->helper); } } }
/** * Dynamically binds all of the attributes for the current BO to the supplied prepared statement * parameters. If arrays of attribute names and values are provided, only those will be bound to * the supplied statement. * * @param mysqli_stmt $stmt The SQL statement to bind to. * @param array Optional array of BO attributes. * @param array Optional array of BO values. * * @return mysqli_stmt * * @since 1.1 */ private function bindParams($stmt, $attributes = array(), $values = array()) { self::$logger->debug('>>bindParams(stmt=[' . var_export($stmt, true) . '])'); $bindingsTypes = ''; $params = array(); // here we are only binding the supplied attributes if (count($attributes) > 0 && count($attributes) == count($values)) { $count = count($values); for ($i = 0; $i < $count; ++$i) { if (Validator::isInteger($values[$i])) { $bindingsTypes .= 'i'; } else { $bindingsTypes .= 's'; } array_push($params, $values[$i]); } if ($this->BO->isTableOverloaded()) { if (isset($this->classname)) { $bindingsTypes .= 's'; array_push($params, $this->classname); } else { $bindingsTypes .= 's'; array_push($params, get_class($this->BO)); } } } else { // bind all attributes on the business object // get the class attributes $reflection = new ReflectionClass(get_class($this->BO)); $properties = $reflection->getProperties(); foreach ($properties as $propObj) { $propName = $propObj->name; if (!in_array($propName, $this->BO->getTransientAttributes())) { // Skip the OID, database auto number takes care of this. if ($propName != 'OID' && $propName != 'version_num') { if ($this->BO->getPropObject($propName) instanceof Integer) { $bindingsTypes .= 'i'; } else { $bindingsTypes .= 's'; } array_push($params, $this->BO->get($propName)); } if ($propName == 'version_num') { $temp = $this->BO->getVersionNumber()->getValue(); $this->BO->set('version_num', $temp + 1); $bindingsTypes .= 'i'; array_push($params, $this->BO->getVersionNumber()->getValue()); } } } if ($this->BO->isTableOverloaded()) { if (isset($this->classname)) { $bindingsTypes .= 's'; array_push($params, $this->classname); } else { $bindingsTypes .= 's'; array_push($params, get_class($this->BO)); } } // the OID may be on the WHERE clause for UPDATEs and DELETEs if (!$this->BO->isTransient()) { $bindingsTypes .= 'i'; array_push($params, $this->BO->getOID()); } } self::$logger->debug('bindingsTypes=[' . $bindingsTypes . '], count: [' . mb_strlen($bindingsTypes) . ']'); self::$logger->debug('params [' . var_export($params, true) . ']'); if ($params != null) { $bind_names[] = $bindingsTypes; $count = count($params); for ($i = 0; $i < $count; ++$i) { $bind_name = 'bind' . $i; ${$bind_name} = $params[$i]; $bind_names[] =& ${$bind_name}; } call_user_func_array(array($stmt, 'bind_param'), $bind_names); } self::$logger->debug('<<bindParams [' . var_export($stmt, true) . ']'); return $stmt; }
/** * Setter for the Integer value. * * @param int $val * * @since 1.0 * * @throws Alpha\Exception\IllegalArguementException */ public function setValue($val) { if (!Validator::isInteger($val)) { throw new IllegalArguementException($this->helper); } if (mb_strlen($val) <= $this->size) { $this->value = $val; } else { throw new IllegalArguementException($this->helper); } }