Passed
Push — 1.0.0-dev ( 896dae...8851fa )
by nguereza
02:32
created

Cookie::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
    defined('ROOT_PATH') || exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
31
    class Cookie extends BaseClass {
32
33
        /**
34
         * Construct new instance
35
         */
36
        public function __construct() {
37
            parent::__construct();
38
        }
39
        
40
        /**
41
         * Get the cookie item value
42
         * @param  string $item    the cookie item name to get
43
         * @param  mixed $default the default value to use if can not find the cokkie item in the list
44
         * @return mixed          the cookie value if exist or the default value
45
         */
46
        public function get($item, $default = null) {
47
            if (array_key_exists($item, get_instance()->globalvar->cookie())) {
48
                return get_instance()->globalvar->cookie($item);
49
            }
50
            $this->logger->warning('Cannot find cookie item [' . $item . '], using the default value [' . $default . ']');
51
            return $default;
52
        }
53
54
        /**
55
         * Set the cookie item value
56
         * @param string  $name     the cookie item name
57
         * @param string  $value    the cookie value to set
58
         * @param integer $expire   the time to live for this cookie
59
         * @param string  $path     the path that the cookie will be available
60
         * @param string  $domain   the domain that the cookie will be available
61
         * @param boolean $secure   if this cookie will be available on secure connection or not
62
         * @param boolean $httponly if this cookie will be available under HTTP protocol.
63
         */
64
        public function set($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false) {
65
            if (headers_sent()) {
66
                show_error('There exists a cookie that we wanted to create that we couldn\'t 
67
						    because headers was already sent. Make sure to do this first 
68
							before outputing anything.');
69
            }
70
            $timestamp = $expire;
71
            if ($expire) {
72
                $timestamp = time() + $expire;
73
            }
74
            setcookie($name, $value, $timestamp, $path, $domain, $secure, $httponly);
75
        }
76
77
        /**
78
         * Delete the cookie item in the list
79
         * @param  string $item the cookie item name to be cleared
80
         * @return boolean true if the item exists and is deleted successfully otherwise will return false.
81
         */
82
        public function delete($item) {
83
            if (array_key_exists($item, get_instance()->globalvar->cookie())) {
84
                $this->logger->info('Delete cookie item [' . $item . ']');
85
                get_instance()->globalvar->removeCookie($item);
86
                return true;
87
            } else {
88
                $this->logger->warning('Cookie item [' . $item . '] to be deleted does not exists');
89
                return false;
90
            }
91
        }
92
93
        /**
94
         * Check if the given cookie item exists
95
         * @param  string $item the cookie item name
96
         * @return boolean       true if the cookie item is set, false or not
97
         */
98
        public function exists($item) {
99
            return array_key_exists($item, get_instance()->globalvar->cookie());
100
        }
101
102
    }
103