mysociety /
theyworkforyou
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace MySociety\TheyWorkForYou; |
||||
| 4 | |||||
| 5 | /** |
||||
| 6 | * Data Class |
||||
| 7 | * |
||||
| 8 | * Includes a metadata file that contains the actual data. It will have an array like: |
||||
| 9 | * |
||||
| 10 | * ```php |
||||
| 11 | * $this->page = array ( |
||||
| 12 | * "default" => array ( |
||||
| 13 | * "sitetitle" => "Haddock Directory", |
||||
| 14 | * "session_vars" => array() |
||||
| 15 | * ), |
||||
| 16 | * "previous" => array ( |
||||
| 17 | * "title" => "Previous links", |
||||
| 18 | * "url" => "previouslinks/", |
||||
| 19 | * "section" => "blah" |
||||
| 20 | * ) |
||||
| 21 | * etc... |
||||
| 22 | * ); |
||||
| 23 | * ``` |
||||
| 24 | * |
||||
| 25 | * And a $this->section array, although this is as yet unspecified. Something like: |
||||
| 26 | * |
||||
| 27 | * ```php |
||||
| 28 | * $this->section = array ( |
||||
| 29 | * "blah" => array ( |
||||
| 30 | * "title" => "Blah", |
||||
| 31 | * "menu" => array ( |
||||
| 32 | * "text" => "Blah", |
||||
| 33 | * "title" => "Here's a link to Blah" |
||||
| 34 | * ) |
||||
| 35 | * ) |
||||
| 36 | * ); |
||||
| 37 | * ``` |
||||
| 38 | * |
||||
| 39 | * At some points we have a function where $type is passed in as, say, "page" |
||||
| 40 | * and then we do: |
||||
| 41 | * |
||||
| 42 | * ```php |
||||
| 43 | * $dataarray =& $this->$type; |
||||
| 44 | * return $dataarray[$item][$key]; |
||||
| 45 | * ``` |
||||
| 46 | * |
||||
| 47 | * Why? Because doing `$this->$type[$item][$key]` doesn't seem to work and |
||||
| 48 | * we need to use the reference to get it working. |
||||
| 49 | * |
||||
| 50 | * @author Phil Gyford <[email protected]> |
||||
| 51 | */ |
||||
| 52 | |||||
| 53 | class Data { |
||||
| 54 | public $page; |
||||
| 55 | public $section; |
||||
| 56 | |||||
| 57 | public function __construct() { |
||||
| 58 | include_once METADATAPATH; // defined in config.php |
||||
| 59 | $this->page = $page; |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||||
| 60 | $this->section = $section; |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 61 | } |
||||
| 62 | |||||
| 63 | /** |
||||
| 64 | * Set $this_section depending on this page's section. |
||||
| 65 | * |
||||
| 66 | * Special function for setting $this_section depending on the value of $this_page. |
||||
| 67 | */ |
||||
| 68 | |||||
| 69 | public function set_section() { |
||||
| 70 | // This should be called at the start of a page. |
||||
| 71 | global $this_section, $this_page; |
||||
| 72 | |||||
| 73 | $this_section = $this->page_metadata($this_page, "section"); |
||||
| 74 | } |
||||
| 75 | |||||
| 76 | /** |
||||
| 77 | * Get page metadata |
||||
| 78 | * |
||||
| 79 | * @param string $page Page name |
||||
| 80 | * @param string $key The element of metadata you want to retrieve |
||||
| 81 | */ |
||||
| 82 | |||||
| 83 | 25 | public function page_metadata($page, $key) { |
|||
| 84 | 25 | return $this->getMetadata( |
|||
| 85 | [ |
||||
| 86 | 25 | 'page' => $page, |
|||
| 87 | 25 | 'key' => $key, |
|||
| 88 | ], |
||||
| 89 | 25 | 'page' |
|||
| 90 | ); |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | /** |
||||
| 94 | * Get section metadata |
||||
| 95 | * |
||||
| 96 | * @param string $section Section name |
||||
| 97 | * @param string $key The element of metadata you want to retrieve |
||||
| 98 | */ |
||||
| 99 | |||||
| 100 | public function section_metadata($section, $key) { |
||||
| 101 | return $this->getMetadata( |
||||
| 102 | [ |
||||
| 103 | 'section' => $section, |
||||
| 104 | 'key' => $key, |
||||
| 105 | ], |
||||
| 106 | 'section' |
||||
| 107 | ); |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | /** |
||||
| 111 | * Set page metadata |
||||
| 112 | * |
||||
| 113 | * @param $page Page name |
||||
|
0 ignored issues
–
show
|
|||||
| 114 | * @param $key The element of metadata you want to set |
||||
| 115 | * @param $value The value to set |
||||
|
0 ignored issues
–
show
The type
MySociety\TheyWorkForYou\The was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 116 | */ |
||||
| 117 | |||||
| 118 | 2 | public function set_page_metadata($page, $key, $value) { |
|||
| 119 | 2 | $this->setMetadata(["page" => $page, "key" => $key, "value" => $value]); |
|||
| 120 | 2 | } |
|||
| 121 | |||||
| 122 | /** |
||||
| 123 | * Set section metadata |
||||
| 124 | * |
||||
| 125 | * @param $section Section name |
||||
| 126 | * @param $key The element of metadata you want to set |
||||
| 127 | * @param $value The value to set |
||||
| 128 | */ |
||||
| 129 | |||||
| 130 | public function set_section_metadata($section, $key, $value) { |
||||
| 131 | $this->setMetadata(["section" => $section, "key" => $key, "value" => $value]); |
||||
| 132 | } |
||||
| 133 | |||||
| 134 | /** |
||||
| 135 | * Directly access an item. |
||||
| 136 | * |
||||
| 137 | * @deprecated |
||||
| 138 | */ |
||||
| 139 | |||||
| 140 | public function metadata($type, $item, $key) { |
||||
| 141 | if ($this->test_for_metadata($type, $item, $key)) { |
||||
| 142 | return $this->$type[$item][$key]; |
||||
| 143 | } else { |
||||
| 144 | return "INVALID METADATA: $type[$item][$key]"; |
||||
| 145 | } |
||||
| 146 | } |
||||
| 147 | |||||
| 148 | /** |
||||
| 149 | * Test for the presence of something |
||||
| 150 | * |
||||
| 151 | * eg $exists = $DATA->test_for_metadata("page", "about", "title") |
||||
| 152 | */ |
||||
| 153 | |||||
| 154 | 25 | public function test_for_metadata($type, $item, $key) { |
|||
| 155 | 25 | $dataarray = & $this->$type; |
|||
| 156 | |||||
| 157 | 25 | if (isset($dataarray[$item][$key])) { |
|||
| 158 | 25 | return true; |
|||
| 159 | } else { |
||||
| 160 | 25 | return false; |
|||
| 161 | } |
||||
| 162 | } |
||||
| 163 | |||||
| 164 | /** |
||||
| 165 | * @param string $type |
||||
| 166 | */ |
||||
| 167 | |||||
| 168 | 25 | private function getMetadata($args, $type) { |
|||
| 169 | // $type is either 'page' or 'section' |
||||
| 170 | 25 | global $this_page, $this_section; |
|||
| 171 | |||||
| 172 | 25 | if (is_array($args)) { |
|||
| 173 | 25 | $item = $args[$type]; |
|||
| 174 | 25 | $key = $args['key']; |
|||
| 175 | } else { |
||||
| 176 | $var = "this_" . $type; |
||||
| 177 | $item = $$var; // $this_page or $this_section. |
||||
| 178 | $key = $args; |
||||
| 179 | } |
||||
| 180 | |||||
| 181 | 25 | twfy_debug("DATA", "$type: $item, $key"); |
|||
| 182 | 25 | $dataarray = & $this->$type; |
|||
| 183 | |||||
| 184 | 25 | if ($this->test_for_metadata($type, $item, $key)) { |
|||
| 185 | 24 | $return = $dataarray[$item][$key]; |
|||
| 186 | 24 | $debugtext = "Key: " . $type . "[" . $item . "][" . $key . "]"; |
|||
| 187 | |||||
| 188 | 25 | } elseif ($this->test_for_metadata($type, "default", $key)) { |
|||
| 189 | 24 | $return = $dataarray["default"][$key]; |
|||
| 190 | 24 | $debugtext = "Key: " . $type . "['default'][" . $key . "]"; |
|||
| 191 | |||||
| 192 | } else { |
||||
| 193 | 25 | $return = false; |
|||
| 194 | 25 | $debugtext = "No metadata found for key '$key'"; |
|||
| 195 | } |
||||
| 196 | |||||
| 197 | 25 | twfy_debug("DATA", "$debugtext, returning '" . (is_scalar($return) ? $return : gettype($return)) . "'."); |
|||
| 198 | |||||
| 199 | 25 | return $return; |
|||
| 200 | } |
||||
| 201 | |||||
| 202 | 2 | private function setMetadata($args) { |
|||
| 203 | |||||
| 204 | 2 | if (isset($args["section"])) { |
|||
| 205 | $type = "section"; |
||||
| 206 | $item = $args["section"]; |
||||
| 207 | } else { |
||||
| 208 | 2 | $type = "page"; |
|||
| 209 | 2 | $item = $args["page"]; |
|||
| 210 | } |
||||
| 211 | |||||
| 212 | 2 | $key = $args["key"]; |
|||
| 213 | 2 | $value = $args["value"]; |
|||
| 214 | |||||
| 215 | 2 | twfy_debug("DATA", "Setting: " . $type . "[" . $item . "][" . $key . "] = '" . print_r($value, 1) . "'"); |
|||
|
0 ignored issues
–
show
Are you sure
print_r($value, 1) of type string|true can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 216 | |||||
| 217 | 2 | $dataarray = & $this->$type; |
|||
| 218 | 2 | $dataarray[$item][$key] = $value; |
|||
| 219 | 2 | } |
|||
| 220 | |||||
| 221 | } |
||||
| 222 |