diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/functions.php | 132 | ||||
-rw-r--r-- | core/include/feed/main.php | 8 | ||||
-rw-r--r-- | core/include/page/main.php | 8 | ||||
-rw-r--r-- | core/include/post/main.php | 8 | ||||
-rw-r--r-- | core/include/user/main.php | 6 | ||||
-rw-r--r-- | core/namespace/Attribute.php | 11 | ||||
-rw-r--r-- | core/namespace/HTTP.php | 67 |
7 files changed, 82 insertions, 158 deletions
diff --git a/core/functions.php b/core/functions.php index ccaf008..35faed4 100644 --- a/core/functions.php +++ b/core/functions.php @@ -2,16 +2,18 @@ #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generatePageNaviTemplate($currentSite): Template\Template { +function generateNaviTemplate(int $current, $location, $namespace): Template\Template { $Database = Application::getDatabase(); - $Statement = $Database->query(sprintf('SELECT COUNT(id) FROM %s', Page\Attribute::TABLE)); + $Attribute = "{$namespace}\\Attribute"; - $lastSite = ceil($Statement->fetchColumn() / Application::get('PAGE.LIST_SIZE')); + $Statement = $Database->query(sprintf('SELECT COUNT(id) FROM %s', $Attribute::TABLE)); + + $lastSite = ceil($Statement->fetchColumn() / Application::get(strtoupper($namespace).'.LIST_SIZE')); $PaginationTemplate = Template\Factory::build('pagination'); - $PaginationTemplate->set('THIS', $currentSite); + $PaginationTemplate->set('THIS', $current); $PaginationTemplate->set('LAST', $lastSite); - $PaginationTemplate->set('HREF', Application::getPageURL('?site=%d')); + $PaginationTemplate->set('HREF', "{$location}?site=%d"); return $PaginationTemplate; } @@ -19,35 +21,22 @@ function generatePageNaviTemplate($currentSite): Template\Template { #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generatePostNaviTemplate($currentSite): Template\Template { - $Database = Application::getDatabase(); - $Statement = $Database->query(sprintf('SELECT COUNT(id) FROM %s', Post\Attribute::TABLE)); - - $lastSite = ceil($Statement->fetchColumn() / Application::get('POST.LIST_SIZE')); - - $PaginationTemplate = Template\Factory::build('pagination'); - $PaginationTemplate->set('THIS', $currentSite); - $PaginationTemplate->set('LAST', $lastSite); - $PaginationTemplate->set('HREF', Application::getPostURL('?site=%d')); - - return $PaginationTemplate; +function generatePageNaviTemplate($current): Template\Template { + return generateNaviTemplate($current, Application::getPageURL(), 'Page'); } #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generateUserNaviTemplate($currentSite): Template\Template { - $Database = Application::getDatabase(); - $Statement = $Database->query(sprintf('SELECT COUNT(id) FROM %s', User\Attribute::TABLE)); - - $lastSite = ceil($Statement->fetchColumn() / Application::get('USER.LIST_SIZE')); - - $PaginationTemplate = Template\Factory::build('pagination'); - $PaginationTemplate->set('THIS', $currentSite); - $PaginationTemplate->set('LAST', $lastSite); - $PaginationTemplate->set('HREF', Application::getUserURL('?site=%d')); +function generatePostNaviTemplate($current): Template\Template { + return generateNaviTemplate($current, Application::getPostURL(), 'Post'); +} - return $PaginationTemplate; +#=============================================================================== +# Helper function to reduce duplicate code +#=============================================================================== +function generateUserNaviTemplate($current): Template\Template { + return generateNaviTemplate($current, Application::getUserURL(), 'User'); } #=============================================================================== @@ -55,8 +44,8 @@ function generateUserNaviTemplate($currentSite): Template\Template { #=============================================================================== function generatePageItemTemplate(Page\Item $Page, User\Item $User): Template\Template { $Template = Template\Factory::build('page/item'); - $Template->set('PAGE', generatePageItemData($Page)); - $Template->set('USER', generateUserItemData($User)); + $Template->set('PAGE', generateItemTemplateData($Page)); + $Template->set('USER', generateItemTemplateData($User)); return $Template; } @@ -66,8 +55,8 @@ function generatePageItemTemplate(Page\Item $Page, User\Item $User): Template\Te #=============================================================================== function generatePostItemTemplate(Post\Item $Post, User\Item $User): Template\Template { $Template = Template\Factory::build('post/item'); - $Template->set('POST', generatePostItemData($Post)); - $Template->set('USER', generateUserItemData($User)); + $Template->set('POST', generateItemTemplateData($Post)); + $Template->set('USER', generateItemTemplateData($User)); return $Template; } @@ -77,7 +66,7 @@ function generatePostItemTemplate(Post\Item $Post, User\Item $User): Template\Te #=============================================================================== function generateUserItemTemplate(User\Item $User): Template\Template { $Template = Template\Factory::build('user/item'); - $Template->set('USER', generateUserItemData($User)); + $Template->set('USER', generateItemTemplateData($User)); return $Template; } @@ -85,13 +74,17 @@ function generateUserItemTemplate(User\Item $User): Template\Template { #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generateItemData(Item $Item): array { +function generateItemTemplateData(Item $Item): array { + $ATTR = $Item->getAttribute()->getAll(['password']); + $ATTR = array_change_key_case($ATTR, CASE_UPPER); + return [ - 'ID' => $Item->getID(), 'URL' => $Item->getURL(), 'GUID' => $Item->getGUID(), 'ARGV' => $Item->getArguments(), + 'ATTR' => $ATTR, + 'PREV' => FALSE, 'NEXT' => FALSE, @@ -106,69 +99,6 @@ function generateItemData(Item $Item): array { 'HTML' => function() use($Item) { return $Item->getHTML(); } - ], - - 'ATTR' => [ - 'USER' => $Item->attr('user'), - 'SLUG' => $Item->attr('slug'), - 'NAME' => $Item->attr('name'), - 'BODY' => $Item->attr('body'), - 'ARGV' => $Item->attr('argv'), - 'TIME_INSERT' => $Item->attr('time_insert'), - 'TIME_UPDATE' => $Item->attr('time_update') - ] - ]; -} - -#=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generatePageItemData(Page\Item $Page): array { - return generateItemData($Page); -} - -#=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generatePostItemData(Post\Item $Post): array { - return generateItemData($Post); -} - -#=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generateUserItemData(User\Item $User): array { - return [ - 'ID' => $User->getID(), - 'URL' => $User->getURL(), - 'GUID' => $User->getGUID(), - 'ARGV' => $User->getArguments(), - - 'PREV' => FALSE, - 'NEXT' => FALSE, - - 'FILE' => [ - 'LIST' => $User->getFiles() - ], - - 'BODY' => [ - 'TEXT' => function() use($User) { - return $User->getBody(); - }, - 'HTML' => function() use($User) { - return $User->getHTML(); - } - ], - - 'ATTR' => [ - 'SLUG' => $User->attr('slug'), - 'BODY' => $User->attr('body'), - 'ARGV' => $User->attr('argv'), - 'USERNAME' => $User->attr('username'), - 'FULLNAME' => $User->attr('fullname'), - 'MAILADDR' => $User->attr('mailaddr'), - 'TIME_INSERT' => $User->attr('time_insert'), - 'TIME_UPDATE' => $User->attr('time_update') ] ]; } @@ -354,7 +284,7 @@ function makeSlugURL($string, $separator = '-') { function PAGE(int $id): array { try { $Page = Page\Factory::build($id); - return generatePageItemData($Page); + return generateItemTemplateData($Page); } catch(Page\Exception $Exception) { return []; } @@ -366,7 +296,7 @@ function PAGE(int $id): array { function POST(int $id): array { try { $Post = Post\Factory::build($id); - return generatePostItemData($Post); + return generateItemTemplateData($Post); } catch(Post\Exception $Exception) { return []; } @@ -378,7 +308,7 @@ function POST(int $id): array { function USER(int $id): array { try { $User = User\Factory::build($id); - return generateUserItemData($User); + return generateItemTemplateData($User); } catch(User\Exception $Exception) { return []; } diff --git a/core/include/feed/main.php b/core/include/feed/main.php index 671f5c7..8879e72 100644 --- a/core/include/feed/main.php +++ b/core/include/feed/main.php @@ -24,8 +24,8 @@ try { $User = User\Factory::build($Post->attr('user')); $ItemTemplate = Template\Factory::build('feed/item_post'); - $ItemTemplate->set('POST', generatePostItemData($Post)); - $ItemTemplate->set('USER', generateUserItemData($User)); + $ItemTemplate->set('POST', generateItemTemplateData($Post)); + $ItemTemplate->set('USER', generateItemTemplateData($User)); $posts[] = $ItemTemplate; } @@ -45,8 +45,8 @@ try { $User = User\Factory::build($Page->attr('user')); $ItemTemplate = Template\Factory::build('feed/item_page'); - $ItemTemplate->set('PAGE', generatePageItemData($Page)); - $ItemTemplate->set('USER', generateUserItemData($User)); + $ItemTemplate->set('PAGE', generateItemTemplateData($Page)); + $ItemTemplate->set('USER', generateItemTemplateData($User)); $pages[] = $ItemTemplate; } diff --git a/core/include/page/main.php b/core/include/page/main.php index 01579fc..18ba4c8 100644 --- a/core/include/page/main.php +++ b/core/include/page/main.php @@ -19,20 +19,20 @@ try { $User = User\Factory::build($Page->attr('user')); - $page_data = generatePageItemData($Page); - $user_data = generateUserItemData($User); + $page_data = generateItemTemplateData($Page); + $user_data = generateItemTemplateData($User); #=============================================================================== # Add page data for previous and next page #=============================================================================== try { $PrevPage = Page\Factory::build($Page->getPrevID()); - $page_data['PREV'] = generatePageItemData($PrevPage); + $page_data['PREV'] = generateItemTemplateData($PrevPage); } catch(Page\Exception $Exception){} try { $NextPage = Page\Factory::build($Page->getNextID()); - $page_data['NEXT'] = generatePageItemData($NextPage); + $page_data['NEXT'] = generateItemTemplateData($NextPage); } catch(Page\Exception $Exception){} #=============================================================================== diff --git a/core/include/post/main.php b/core/include/post/main.php index 8cd0a49..f091a4c 100644 --- a/core/include/post/main.php +++ b/core/include/post/main.php @@ -19,20 +19,20 @@ try { $User = User\Factory::build($Post->attr('user')); - $post_data = generatePostItemData($Post); - $user_data = generateUserItemData($User); + $post_data = generateItemTemplateData($Post); + $user_data = generateItemTemplateData($User); #=============================================================================== # Add post data for previous and next post #=============================================================================== try { $PrevPost = Post\Factory::build($Post->getPrevID()); - $post_data['PREV'] = generatePostItemData($PrevPost); + $post_data['PREV'] = generateItemTemplateData($PrevPost); } catch(Post\Exception $Exception){} try { $NextPost = Post\Factory::build($Post->getNextID()); - $post_data['NEXT'] = generatePostItemData($NextPost); + $post_data['NEXT'] = generateItemTemplateData($NextPost); } catch(Post\Exception $Exception){} #=============================================================================== diff --git a/core/include/user/main.php b/core/include/user/main.php index ba0d07b..b2b054a 100644 --- a/core/include/user/main.php +++ b/core/include/user/main.php @@ -17,19 +17,19 @@ try { $User = User\Factory::build($param); } - $user_data = generateUserItemData($User); + $user_data = generateItemTemplateData($User); #=============================================================================== # Add user data for previous and next user #=============================================================================== try { $PrevUser = User\Factory::build($User->getPrevID()); - $user_data['PREV'] = generateUserItemData($PrevUser); + $user_data['PREV'] = generateItemTemplateData($PrevUser); } catch(User\Exception $Exception){} try { $NextUser = User\Factory::build($User->getNextID()); - $user_data['NEXT'] = generateUserItemData($NextUser); + $user_data['NEXT'] = generateItemTemplateData($NextUser); } catch(User\Exception $Exception){} #=============================================================================== diff --git a/core/namespace/Attribute.php b/core/namespace/Attribute.php index 69998e0..32cfa0a 100644 --- a/core/namespace/Attribute.php +++ b/core/namespace/Attribute.php @@ -16,6 +16,17 @@ abstract class Attribute implements AttributeInterface { } #=============================================================================== + # Get all attributes + #=============================================================================== + public function getAll($exclude = []): array { + $attributes = get_object_vars($this); + + return array_filter($attributes, function($attribute) use($exclude) { + return !in_array($attribute, $exclude); + }, ARRAY_FILTER_USE_KEY); + } + + #=============================================================================== # Get array with not FALSE attributes #=============================================================================== protected function getFilteredAttributes(): array { diff --git a/core/namespace/HTTP.php b/core/namespace/HTTP.php index bc2e062..d14b69b 100644 --- a/core/namespace/HTTP.php +++ b/core/namespace/HTTP.php @@ -81,23 +81,22 @@ class HTTP { self::$POST = $POST; self::$FILE = $FILE; - $removeArrays AND self::removeArrays(); + if($removeArrays) { + self::$GET = self::removeArrayValues(self::$GET); + self::$POST = self::removeArrayValues(self::$POST); + } self::$GET = ($trimValues === TRUE ? self::trim(self::$GET) : self::$GET ); self::$POST = ($trimValues === TRUE ? self::trim(self::$POST) : self::$POST); } #=============================================================================== - # Remove all arrays from $_GET and $_POST + # Remove all array values inside an array #=============================================================================== - private static function removeArrays() { - foreach(['GET', 'POST'] as $HTTP) { - foreach(self::$$HTTP as $name => $value) { - if(is_array(self::$$HTTP[$name])) { - unset(self::$$HTTP[$name]); - } - } - } + private static function removeArrayValues(array $array) { + return array_filter($array, function($value) { + return !is_array($value); + }); } #=============================================================================== @@ -114,7 +113,7 @@ class HTTP { #=============================================================================== # Checks if all elements of $arguments are set as key of $data #=============================================================================== - private static function issetData($data, $arguments) { + private static function issetData($data, $arguments): bool { foreach($arguments as $key) { if(is_array($key)) { if(!isset($data[key($key)]) OR $data[key($key)] !== $key[key($key)]) { @@ -131,61 +130,45 @@ class HTTP { } #=============================================================================== - # Return null or the value (if set) from one of the three requests attributes - #=============================================================================== - public static function returnKey($data, array $paths) { - $current = &$data; - - foreach($paths as $path) { - if(!isset($current[$path])) { - return NULL; - } - $current = &$current[$path]; - } - - return $current; - } - - #=============================================================================== # Return GET value #=============================================================================== - public static function GET() { - return self::returnKey(self::$GET, func_get_args()); + public static function GET($parameter) { + return self::$GET[$parameter] ?? NULL; } #=============================================================================== # Return POST value #=============================================================================== - public static function POST() { - return self::returnKey(self::$POST, func_get_args()); + public static function POST($parameter) { + return self::$POST[$parameter] ?? NULL; } #=============================================================================== # Return FILE value #=============================================================================== - public static function FILE() { - return self::returnKey(self::$FILE, func_get_args()); + public static function FILE($parameter) { + return self::$FILE[$parameter] ?? NULL; } #=============================================================================== - # Checks if all elements of func_get_args() are set key of self::$POST + # Checks if all elements of $parameters are set as key in self::$GET #=============================================================================== - public static function issetPOST() { - return self::issetData(self::$POST, func_get_args()); + public static function issetGET(... $parameters): bool { + return self::issetData(self::$GET, $parameters); } #=============================================================================== - # Checks if all elements of func_get_args() are set key of self::$GET + # Checks if all elements of $parameters are set as key in self::$POST #=============================================================================== - public static function issetGET() { - return self::issetData(self::$GET, func_get_args()); + public static function issetPOST(... $parameters): bool { + return self::issetData(self::$POST, $parameters); } #=============================================================================== - # Checks if all elements of func_get_args() are set key of self::$FILE + # Checks if all elements of $parameters are set as key in self::$FILE #=============================================================================== - public static function issetFILE() { - return self::issetData(self::$FILE, func_get_args()); + public static function issetFILE(... $parameters): bool { + return self::issetData(self::$FILE, $parameters); } #=============================================================================== |