@@ -25,17 +25,17 @@ |
||
| 25 | 25 | * |
| 26 | 26 | * @return string |
| 27 | 27 | */ |
| 28 | - function render() : string; |
|
| 28 | + function render() : string; |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * Converts the string builder to a string. |
| 32 | 32 | * |
| 33 | 33 | * @return string |
| 34 | 34 | */ |
| 35 | - function __toString(); |
|
| 35 | + function __toString(); |
|
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * Renders the string and echos it. |
| 39 | 39 | */ |
| 40 | - function display() : void; |
|
| 40 | + function display() : void; |
|
| 41 | 41 | } |
@@ -28,9 +28,9 @@ discard block |
||
| 28 | 28 | { |
| 29 | 29 | const ERROR_CANNOT_APPEND_FRAGMENT = 491001; |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @var boolean |
|
| 33 | - */ |
|
| 31 | + /** |
|
| 32 | + * @var boolean |
|
| 33 | + */ |
|
| 34 | 34 | private static $simulation = false; |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -38,11 +38,11 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | private $dom; |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Creates a new XMLHelper instance. |
|
| 43 | - * |
|
| 44 | - * @return XMLHelper |
|
| 45 | - */ |
|
| 41 | + /** |
|
| 42 | + * Creates a new XMLHelper instance. |
|
| 43 | + * |
|
| 44 | + * @return XMLHelper |
|
| 45 | + */ |
|
| 46 | 46 | public static function create() : XMLHelper |
| 47 | 47 | { |
| 48 | 48 | $dom = new DOMDocument('1.0', 'UTF-8'); |
@@ -51,72 +51,72 @@ discard block |
||
| 51 | 51 | return new XMLHelper($dom); |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Creates a converter instance from an XML file. |
|
| 56 | - * @param string $xmlFile |
|
| 57 | - * @return XMLHelper_Converter |
|
| 58 | - */ |
|
| 54 | + /** |
|
| 55 | + * Creates a converter instance from an XML file. |
|
| 56 | + * @param string $xmlFile |
|
| 57 | + * @return XMLHelper_Converter |
|
| 58 | + */ |
|
| 59 | 59 | public static function convertFile(string $xmlFile) : XMLHelper_Converter |
| 60 | 60 | { |
| 61 | 61 | return XMLHelper_Converter::fromFile($xmlFile); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Creates a converter from an XML string. |
|
| 66 | - * @param string $xmlString |
|
| 67 | - * @return XMLHelper_Converter |
|
| 68 | - */ |
|
| 64 | + /** |
|
| 65 | + * Creates a converter from an XML string. |
|
| 66 | + * @param string $xmlString |
|
| 67 | + * @return XMLHelper_Converter |
|
| 68 | + */ |
|
| 69 | 69 | public static function convertString(string $xmlString) : XMLHelper_Converter |
| 70 | 70 | { |
| 71 | 71 | return XMLHelper_Converter::fromString($xmlString); |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Creates a converter from a SimpleXMLElement instance. |
|
| 76 | - * @param SimpleXMLElement $element |
|
| 77 | - * @return XMLHelper_Converter |
|
| 78 | - */ |
|
| 74 | + /** |
|
| 75 | + * Creates a converter from a SimpleXMLElement instance. |
|
| 76 | + * @param SimpleXMLElement $element |
|
| 77 | + * @return XMLHelper_Converter |
|
| 78 | + */ |
|
| 79 | 79 | public static function convertElement(SimpleXMLElement $element) : XMLHelper_Converter |
| 80 | 80 | { |
| 81 | 81 | return XMLHelper_Converter::fromElement($element); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Creates a converter from a DOMElement instance. |
|
| 86 | - * @param DOMElement $element |
|
| 87 | - * @return XMLHelper_Converter |
|
| 88 | - */ |
|
| 84 | + /** |
|
| 85 | + * Creates a converter from a DOMElement instance. |
|
| 86 | + * @param DOMElement $element |
|
| 87 | + * @return XMLHelper_Converter |
|
| 88 | + */ |
|
| 89 | 89 | public static function convertDOMElement(DOMElement $element) : XMLHelper_Converter |
| 90 | 90 | { |
| 91 | 91 | return XMLHelper_Converter::fromDOMElement($element); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Creates a new helper using an existing DOMDocument object. |
|
| 96 | - * @param DOMDocument $dom |
|
| 97 | - */ |
|
| 94 | + /** |
|
| 95 | + * Creates a new helper using an existing DOMDocument object. |
|
| 96 | + * @param DOMDocument $dom |
|
| 97 | + */ |
|
| 98 | 98 | public function __construct(DOMDocument $dom) |
| 99 | 99 | { |
| 100 | 100 | $this->dom = $dom; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @return DOMDocument |
|
| 105 | - */ |
|
| 103 | + /** |
|
| 104 | + * @return DOMDocument |
|
| 105 | + */ |
|
| 106 | 106 | public function getDOM() : DOMDocument |
| 107 | 107 | { |
| 108 | 108 | return $this->dom; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Adds an attribute to an existing tag with |
|
| 113 | - * the specified value. |
|
| 114 | - * |
|
| 115 | - * @param DOMNode $parent |
|
| 116 | - * @param string $name |
|
| 117 | - * @param mixed $value |
|
| 118 | - * @return DOMNode |
|
| 119 | - */ |
|
| 111 | + /** |
|
| 112 | + * Adds an attribute to an existing tag with |
|
| 113 | + * the specified value. |
|
| 114 | + * |
|
| 115 | + * @param DOMNode $parent |
|
| 116 | + * @param string $name |
|
| 117 | + * @param mixed $value |
|
| 118 | + * @return DOMNode |
|
| 119 | + */ |
|
| 120 | 120 | public function addAttribute(DOMNode $parent, string $name, $value) |
| 121 | 121 | { |
| 122 | 122 | $node = $this->dom->createAttribute($name); |
@@ -126,12 +126,12 @@ discard block |
||
| 126 | 126 | return $parent->appendChild($node); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * Adds several attributes to the target node. |
|
| 131 | - * |
|
| 132 | - * @param DOMNode $parent |
|
| 133 | - * @param array<string,mixed> $attributes |
|
| 134 | - */ |
|
| 129 | + /** |
|
| 130 | + * Adds several attributes to the target node. |
|
| 131 | + * |
|
| 132 | + * @param DOMNode $parent |
|
| 133 | + * @param array<string,mixed> $attributes |
|
| 134 | + */ |
|
| 135 | 135 | public function addAttributes(DOMNode $parent, array $attributes) : void |
| 136 | 136 | { |
| 137 | 137 | foreach ($attributes as $name => $value) { |
@@ -139,14 +139,14 @@ discard block |
||
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Adds a tag without content. |
|
| 144 | - * |
|
| 145 | - * @param DOMNode $parent |
|
| 146 | - * @param string $name |
|
| 147 | - * @param integer $indent |
|
| 148 | - * @return DOMNode |
|
| 149 | - */ |
|
| 142 | + /** |
|
| 143 | + * Adds a tag without content. |
|
| 144 | + * |
|
| 145 | + * @param DOMNode $parent |
|
| 146 | + * @param string $name |
|
| 147 | + * @param integer $indent |
|
| 148 | + * @return DOMNode |
|
| 149 | + */ |
|
| 150 | 150 | public function addTag(DOMNode $parent, string $name, int $indent = 0) : DOMNode |
| 151 | 151 | { |
| 152 | 152 | if ($indent > 0) { |
@@ -168,17 +168,17 @@ discard block |
||
| 168 | 168 | $parent->appendChild($this->dom->createTextNode(str_repeat("\t", $amount))); |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * Adds a tag with textual content, like: |
|
| 173 | - * |
|
| 174 | - * <tagname>text</tagname> |
|
| 175 | - * |
|
| 176 | - * @param DOMNode $parent |
|
| 177 | - * @param string $name |
|
| 178 | - * @param string $text |
|
| 179 | - * @param integer $indent |
|
| 180 | - * @return DOMNode |
|
| 181 | - */ |
|
| 171 | + /** |
|
| 172 | + * Adds a tag with textual content, like: |
|
| 173 | + * |
|
| 174 | + * <tagname>text</tagname> |
|
| 175 | + * |
|
| 176 | + * @param DOMNode $parent |
|
| 177 | + * @param string $name |
|
| 178 | + * @param string $text |
|
| 179 | + * @param integer $indent |
|
| 180 | + * @return DOMNode |
|
| 181 | + */ |
|
| 182 | 182 | public function addTextTag(DOMNode $parent, string $name, string $text, int $indent = 0) : DOMNode |
| 183 | 183 | { |
| 184 | 184 | if ($indent > 0) { |
@@ -192,19 +192,19 @@ discard block |
||
| 192 | 192 | return $parent->appendChild($tag); |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Adds a tag with textual content, like: |
|
| 197 | - * |
|
| 198 | - * <tagname>text</tagname> |
|
| 199 | - * |
|
| 200 | - * and removes <p> tags |
|
| 201 | - * |
|
| 202 | - * @param DOMNode $parent |
|
| 203 | - * @param string $name |
|
| 204 | - * @param string $text |
|
| 205 | - * @param integer $indent |
|
| 206 | - * @return DOMNode |
|
| 207 | - */ |
|
| 195 | + /** |
|
| 196 | + * Adds a tag with textual content, like: |
|
| 197 | + * |
|
| 198 | + * <tagname>text</tagname> |
|
| 199 | + * |
|
| 200 | + * and removes <p> tags |
|
| 201 | + * |
|
| 202 | + * @param DOMNode $parent |
|
| 203 | + * @param string $name |
|
| 204 | + * @param string $text |
|
| 205 | + * @param integer $indent |
|
| 206 | + * @return DOMNode |
|
| 207 | + */ |
|
| 208 | 208 | public function addEscapedTag(DOMNode $parent, string $name, string $text, int $indent = 0) |
| 209 | 209 | { |
| 210 | 210 | if ($indent > 0) { |
@@ -220,19 +220,19 @@ discard block |
||
| 220 | 220 | return $parent->appendChild($tag); |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * Adds a tag with HTML content, like: |
|
| 225 | - * |
|
| 226 | - * <tagname><i>text</i></tagname> |
|
| 227 | - * |
|
| 228 | - * Tags will not be escaped. |
|
| 229 | - * |
|
| 230 | - * @param DOMNode $parent |
|
| 231 | - * @param string $name |
|
| 232 | - * @param string $text |
|
| 233 | - * @param integer $indent |
|
| 234 | - * @return DOMNode |
|
| 235 | - */ |
|
| 223 | + /** |
|
| 224 | + * Adds a tag with HTML content, like: |
|
| 225 | + * |
|
| 226 | + * <tagname><i>text</i></tagname> |
|
| 227 | + * |
|
| 228 | + * Tags will not be escaped. |
|
| 229 | + * |
|
| 230 | + * @param DOMNode $parent |
|
| 231 | + * @param string $name |
|
| 232 | + * @param string $text |
|
| 233 | + * @param integer $indent |
|
| 234 | + * @return DOMNode |
|
| 235 | + */ |
|
| 236 | 236 | public function addFragmentTag(DOMNode $parent, string $name, string $text, int $indent = 0) |
| 237 | 237 | { |
| 238 | 238 | if ($indent > 0) { |
@@ -260,16 +260,16 @@ discard block |
||
| 260 | 260 | return $parent->appendChild($tag); |
| 261 | 261 | } |
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Adds a tag with CDATA content, like: |
|
| 265 | - * |
|
| 266 | - * <tagname><![CDATA[value]]></tagname> |
|
| 267 | - * |
|
| 268 | - * @param DOMNode $parent |
|
| 269 | - * @param string $name |
|
| 270 | - * @param string $content |
|
| 271 | - * @return DOMNode |
|
| 272 | - */ |
|
| 263 | + /** |
|
| 264 | + * Adds a tag with CDATA content, like: |
|
| 265 | + * |
|
| 266 | + * <tagname><![CDATA[value]]></tagname> |
|
| 267 | + * |
|
| 268 | + * @param DOMNode $parent |
|
| 269 | + * @param string $name |
|
| 270 | + * @param string $content |
|
| 271 | + * @return DOMNode |
|
| 272 | + */ |
|
| 273 | 273 | public function addCDATATag(DOMNode $parent, string $name, string $content) : DOMNode |
| 274 | 274 | { |
| 275 | 275 | $tag = $this->dom->createElement($name); |
@@ -279,12 +279,12 @@ discard block |
||
| 279 | 279 | return $parent->appendChild($tag); |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | - /** |
|
| 283 | - * Creates the root element of the document. |
|
| 284 | - * @param string $name |
|
| 285 | - * @param array<string,mixed> $attributes |
|
| 286 | - * @return DOMNode |
|
| 287 | - */ |
|
| 282 | + /** |
|
| 283 | + * Creates the root element of the document. |
|
| 284 | + * @param string $name |
|
| 285 | + * @param array<string,mixed> $attributes |
|
| 286 | + * @return DOMNode |
|
| 287 | + */ |
|
| 288 | 288 | public function createRoot(string $name, array $attributes=array()) |
| 289 | 289 | { |
| 290 | 290 | $root = $this->dom->appendChild($this->dom->createElement($name)); |
@@ -292,12 +292,12 @@ discard block |
||
| 292 | 292 | return $root; |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - /** |
|
| 296 | - * Escaped the string for use in XML. |
|
| 297 | - * |
|
| 298 | - * @param string $string |
|
| 299 | - * @return string |
|
| 300 | - */ |
|
| 295 | + /** |
|
| 296 | + * Escaped the string for use in XML. |
|
| 297 | + * |
|
| 298 | + * @param string $string |
|
| 299 | + * @return string |
|
| 300 | + */ |
|
| 301 | 301 | public function escape(string $string) : string |
| 302 | 302 | { |
| 303 | 303 | $string = preg_replace('#<p>(.*)</p>#isUm', '$1', $string); |
@@ -317,18 +317,18 @@ discard block |
||
| 317 | 317 | return $string; |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | - /** |
|
| 321 | - * Sends the specified XML string to the browser with |
|
| 322 | - * the correct headers to trigger a download of the XML |
|
| 323 | - * to a local file. |
|
| 324 | - * |
|
| 325 | - * NOTE: Ensure calling exit after this is done, and to |
|
| 326 | - * not send additional content, which would corrupt the |
|
| 327 | - * download. |
|
| 328 | - * |
|
| 329 | - * @param string $xml |
|
| 330 | - * @param string $filename |
|
| 331 | - */ |
|
| 320 | + /** |
|
| 321 | + * Sends the specified XML string to the browser with |
|
| 322 | + * the correct headers to trigger a download of the XML |
|
| 323 | + * to a local file. |
|
| 324 | + * |
|
| 325 | + * NOTE: Ensure calling exit after this is done, and to |
|
| 326 | + * not send additional content, which would corrupt the |
|
| 327 | + * download. |
|
| 328 | + * |
|
| 329 | + * @param string $xml |
|
| 330 | + * @param string $filename |
|
| 331 | + */ |
|
| 332 | 332 | public static function downloadXML(string $xml, string $filename = 'download.xml') : void |
| 333 | 333 | { |
| 334 | 334 | if(!headers_sent() && !self::$simulation) |
@@ -339,12 +339,12 @@ discard block |
||
| 339 | 339 | echo $xml; |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - /** |
|
| 343 | - * Sends the specified XML string to the browser with |
|
| 344 | - * the correct headers and terminates the request. |
|
| 345 | - * |
|
| 346 | - * @param string $xml |
|
| 347 | - */ |
|
| 342 | + /** |
|
| 343 | + * Sends the specified XML string to the browser with |
|
| 344 | + * the correct headers and terminates the request. |
|
| 345 | + * |
|
| 346 | + * @param string $xml |
|
| 347 | + */ |
|
| 348 | 348 | public static function displayXML(string $xml) : void |
| 349 | 349 | { |
| 350 | 350 | if(!headers_sent() && !self::$simulation) |
@@ -411,23 +411,23 @@ discard block |
||
| 411 | 411 | return $xml->saveXML(); |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | - /** |
|
| 415 | - * Creates XML markup to describe an application error |
|
| 416 | - * when using XML services. Creates XML with the |
|
| 417 | - * following structure: |
|
| 418 | - * |
|
| 419 | - * <error> |
|
| 420 | - * <id>99</id> |
|
| 421 | - * <message>Full error message text</message> |
|
| 422 | - * <title>Short error label</title> |
|
| 423 | - * </error> |
|
| 424 | - * |
|
| 425 | - * @param string|number $code |
|
| 426 | - * @param string $message |
|
| 427 | - * @param string $title |
|
| 428 | - * @param array<string,string> $customInfo |
|
| 429 | - * @return string |
|
| 430 | - */ |
|
| 414 | + /** |
|
| 415 | + * Creates XML markup to describe an application error |
|
| 416 | + * when using XML services. Creates XML with the |
|
| 417 | + * following structure: |
|
| 418 | + * |
|
| 419 | + * <error> |
|
| 420 | + * <id>99</id> |
|
| 421 | + * <message>Full error message text</message> |
|
| 422 | + * <title>Short error label</title> |
|
| 423 | + * </error> |
|
| 424 | + * |
|
| 425 | + * @param string|number $code |
|
| 426 | + * @param string $message |
|
| 427 | + * @param string $title |
|
| 428 | + * @param array<string,string> $customInfo |
|
| 429 | + * @return string |
|
| 430 | + */ |
|
| 431 | 431 | public static function buildErrorXML($code, string $message, string $title, array $customInfo=array()) |
| 432 | 432 | { |
| 433 | 433 | $xml = new DOMDocument('1.0', 'UTF-8'); |
@@ -460,31 +460,31 @@ discard block |
||
| 460 | 460 | return $this->dom->saveXML(); |
| 461 | 461 | } |
| 462 | 462 | |
| 463 | - /** |
|
| 464 | - * Creates a new SimpleXML helper instance: this |
|
| 465 | - * object is useful to work with loading XML strings |
|
| 466 | - * and files with easy access to any errors that |
|
| 467 | - * may occurr, since the simplexml functions can be |
|
| 468 | - * somewhat cryptic. |
|
| 469 | - * |
|
| 470 | - * @return XMLHelper_SimpleXML |
|
| 471 | - */ |
|
| 463 | + /** |
|
| 464 | + * Creates a new SimpleXML helper instance: this |
|
| 465 | + * object is useful to work with loading XML strings |
|
| 466 | + * and files with easy access to any errors that |
|
| 467 | + * may occurr, since the simplexml functions can be |
|
| 468 | + * somewhat cryptic. |
|
| 469 | + * |
|
| 470 | + * @return XMLHelper_SimpleXML |
|
| 471 | + */ |
|
| 472 | 472 | public static function createSimplexml() : XMLHelper_SimpleXML |
| 473 | 473 | { |
| 474 | 474 | return new XMLHelper_SimpleXML(); |
| 475 | 475 | } |
| 476 | 476 | |
| 477 | - /** |
|
| 478 | - * Converts a string to valid XML: can be a text only string |
|
| 479 | - * or an HTML string. Returns valid XML code. |
|
| 480 | - * |
|
| 481 | - * NOTE: The string may contain custom tags, which are |
|
| 482 | - * preserved. |
|
| 483 | - * |
|
| 484 | - * @param string $string |
|
| 485 | - * @throws XMLHelper_Exception |
|
| 486 | - * @return string |
|
| 487 | - */ |
|
| 477 | + /** |
|
| 478 | + * Converts a string to valid XML: can be a text only string |
|
| 479 | + * or an HTML string. Returns valid XML code. |
|
| 480 | + * |
|
| 481 | + * NOTE: The string may contain custom tags, which are |
|
| 482 | + * preserved. |
|
| 483 | + * |
|
| 484 | + * @param string $string |
|
| 485 | + * @throws XMLHelper_Exception |
|
| 486 | + * @return string |
|
| 487 | + */ |
|
| 488 | 488 | public static function string2xml(string $string) : string |
| 489 | 489 | { |
| 490 | 490 | return XMLHelper_HTMLLoader::loadFragment($string)->fragmentToXML(); |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | |
| 244 | 244 | if (!empty($text)) { |
| 245 | 245 | $fragment = $this->dom->createDocumentFragment(); |
| 246 | - if(!@$fragment->appendXML($text)) { |
|
| 246 | + if (!@$fragment->appendXML($text)) { |
|
| 247 | 247 | throw new XMLHelper_Exception( |
| 248 | 248 | 'Cannot append XML fragment', |
| 249 | 249 | sprintf( |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | * @param array<string,mixed> $attributes |
| 286 | 286 | * @return DOMNode |
| 287 | 287 | */ |
| 288 | - public function createRoot(string $name, array $attributes=array()) |
|
| 288 | + public function createRoot(string $name, array $attributes = array()) |
|
| 289 | 289 | { |
| 290 | 290 | $root = $this->dom->appendChild($this->dom->createElement($name)); |
| 291 | 291 | $this->addAttributes($root, $attributes); |
@@ -311,8 +311,8 @@ discard block |
||
| 311 | 311 | $string = str_replace('<', 'LT_ESCAPE', $string); |
| 312 | 312 | $string = str_replace('>', 'GT_ESCAPE', $string); |
| 313 | 313 | |
| 314 | - $string = str_replace(' ',' ', $string); |
|
| 315 | - $string = str_replace('&','&', $string); |
|
| 314 | + $string = str_replace(' ', ' ', $string); |
|
| 315 | + $string = str_replace('&', '&', $string); |
|
| 316 | 316 | |
| 317 | 317 | return $string; |
| 318 | 318 | } |
@@ -331,9 +331,9 @@ discard block |
||
| 331 | 331 | */ |
| 332 | 332 | public static function downloadXML(string $xml, string $filename = 'download.xml') : void |
| 333 | 333 | { |
| 334 | - if(!headers_sent() && !self::$simulation) |
|
| 334 | + if (!headers_sent() && !self::$simulation) |
|
| 335 | 335 | { |
| 336 | - header('Content-Disposition: attachment; filename="' . $filename . '"'); |
|
| 336 | + header('Content-Disposition: attachment; filename="'.$filename.'"'); |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | echo $xml; |
@@ -347,12 +347,12 @@ discard block |
||
| 347 | 347 | */ |
| 348 | 348 | public static function displayXML(string $xml) : void |
| 349 | 349 | { |
| 350 | - if(!headers_sent() && !self::$simulation) |
|
| 350 | + if (!headers_sent() && !self::$simulation) |
|
| 351 | 351 | { |
| 352 | 352 | header('Content-Type:text/xml; charset=utf-8'); |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | - if(self::$simulation) |
|
| 355 | + if (self::$simulation) |
|
| 356 | 356 | { |
| 357 | 357 | $xml = '<pre>'.htmlspecialchars($xml).'</pre>'; |
| 358 | 358 | } |
@@ -370,16 +370,16 @@ discard block |
||
| 370 | 370 | * @param array<string,string> $customInfo Associative array with name => value pairs for custom tags to add to the output xml |
| 371 | 371 | * @see buildErrorXML() |
| 372 | 372 | */ |
| 373 | - public static function displayErrorXML($code, string $message, string $title, array $customInfo=array()) |
|
| 373 | + public static function displayErrorXML($code, string $message, string $title, array $customInfo = array()) |
|
| 374 | 374 | { |
| 375 | - if(!headers_sent() && !self::$simulation) { |
|
| 376 | - header('HTTP/1.1 400 Bad Request: ' . $title, true, 400); |
|
| 375 | + if (!headers_sent() && !self::$simulation) { |
|
| 376 | + header('HTTP/1.1 400 Bad Request: '.$title, true, 400); |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | self::displayXML(self::buildErrorXML($code, $message, $title, $customInfo)); |
| 380 | 380 | } |
| 381 | 381 | |
| 382 | - public static function setSimulation(bool $simulate=true) : void |
|
| 382 | + public static function setSimulation(bool $simulate = true) : void |
|
| 383 | 383 | { |
| 384 | 384 | self::$simulation = $simulate; |
| 385 | 385 | } |
@@ -428,7 +428,7 @@ discard block |
||
| 428 | 428 | * @param array<string,string> $customInfo |
| 429 | 429 | * @return string |
| 430 | 430 | */ |
| 431 | - public static function buildErrorXML($code, string $message, string $title, array $customInfo=array()) |
|
| 431 | + public static function buildErrorXML($code, string $message, string $title, array $customInfo = array()) |
|
| 432 | 432 | { |
| 433 | 433 | $xml = new DOMDocument('1.0', 'UTF-8'); |
| 434 | 434 | $xml->formatOutput = true; |
@@ -442,7 +442,7 @@ discard block |
||
| 442 | 442 | $helper->addTextTag($root, 'title', $title); |
| 443 | 443 | $helper->addTextTag($root, 'request_uri', $_SERVER['REQUEST_URI']); |
| 444 | 444 | |
| 445 | - foreach($customInfo as $name => $value) { |
|
| 445 | + foreach ($customInfo as $name => $value) { |
|
| 446 | 446 | $helper->addTextTag($root, $name, $value); |
| 447 | 447 | } |
| 448 | 448 | |
@@ -41,71 +41,71 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | class Highlighter |
| 43 | 43 | { |
| 44 | - /** |
|
| 45 | - * Creates a new GeSHi instance from a source code string. |
|
| 46 | - * |
|
| 47 | - * @param string $sourceCode |
|
| 48 | - * @param string $format |
|
| 49 | - * @return GeSHi |
|
| 50 | - */ |
|
| 44 | + /** |
|
| 45 | + * Creates a new GeSHi instance from a source code string. |
|
| 46 | + * |
|
| 47 | + * @param string $sourceCode |
|
| 48 | + * @param string $format |
|
| 49 | + * @return GeSHi |
|
| 50 | + */ |
|
| 51 | 51 | public static function fromString(string $sourceCode, string $format) : GeSHi |
| 52 | 52 | { |
| 53 | 53 | return new GeSHi($sourceCode, $format); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Creates a new GeSHi instance from the contents of a file. |
|
| 58 | - * |
|
| 59 | - * @param string $path |
|
| 60 | - * @param string $format |
|
| 61 | - * @return GeSHi |
|
| 62 | - */ |
|
| 56 | + /** |
|
| 57 | + * Creates a new GeSHi instance from the contents of a file. |
|
| 58 | + * |
|
| 59 | + * @param string $path |
|
| 60 | + * @param string $format |
|
| 61 | + * @return GeSHi |
|
| 62 | + */ |
|
| 63 | 63 | public static function fromFile(string $path, string $format) : GeSHi |
| 64 | 64 | { |
| 65 | 65 | return self::fromString(FileHelper::readContents($path), $format); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Parses and highlights the target string. |
|
| 70 | - * |
|
| 71 | - * @param string $sourceCode |
|
| 72 | - * @param string $format |
|
| 73 | - * @return string |
|
| 74 | - */ |
|
| 68 | + /** |
|
| 69 | + * Parses and highlights the target string. |
|
| 70 | + * |
|
| 71 | + * @param string $sourceCode |
|
| 72 | + * @param string $format |
|
| 73 | + * @return string |
|
| 74 | + */ |
|
| 75 | 75 | public static function parseString(string $sourceCode, string $format) : string |
| 76 | 76 | { |
| 77 | 77 | return self::fromString($sourceCode, $format)->parse_code(); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Parses and highlights the contents of the target file. |
|
| 82 | - * |
|
| 83 | - * @param string $path |
|
| 84 | - * @param string $format |
|
| 85 | - * @return string |
|
| 86 | - */ |
|
| 80 | + /** |
|
| 81 | + * Parses and highlights the contents of the target file. |
|
| 82 | + * |
|
| 83 | + * @param string $path |
|
| 84 | + * @param string $format |
|
| 85 | + * @return string |
|
| 86 | + */ |
|
| 87 | 87 | public static function parseFile(string $path, string $format) : string |
| 88 | 88 | { |
| 89 | 89 | return self::fromFile($path, $format)->parse_code(); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Adds HTML syntax highlighting to the specified SQL string. |
|
| 94 | - * |
|
| 95 | - * @param string $sql |
|
| 96 | - * @return string |
|
| 97 | - */ |
|
| 92 | + /** |
|
| 93 | + * Adds HTML syntax highlighting to the specified SQL string. |
|
| 94 | + * |
|
| 95 | + * @param string $sql |
|
| 96 | + * @return string |
|
| 97 | + */ |
|
| 98 | 98 | public static function sql(string $sql) : string |
| 99 | 99 | { |
| 100 | 100 | return self::parseString($sql, 'sql'); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Adds HTML syntax highlighting to a JSON string, or a data array/object. |
|
| 105 | - * |
|
| 106 | - * @param array|object|string $subject A JSON string, or data array/object to convert to JSON to highlight. |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 103 | + /** |
|
| 104 | + * Adds HTML syntax highlighting to a JSON string, or a data array/object. |
|
| 105 | + * |
|
| 106 | + * @param array|object|string $subject A JSON string, or data array/object to convert to JSON to highlight. |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | 109 | public static function json($subject) : string |
| 110 | 110 | { |
| 111 | 111 | if(!is_string($subject)) |
@@ -118,13 +118,13 @@ discard block |
||
| 118 | 118 | return self::parseString($subject, 'javascript'); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Adds HTML syntax highlighting to the specified XML code. |
|
| 123 | - * |
|
| 124 | - * @param string $xml The XML to highlight. |
|
| 125 | - * @param bool $formatSource Whether to format the source with indentation to make it readable. |
|
| 126 | - * @return string |
|
| 127 | - */ |
|
| 121 | + /** |
|
| 122 | + * Adds HTML syntax highlighting to the specified XML code. |
|
| 123 | + * |
|
| 124 | + * @param string $xml The XML to highlight. |
|
| 125 | + * @param bool $formatSource Whether to format the source with indentation to make it readable. |
|
| 126 | + * @return string |
|
| 127 | + */ |
|
| 128 | 128 | public static function xml(string $xml, bool $formatSource=false) : string |
| 129 | 129 | { |
| 130 | 130 | if($formatSource) |
@@ -141,13 +141,13 @@ discard block |
||
| 141 | 141 | return self::parseString($xml, 'xml'); |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * Adds HTML syntax highlighting to the specified HTML code. |
|
| 146 | - * |
|
| 147 | - * @param string $html |
|
| 148 | - * @param bool $formatSource |
|
| 149 | - * @return string |
|
| 150 | - */ |
|
| 144 | + /** |
|
| 145 | + * Adds HTML syntax highlighting to the specified HTML code. |
|
| 146 | + * |
|
| 147 | + * @param string $html |
|
| 148 | + * @param bool $formatSource |
|
| 149 | + * @return string |
|
| 150 | + */ |
|
| 151 | 151 | public static function html(string $html, bool $formatSource=false) : string |
| 152 | 152 | { |
| 153 | 153 | if($formatSource) |
@@ -164,27 +164,27 @@ discard block |
||
| 164 | 164 | return self::parseString($html, 'xml'); |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Adds HTML syntax highlighting to a bit of PHP code. |
|
| 169 | - * |
|
| 170 | - * @param string $phpCode |
|
| 171 | - * @return string |
|
| 172 | - */ |
|
| 167 | + /** |
|
| 168 | + * Adds HTML syntax highlighting to a bit of PHP code. |
|
| 169 | + * |
|
| 170 | + * @param string $phpCode |
|
| 171 | + * @return string |
|
| 172 | + */ |
|
| 173 | 173 | public static function php(string $phpCode) : string |
| 174 | 174 | { |
| 175 | 175 | return self::parseString($phpCode, 'php'); |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * Adds HTML syntax highlighting to an URL. |
|
| 180 | - * |
|
| 181 | - * NOTE: Includes the necessary CSS styles. When |
|
| 182 | - * highlighting several URLs in the same page, |
|
| 183 | - * prefer using the `parseURL` function instead. |
|
| 184 | - * |
|
| 185 | - * @param string $url |
|
| 186 | - * @return string |
|
| 187 | - */ |
|
| 178 | + /** |
|
| 179 | + * Adds HTML syntax highlighting to an URL. |
|
| 180 | + * |
|
| 181 | + * NOTE: Includes the necessary CSS styles. When |
|
| 182 | + * highlighting several URLs in the same page, |
|
| 183 | + * prefer using the `parseURL` function instead. |
|
| 184 | + * |
|
| 185 | + * @param string $url |
|
| 186 | + * @return string |
|
| 187 | + */ |
|
| 188 | 188 | public static function url(string $url) : string |
| 189 | 189 | { |
| 190 | 190 | $info = parseURL($url); |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | */ |
| 109 | 109 | public static function json($subject) : string |
| 110 | 110 | { |
| 111 | - if(!is_string($subject)) |
|
| 111 | + if (!is_string($subject)) |
|
| 112 | 112 | { |
| 113 | 113 | $subject = json_encode($subject, JSON_PRETTY_PRINT); |
| 114 | 114 | } |
@@ -125,9 +125,9 @@ discard block |
||
| 125 | 125 | * @param bool $formatSource Whether to format the source with indentation to make it readable. |
| 126 | 126 | * @return string |
| 127 | 127 | */ |
| 128 | - public static function xml(string $xml, bool $formatSource=false) : string |
|
| 128 | + public static function xml(string $xml, bool $formatSource = false) : string |
|
| 129 | 129 | { |
| 130 | - if($formatSource) |
|
| 130 | + if ($formatSource) |
|
| 131 | 131 | { |
| 132 | 132 | $dom = new DOMDocument(); |
| 133 | 133 | $dom->preserveWhiteSpace = false; |
@@ -148,9 +148,9 @@ discard block |
||
| 148 | 148 | * @param bool $formatSource |
| 149 | 149 | * @return string |
| 150 | 150 | */ |
| 151 | - public static function html(string $html, bool $formatSource=false) : string |
|
| 151 | + public static function html(string $html, bool $formatSource = false) : string |
|
| 152 | 152 | { |
| 153 | - if($formatSource) |
|
| 153 | + if ($formatSource) |
|
| 154 | 154 | { |
| 155 | 155 | $dom = new DOMDocument(); |
| 156 | 156 | $dom->preserveWhiteSpace = false; |
@@ -23,19 +23,19 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | class ConvertHelper_TimeConverter |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * @var float |
|
| 28 | - */ |
|
| 26 | + /** |
|
| 27 | + * @var float |
|
| 28 | + */ |
|
| 29 | 29 | private $seconds; |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @var array<int,array<string,string|int>> |
|
| 33 | - */ |
|
| 31 | + /** |
|
| 32 | + * @var array<int,array<string,string|int>> |
|
| 33 | + */ |
|
| 34 | 34 | private static $units; |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param float $seconds |
|
| 38 | - */ |
|
| 36 | + /** |
|
| 37 | + * @param float $seconds |
|
| 38 | + */ |
|
| 39 | 39 | public function __construct($seconds) |
| 40 | 40 | { |
| 41 | 41 | $this->seconds = $seconds; |
@@ -43,9 +43,9 @@ discard block |
||
| 43 | 43 | $this->initUnits(); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Creates the list of units once per request as needed. |
|
| 48 | - */ |
|
| 46 | + /** |
|
| 47 | + * Creates the list of units once per request as needed. |
|
| 48 | + */ |
|
| 49 | 49 | private function initUnits() : void |
| 50 | 50 | { |
| 51 | 51 | if(isset(self::$units)) |
@@ -112,11 +112,11 @@ discard block |
||
| 112 | 112 | return implode(', ', $tokens) . ' ' . t('and') . ' ' . $last; |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Resolves the list of converted units. |
|
| 117 | - * |
|
| 118 | - * @return string[] |
|
| 119 | - */ |
|
| 115 | + /** |
|
| 116 | + * Resolves the list of converted units. |
|
| 117 | + * |
|
| 118 | + * @return string[] |
|
| 119 | + */ |
|
| 120 | 120 | private function resolveTokens() : array |
| 121 | 121 | { |
| 122 | 122 | $seconds = $this->seconds; |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | private function initUnits() : void |
| 50 | 50 | { |
| 51 | - if(isset(self::$units)) |
|
| 51 | + if (isset(self::$units)) |
|
| 52 | 52 | { |
| 53 | 53 | return; |
| 54 | 54 | } |
@@ -90,12 +90,12 @@ discard block |
||
| 90 | 90 | public function toString() : string |
| 91 | 91 | { |
| 92 | 92 | // specifically handle zero |
| 93 | - if($this->seconds <= 0) |
|
| 93 | + if ($this->seconds <= 0) |
|
| 94 | 94 | { |
| 95 | - return '0 ' . t('seconds'); |
|
| 95 | + return '0 '.t('seconds'); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if($this->seconds < 1) |
|
| 98 | + if ($this->seconds < 1) |
|
| 99 | 99 | { |
| 100 | 100 | return t('less than a second'); |
| 101 | 101 | } |
@@ -104,12 +104,12 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | $last = array_pop($tokens); |
| 106 | 106 | |
| 107 | - if(empty($tokens)) |
|
| 107 | + if (empty($tokens)) |
|
| 108 | 108 | { |
| 109 | 109 | return $last; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | - return implode(', ', $tokens) . ' ' . t('and') . ' ' . $last; |
|
| 112 | + return implode(', ', $tokens).' '.t('and').' '.$last; |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | /** |
@@ -122,18 +122,18 @@ discard block |
||
| 122 | 122 | $seconds = $this->seconds; |
| 123 | 123 | $tokens = array(); |
| 124 | 124 | |
| 125 | - foreach(self::$units as $def) |
|
| 125 | + foreach (self::$units as $def) |
|
| 126 | 126 | { |
| 127 | 127 | $unitValue = intval($seconds / $def['value']); |
| 128 | 128 | |
| 129 | - if($unitValue <= 0) |
|
| 129 | + if ($unitValue <= 0) |
|
| 130 | 130 | { |
| 131 | 131 | continue; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - $item = strval($unitValue) . ' '; |
|
| 134 | + $item = strval($unitValue).' '; |
|
| 135 | 135 | |
| 136 | - if(abs($unitValue) > 1) |
|
| 136 | + if (abs($unitValue) > 1) |
|
| 137 | 137 | { |
| 138 | 138 | $item .= $def['plural']; |
| 139 | 139 | } |
@@ -136,8 +136,7 @@ |
||
| 136 | 136 | if(abs($unitValue) > 1) |
| 137 | 137 | { |
| 138 | 138 | $item .= $def['plural']; |
| 139 | - } |
|
| 140 | - else |
|
| 139 | + } else |
|
| 141 | 140 | { |
| 142 | 141 | $item .= $def['singular']; |
| 143 | 142 | } |
@@ -191,8 +191,7 @@ discard block |
||
| 191 | 191 | if ($item->isDir()) |
| 192 | 192 | { |
| 193 | 193 | FileHelper::copyTree($itemPath, $target . '/' . $baseName); |
| 194 | - } |
|
| 195 | - else if($item->isFile()) |
|
| 194 | + } else if($item->isFile()) |
|
| 196 | 195 | { |
| 197 | 196 | self::copyFile($itemPath, $target . '/' . $baseName); |
| 198 | 197 | } |
@@ -225,8 +224,7 @@ discard block |
||
| 225 | 224 | if(!file_exists($targetFolder)) |
| 226 | 225 | { |
| 227 | 226 | self::createFolder($targetFolder); |
| 228 | - } |
|
| 229 | - else if(!is_writable($targetFolder)) |
|
| 227 | + } else if(!is_writable($targetFolder)) |
|
| 230 | 228 | { |
| 231 | 229 | throw new FileHelper_Exception( |
| 232 | 230 | sprintf('Target folder [%s] is not writable.', basename($targetFolder)), |
@@ -715,8 +713,7 @@ discard block |
||
| 715 | 713 | if(isset($options['relative-path']) && $options['relative-path'] === true) |
| 716 | 714 | { |
| 717 | 715 | $finder->setPathmodeRelative(); |
| 718 | - } |
|
| 719 | - else if(isset($options['absolute-path']) && $options['absolute-path'] === true) |
|
| 716 | + } else if(isset($options['absolute-path']) && $options['absolute-path'] === true) |
|
| 720 | 717 | { |
| 721 | 718 | $finder->setPathmodeAbsolute(); |
| 722 | 719 | } |
@@ -52,32 +52,32 @@ discard block |
||
| 52 | 52 | const ERROR_PATH_IS_NOT_A_FOLDER = 340034; |
| 53 | 53 | const ERROR_CANNOT_WRITE_TO_FOLDER = 340035; |
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Opens a serialized file and returns the unserialized data. |
|
| 57 | - * |
|
| 58 | - * @param string $file |
|
| 59 | - * @throws FileHelper_Exception |
|
| 60 | - * @return array |
|
| 61 | - * @deprecated Use parseSerializedFile() instead. |
|
| 62 | - * @see FileHelper::parseSerializedFile() |
|
| 63 | - */ |
|
| 55 | + /** |
|
| 56 | + * Opens a serialized file and returns the unserialized data. |
|
| 57 | + * |
|
| 58 | + * @param string $file |
|
| 59 | + * @throws FileHelper_Exception |
|
| 60 | + * @return array |
|
| 61 | + * @deprecated Use parseSerializedFile() instead. |
|
| 62 | + * @see FileHelper::parseSerializedFile() |
|
| 63 | + */ |
|
| 64 | 64 | public static function openUnserialized(string $file) : array |
| 65 | 65 | { |
| 66 | 66 | return self::parseSerializedFile($file); |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Opens a serialized file and returns the unserialized data. |
|
| 71 | - * |
|
| 72 | - * @param string $file |
|
| 73 | - * @throws FileHelper_Exception |
|
| 74 | - * @return array |
|
| 75 | - * @see FileHelper::parseSerializedFile() |
|
| 76 | - * |
|
| 77 | - * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 78 | - * @see FileHelper::ERROR_SERIALIZED_FILE_CANNOT_BE_READ |
|
| 79 | - * @see FileHelper::ERROR_SERIALIZED_FILE_UNSERIALZE_FAILED |
|
| 80 | - */ |
|
| 69 | + /** |
|
| 70 | + * Opens a serialized file and returns the unserialized data. |
|
| 71 | + * |
|
| 72 | + * @param string $file |
|
| 73 | + * @throws FileHelper_Exception |
|
| 74 | + * @return array |
|
| 75 | + * @see FileHelper::parseSerializedFile() |
|
| 76 | + * |
|
| 77 | + * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 78 | + * @see FileHelper::ERROR_SERIALIZED_FILE_CANNOT_BE_READ |
|
| 79 | + * @see FileHelper::ERROR_SERIALIZED_FILE_UNSERIALZE_FAILED |
|
| 80 | + */ |
|
| 81 | 81 | public static function parseSerializedFile(string $file) |
| 82 | 82 | { |
| 83 | 83 | self::requireFileExists($file); |
@@ -146,13 +146,13 @@ discard block |
||
| 146 | 146 | return rmdir($rootFolder); |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Create a folder, if it does not exist yet. |
|
| 151 | - * |
|
| 152 | - * @param string $path |
|
| 153 | - * @throws FileHelper_Exception |
|
| 154 | - * @see FileHelper::ERROR_CANNOT_CREATE_FOLDER |
|
| 155 | - */ |
|
| 149 | + /** |
|
| 150 | + * Create a folder, if it does not exist yet. |
|
| 151 | + * |
|
| 152 | + * @param string $path |
|
| 153 | + * @throws FileHelper_Exception |
|
| 154 | + * @see FileHelper::ERROR_CANNOT_CREATE_FOLDER |
|
| 155 | + */ |
|
| 156 | 156 | public static function createFolder($path) |
| 157 | 157 | { |
| 158 | 158 | if(is_dir($path) || mkdir($path, 0777, true)) { |
@@ -199,22 +199,22 @@ discard block |
||
| 199 | 199 | } |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * Copies a file to the target location. Includes checks |
|
| 204 | - * for most error sources, like the source file not being |
|
| 205 | - * readable. Automatically creates the target folder if it |
|
| 206 | - * does not exist yet. |
|
| 207 | - * |
|
| 208 | - * @param string $sourcePath |
|
| 209 | - * @param string $targetPath |
|
| 210 | - * @throws FileHelper_Exception |
|
| 211 | - * |
|
| 212 | - * @see FileHelper::ERROR_CANNOT_CREATE_FOLDER |
|
| 213 | - * @see FileHelper::ERROR_SOURCE_FILE_NOT_FOUND |
|
| 214 | - * @see FileHelper::ERROR_SOURCE_FILE_NOT_READABLE |
|
| 215 | - * @see FileHelper::ERROR_TARGET_COPY_FOLDER_NOT_WRITABLE |
|
| 216 | - * @see FileHelper::ERROR_CANNOT_COPY_FILE |
|
| 217 | - */ |
|
| 202 | + /** |
|
| 203 | + * Copies a file to the target location. Includes checks |
|
| 204 | + * for most error sources, like the source file not being |
|
| 205 | + * readable. Automatically creates the target folder if it |
|
| 206 | + * does not exist yet. |
|
| 207 | + * |
|
| 208 | + * @param string $sourcePath |
|
| 209 | + * @param string $targetPath |
|
| 210 | + * @throws FileHelper_Exception |
|
| 211 | + * |
|
| 212 | + * @see FileHelper::ERROR_CANNOT_CREATE_FOLDER |
|
| 213 | + * @see FileHelper::ERROR_SOURCE_FILE_NOT_FOUND |
|
| 214 | + * @see FileHelper::ERROR_SOURCE_FILE_NOT_READABLE |
|
| 215 | + * @see FileHelper::ERROR_TARGET_COPY_FOLDER_NOT_WRITABLE |
|
| 216 | + * @see FileHelper::ERROR_CANNOT_COPY_FILE |
|
| 217 | + */ |
|
| 218 | 218 | public static function copyFile(string $sourcePath, string $targetPath) : void |
| 219 | 219 | { |
| 220 | 220 | self::requireFileExists($sourcePath, self::ERROR_SOURCE_FILE_NOT_FOUND); |
@@ -254,15 +254,15 @@ discard block |
||
| 254 | 254 | ); |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - /** |
|
| 258 | - * Deletes the target file. Ignored if it cannot be found, |
|
| 259 | - * and throws an exception if it fails. |
|
| 260 | - * |
|
| 261 | - * @param string $filePath |
|
| 262 | - * @throws FileHelper_Exception |
|
| 263 | - * |
|
| 264 | - * @see FileHelper::ERROR_CANNOT_DELETE_FILE |
|
| 265 | - */ |
|
| 257 | + /** |
|
| 258 | + * Deletes the target file. Ignored if it cannot be found, |
|
| 259 | + * and throws an exception if it fails. |
|
| 260 | + * |
|
| 261 | + * @param string $filePath |
|
| 262 | + * @throws FileHelper_Exception |
|
| 263 | + * |
|
| 264 | + * @see FileHelper::ERROR_CANNOT_DELETE_FILE |
|
| 265 | + */ |
|
| 266 | 266 | public static function deleteFile(string $filePath) : void |
| 267 | 267 | { |
| 268 | 268 | if(!file_exists($filePath)) { |
@@ -284,15 +284,15 @@ discard block |
||
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | /** |
| 287 | - * Creates a new CSV parser instance and returns it. |
|
| 288 | - * |
|
| 289 | - * @param string $delimiter |
|
| 290 | - * @param string $enclosure |
|
| 291 | - * @param string $escape |
|
| 292 | - * @param bool $heading |
|
| 293 | - * @return Csv |
|
| 287 | + * Creates a new CSV parser instance and returns it. |
|
| 288 | + * |
|
| 289 | + * @param string $delimiter |
|
| 290 | + * @param string $enclosure |
|
| 291 | + * @param string $escape |
|
| 292 | + * @param bool $heading |
|
| 293 | + * @return Csv |
|
| 294 | 294 | * @see CSVHelper::createParser() |
| 295 | - */ |
|
| 295 | + */ |
|
| 296 | 296 | public static function createCSVParser(string $delimiter = ';', string $enclosure = '"', string $escape = '\\', bool $heading=false) : Csv |
| 297 | 297 | { |
| 298 | 298 | if($delimiter==='') { $delimiter = ';'; } |
@@ -305,21 +305,21 @@ discard block |
||
| 305 | 305 | return $parser; |
| 306 | 306 | } |
| 307 | 307 | |
| 308 | - /** |
|
| 309 | - * Parses all lines in the specified string and returns an |
|
| 310 | - * indexed array with all csv values in each line. |
|
| 311 | - * |
|
| 312 | - * @param string $csv |
|
| 313 | - * @param string $delimiter |
|
| 314 | - * @param string $enclosure |
|
| 315 | - * @param string $escape |
|
| 316 | - * @param bool $heading |
|
| 317 | - * @return array |
|
| 318 | - * @throws FileHelper_Exception |
|
| 319 | - * |
|
| 320 | - * @see parseCSVFile() |
|
| 321 | - * @see FileHelper::ERROR_PARSING_CSV |
|
| 322 | - */ |
|
| 308 | + /** |
|
| 309 | + * Parses all lines in the specified string and returns an |
|
| 310 | + * indexed array with all csv values in each line. |
|
| 311 | + * |
|
| 312 | + * @param string $csv |
|
| 313 | + * @param string $delimiter |
|
| 314 | + * @param string $enclosure |
|
| 315 | + * @param string $escape |
|
| 316 | + * @param bool $heading |
|
| 317 | + * @return array |
|
| 318 | + * @throws FileHelper_Exception |
|
| 319 | + * |
|
| 320 | + * @see parseCSVFile() |
|
| 321 | + * @see FileHelper::ERROR_PARSING_CSV |
|
| 322 | + */ |
|
| 323 | 323 | public static function parseCSVString(string $csv, string $delimiter = ';', string $enclosure = '"', string $escape = '\\', bool $heading=false) : array |
| 324 | 324 | { |
| 325 | 325 | $parser = self::createCSVParser($delimiter, $enclosure, '\\', $heading); |
@@ -490,31 +490,31 @@ discard block |
||
| 490 | 490 | ); |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | - /** |
|
| 494 | - * Verifies whether the target file is a PHP file. The path |
|
| 495 | - * to the file can be a path to a file as a string, or a |
|
| 496 | - * DirectoryIterator object instance. |
|
| 497 | - * |
|
| 498 | - * @param string|\DirectoryIterator $pathOrDirIterator |
|
| 499 | - * @return boolean |
|
| 500 | - */ |
|
| 493 | + /** |
|
| 494 | + * Verifies whether the target file is a PHP file. The path |
|
| 495 | + * to the file can be a path to a file as a string, or a |
|
| 496 | + * DirectoryIterator object instance. |
|
| 497 | + * |
|
| 498 | + * @param string|\DirectoryIterator $pathOrDirIterator |
|
| 499 | + * @return boolean |
|
| 500 | + */ |
|
| 501 | 501 | public static function isPHPFile($pathOrDirIterator) |
| 502 | 502 | { |
| 503 | - if(self::getExtension($pathOrDirIterator) == 'php') { |
|
| 504 | - return true; |
|
| 505 | - } |
|
| 503 | + if(self::getExtension($pathOrDirIterator) == 'php') { |
|
| 504 | + return true; |
|
| 505 | + } |
|
| 506 | 506 | |
| 507 | - return false; |
|
| 507 | + return false; |
|
| 508 | 508 | } |
| 509 | 509 | |
| 510 | - /** |
|
| 511 | - * Retrieves the extension of the specified file. Can be a path |
|
| 512 | - * to a file as a string, or a DirectoryIterator object instance. |
|
| 513 | - * |
|
| 514 | - * @param string|\DirectoryIterator $pathOrDirIterator |
|
| 515 | - * @param bool $lowercase |
|
| 516 | - * @return string |
|
| 517 | - */ |
|
| 510 | + /** |
|
| 511 | + * Retrieves the extension of the specified file. Can be a path |
|
| 512 | + * to a file as a string, or a DirectoryIterator object instance. |
|
| 513 | + * |
|
| 514 | + * @param string|\DirectoryIterator $pathOrDirIterator |
|
| 515 | + * @param bool $lowercase |
|
| 516 | + * @return string |
|
| 517 | + */ |
|
| 518 | 518 | public static function getExtension($pathOrDirIterator, bool $lowercase = true) : string |
| 519 | 519 | { |
| 520 | 520 | if($pathOrDirIterator instanceof \DirectoryIterator) { |
@@ -525,51 +525,51 @@ discard block |
||
| 525 | 525 | |
| 526 | 526 | $ext = pathinfo($filename, PATHINFO_EXTENSION); |
| 527 | 527 | if($lowercase) { |
| 528 | - $ext = mb_strtolower($ext); |
|
| 528 | + $ext = mb_strtolower($ext); |
|
| 529 | 529 | } |
| 530 | 530 | |
| 531 | 531 | return $ext; |
| 532 | 532 | } |
| 533 | 533 | |
| 534 | - /** |
|
| 535 | - * Retrieves the file name from a path, with or without extension. |
|
| 536 | - * The path to the file can be a string, or a DirectoryIterator object |
|
| 537 | - * instance. |
|
| 538 | - * |
|
| 539 | - * In case of folders, behaves like the pathinfo function: returns |
|
| 540 | - * the name of the folder. |
|
| 541 | - * |
|
| 542 | - * @param string|\DirectoryIterator $pathOrDirIterator |
|
| 543 | - * @param bool $extension |
|
| 544 | - * @return string |
|
| 545 | - */ |
|
| 534 | + /** |
|
| 535 | + * Retrieves the file name from a path, with or without extension. |
|
| 536 | + * The path to the file can be a string, or a DirectoryIterator object |
|
| 537 | + * instance. |
|
| 538 | + * |
|
| 539 | + * In case of folders, behaves like the pathinfo function: returns |
|
| 540 | + * the name of the folder. |
|
| 541 | + * |
|
| 542 | + * @param string|\DirectoryIterator $pathOrDirIterator |
|
| 543 | + * @param bool $extension |
|
| 544 | + * @return string |
|
| 545 | + */ |
|
| 546 | 546 | public static function getFilename($pathOrDirIterator, $extension = true) |
| 547 | 547 | { |
| 548 | 548 | $path = $pathOrDirIterator; |
| 549 | - if($pathOrDirIterator instanceof \DirectoryIterator) { |
|
| 550 | - $path = $pathOrDirIterator->getFilename(); |
|
| 551 | - } |
|
| 549 | + if($pathOrDirIterator instanceof \DirectoryIterator) { |
|
| 550 | + $path = $pathOrDirIterator->getFilename(); |
|
| 551 | + } |
|
| 552 | 552 | |
| 553 | - $path = self::normalizePath($path); |
|
| 553 | + $path = self::normalizePath($path); |
|
| 554 | 554 | |
| 555 | - if(!$extension) { |
|
| 556 | - return pathinfo($path, PATHINFO_FILENAME); |
|
| 557 | - } |
|
| 555 | + if(!$extension) { |
|
| 556 | + return pathinfo($path, PATHINFO_FILENAME); |
|
| 557 | + } |
|
| 558 | 558 | |
| 559 | - return pathinfo($path, PATHINFO_BASENAME); |
|
| 559 | + return pathinfo($path, PATHINFO_BASENAME); |
|
| 560 | 560 | } |
| 561 | 561 | |
| 562 | - /** |
|
| 563 | - * Tries to read the contents of the target file and |
|
| 564 | - * treat it as JSON to return the decoded JSON data. |
|
| 565 | - * |
|
| 566 | - * @param string $file |
|
| 567 | - * @throws FileHelper_Exception |
|
| 568 | - * @return array |
|
| 569 | - * |
|
| 570 | - * @see FileHelper::ERROR_CANNOT_FIND_JSON_FILE |
|
| 571 | - * @see FileHelper::ERROR_CANNOT_DECODE_JSON_FILE |
|
| 572 | - */ |
|
| 562 | + /** |
|
| 563 | + * Tries to read the contents of the target file and |
|
| 564 | + * treat it as JSON to return the decoded JSON data. |
|
| 565 | + * |
|
| 566 | + * @param string $file |
|
| 567 | + * @throws FileHelper_Exception |
|
| 568 | + * @return array |
|
| 569 | + * |
|
| 570 | + * @see FileHelper::ERROR_CANNOT_FIND_JSON_FILE |
|
| 571 | + * @see FileHelper::ERROR_CANNOT_DECODE_JSON_FILE |
|
| 572 | + */ |
|
| 573 | 573 | public static function parseJSONFile(string $file, $targetEncoding=null, $sourceEncoding=null) |
| 574 | 574 | { |
| 575 | 575 | self::requireFileExists($file, self::ERROR_CANNOT_FIND_JSON_FILE); |
@@ -605,16 +605,16 @@ discard block |
||
| 605 | 605 | return $json; |
| 606 | 606 | } |
| 607 | 607 | |
| 608 | - /** |
|
| 609 | - * Corrects common formatting mistakes when users enter |
|
| 610 | - * file names, like too many spaces, dots and the like. |
|
| 611 | - * |
|
| 612 | - * NOTE: if the file name contains a path, the path is |
|
| 613 | - * stripped, leaving only the file name. |
|
| 614 | - * |
|
| 615 | - * @param string $name |
|
| 616 | - * @return string |
|
| 617 | - */ |
|
| 608 | + /** |
|
| 609 | + * Corrects common formatting mistakes when users enter |
|
| 610 | + * file names, like too many spaces, dots and the like. |
|
| 611 | + * |
|
| 612 | + * NOTE: if the file name contains a path, the path is |
|
| 613 | + * stripped, leaving only the file name. |
|
| 614 | + * |
|
| 615 | + * @param string $name |
|
| 616 | + * @return string |
|
| 617 | + */ |
|
| 618 | 618 | public static function fixFileName(string $name) : string |
| 619 | 619 | { |
| 620 | 620 | $name = trim($name); |
@@ -644,68 +644,68 @@ discard block |
||
| 644 | 644 | return $name; |
| 645 | 645 | } |
| 646 | 646 | |
| 647 | - /** |
|
| 648 | - * Creates an instance of the file finder, which is an easier |
|
| 649 | - * alternative to the other manual findFile methods, since all |
|
| 650 | - * options can be set by chaining. |
|
| 651 | - * |
|
| 652 | - * @param string $path |
|
| 653 | - * @return FileHelper_FileFinder |
|
| 654 | - */ |
|
| 647 | + /** |
|
| 648 | + * Creates an instance of the file finder, which is an easier |
|
| 649 | + * alternative to the other manual findFile methods, since all |
|
| 650 | + * options can be set by chaining. |
|
| 651 | + * |
|
| 652 | + * @param string $path |
|
| 653 | + * @return FileHelper_FileFinder |
|
| 654 | + */ |
|
| 655 | 655 | public static function createFileFinder(string $path) : FileHelper_FileFinder |
| 656 | 656 | { |
| 657 | 657 | return new FileHelper_FileFinder($path); |
| 658 | 658 | } |
| 659 | 659 | |
| 660 | - /** |
|
| 661 | - * Searches for all HTML files in the target folder. |
|
| 662 | - * |
|
| 663 | - * NOTE: This method only exists for backwards compatibility. |
|
| 664 | - * Use the `createFileFinder()` method instead, which offers |
|
| 665 | - * an object oriented interface that is much easier to use. |
|
| 666 | - * |
|
| 667 | - * @param string $targetFolder |
|
| 668 | - * @param array $options |
|
| 669 | - * @return array An indexed array with files. |
|
| 670 | - * @see FileHelper::createFileFinder() |
|
| 671 | - */ |
|
| 660 | + /** |
|
| 661 | + * Searches for all HTML files in the target folder. |
|
| 662 | + * |
|
| 663 | + * NOTE: This method only exists for backwards compatibility. |
|
| 664 | + * Use the `createFileFinder()` method instead, which offers |
|
| 665 | + * an object oriented interface that is much easier to use. |
|
| 666 | + * |
|
| 667 | + * @param string $targetFolder |
|
| 668 | + * @param array $options |
|
| 669 | + * @return array An indexed array with files. |
|
| 670 | + * @see FileHelper::createFileFinder() |
|
| 671 | + */ |
|
| 672 | 672 | public static function findHTMLFiles(string $targetFolder, array $options=array()) : array |
| 673 | 673 | { |
| 674 | 674 | return self::findFiles($targetFolder, array('html'), $options); |
| 675 | 675 | } |
| 676 | 676 | |
| 677 | - /** |
|
| 678 | - * Searches for all PHP files in the target folder. |
|
| 679 | - * |
|
| 680 | - * NOTE: This method only exists for backwards compatibility. |
|
| 681 | - * Use the `createFileFinder()` method instead, which offers |
|
| 682 | - * an object oriented interface that is much easier to use. |
|
| 683 | - * |
|
| 684 | - * @param string $targetFolder |
|
| 685 | - * @param array $options |
|
| 686 | - * @return array An indexed array of PHP files. |
|
| 687 | - * @see FileHelper::createFileFinder() |
|
| 688 | - */ |
|
| 677 | + /** |
|
| 678 | + * Searches for all PHP files in the target folder. |
|
| 679 | + * |
|
| 680 | + * NOTE: This method only exists for backwards compatibility. |
|
| 681 | + * Use the `createFileFinder()` method instead, which offers |
|
| 682 | + * an object oriented interface that is much easier to use. |
|
| 683 | + * |
|
| 684 | + * @param string $targetFolder |
|
| 685 | + * @param array $options |
|
| 686 | + * @return array An indexed array of PHP files. |
|
| 687 | + * @see FileHelper::createFileFinder() |
|
| 688 | + */ |
|
| 689 | 689 | public static function findPHPFiles(string $targetFolder, array $options=array()) : array |
| 690 | 690 | { |
| 691 | 691 | return self::findFiles($targetFolder, array('php'), $options); |
| 692 | 692 | } |
| 693 | 693 | |
| 694 | - /** |
|
| 695 | - * Finds files according to the specified options. |
|
| 696 | - * |
|
| 697 | - * NOTE: This method only exists for backwards compatibility. |
|
| 698 | - * Use the `createFileFinder()` method instead, which offers |
|
| 699 | - * an object oriented interface that is much easier to use. |
|
| 700 | - * |
|
| 701 | - * @param string $targetFolder |
|
| 702 | - * @param array $extensions |
|
| 703 | - * @param array $options |
|
| 704 | - * @param array $files |
|
| 705 | - * @throws FileHelper_Exception |
|
| 706 | - * @return array |
|
| 707 | - * @see FileHelper::createFileFinder() |
|
| 708 | - */ |
|
| 694 | + /** |
|
| 695 | + * Finds files according to the specified options. |
|
| 696 | + * |
|
| 697 | + * NOTE: This method only exists for backwards compatibility. |
|
| 698 | + * Use the `createFileFinder()` method instead, which offers |
|
| 699 | + * an object oriented interface that is much easier to use. |
|
| 700 | + * |
|
| 701 | + * @param string $targetFolder |
|
| 702 | + * @param array $extensions |
|
| 703 | + * @param array $options |
|
| 704 | + * @param array $files |
|
| 705 | + * @throws FileHelper_Exception |
|
| 706 | + * @return array |
|
| 707 | + * @see FileHelper::createFileFinder() |
|
| 708 | + */ |
|
| 709 | 709 | public static function findFiles(string $targetFolder, array $extensions=array(), array $options=array(), array $files=array()) : array |
| 710 | 710 | { |
| 711 | 711 | $finder = self::createFileFinder($targetFolder); |
@@ -731,14 +731,14 @@ discard block |
||
| 731 | 731 | return $finder->getAll(); |
| 732 | 732 | } |
| 733 | 733 | |
| 734 | - /** |
|
| 735 | - * Removes the extension from the specified path or file name, |
|
| 736 | - * if any, and returns the name without the extension. |
|
| 737 | - * |
|
| 738 | - * @param string $filename |
|
| 739 | - * @param bool $keepPath Whether to keep the path component, if any. Default PHP pathinfo behavior is not to. |
|
| 740 | - * @return string |
|
| 741 | - */ |
|
| 734 | + /** |
|
| 735 | + * Removes the extension from the specified path or file name, |
|
| 736 | + * if any, and returns the name without the extension. |
|
| 737 | + * |
|
| 738 | + * @param string $filename |
|
| 739 | + * @param bool $keepPath Whether to keep the path component, if any. Default PHP pathinfo behavior is not to. |
|
| 740 | + * @return string |
|
| 741 | + */ |
|
| 742 | 742 | public static function removeExtension(string $filename, bool $keepPath=false) : string |
| 743 | 743 | { |
| 744 | 744 | // normalize paths to allow windows style slashes even on nix servers |
@@ -758,22 +758,22 @@ discard block |
||
| 758 | 758 | return implode('/', $parts); |
| 759 | 759 | } |
| 760 | 760 | |
| 761 | - /** |
|
| 762 | - * Detects the UTF BOM in the target file, if any. Returns |
|
| 763 | - * the encoding matching the BOM, which can be any of the |
|
| 764 | - * following: |
|
| 765 | - * |
|
| 766 | - * <ul> |
|
| 767 | - * <li>UTF32-BE</li> |
|
| 768 | - * <li>UTF32-LE</li> |
|
| 769 | - * <li>UTF16-BE</li> |
|
| 770 | - * <li>UTF16-LE</li> |
|
| 771 | - * <li>UTF8</li> |
|
| 772 | - * </ul> |
|
| 773 | - * |
|
| 774 | - * @param string $filename |
|
| 775 | - * @return string|NULL |
|
| 776 | - */ |
|
| 761 | + /** |
|
| 762 | + * Detects the UTF BOM in the target file, if any. Returns |
|
| 763 | + * the encoding matching the BOM, which can be any of the |
|
| 764 | + * following: |
|
| 765 | + * |
|
| 766 | + * <ul> |
|
| 767 | + * <li>UTF32-BE</li> |
|
| 768 | + * <li>UTF32-LE</li> |
|
| 769 | + * <li>UTF16-BE</li> |
|
| 770 | + * <li>UTF16-LE</li> |
|
| 771 | + * <li>UTF8</li> |
|
| 772 | + * </ul> |
|
| 773 | + * |
|
| 774 | + * @param string $filename |
|
| 775 | + * @return string|NULL |
|
| 776 | + */ |
|
| 777 | 777 | public static function detectUTFBom(string $filename) : ?string |
| 778 | 778 | { |
| 779 | 779 | $fp = fopen($filename, 'r'); |
@@ -805,13 +805,13 @@ discard block |
||
| 805 | 805 | |
| 806 | 806 | protected static $utfBoms; |
| 807 | 807 | |
| 808 | - /** |
|
| 809 | - * Retrieves a list of all UTF byte order mark character |
|
| 810 | - * sequences, as an assocative array with UTF encoding => bom sequence |
|
| 811 | - * pairs. |
|
| 812 | - * |
|
| 813 | - * @return array |
|
| 814 | - */ |
|
| 808 | + /** |
|
| 809 | + * Retrieves a list of all UTF byte order mark character |
|
| 810 | + * sequences, as an assocative array with UTF encoding => bom sequence |
|
| 811 | + * pairs. |
|
| 812 | + * |
|
| 813 | + * @return array |
|
| 814 | + */ |
|
| 815 | 815 | public static function getUTFBOMs() |
| 816 | 816 | { |
| 817 | 817 | if(!isset(self::$utfBoms)) { |
@@ -827,15 +827,15 @@ discard block |
||
| 827 | 827 | return self::$utfBoms; |
| 828 | 828 | } |
| 829 | 829 | |
| 830 | - /** |
|
| 831 | - * Checks whether the specified encoding is a valid |
|
| 832 | - * unicode encoding, for example "UTF16-LE" or "UTF8". |
|
| 833 | - * Also accounts for alternate way to write the, like |
|
| 834 | - * "UTF-8", and omitting little/big endian suffixes. |
|
| 835 | - * |
|
| 836 | - * @param string $encoding |
|
| 837 | - * @return boolean |
|
| 838 | - */ |
|
| 830 | + /** |
|
| 831 | + * Checks whether the specified encoding is a valid |
|
| 832 | + * unicode encoding, for example "UTF16-LE" or "UTF8". |
|
| 833 | + * Also accounts for alternate way to write the, like |
|
| 834 | + * "UTF-8", and omitting little/big endian suffixes. |
|
| 835 | + * |
|
| 836 | + * @param string $encoding |
|
| 837 | + * @return boolean |
|
| 838 | + */ |
|
| 839 | 839 | public static function isValidUnicodeEncoding(string $encoding) : bool |
| 840 | 840 | { |
| 841 | 841 | $encodings = self::getKnownUnicodeEncodings(); |
@@ -854,40 +854,40 @@ discard block |
||
| 854 | 854 | return in_array($encoding, $keep); |
| 855 | 855 | } |
| 856 | 856 | |
| 857 | - /** |
|
| 858 | - * Retrieves a list of all known unicode file encodings. |
|
| 859 | - * @return array |
|
| 860 | - */ |
|
| 857 | + /** |
|
| 858 | + * Retrieves a list of all known unicode file encodings. |
|
| 859 | + * @return array |
|
| 860 | + */ |
|
| 861 | 861 | public static function getKnownUnicodeEncodings() |
| 862 | 862 | { |
| 863 | 863 | return array_keys(self::getUTFBOMs()); |
| 864 | 864 | } |
| 865 | 865 | |
| 866 | - /** |
|
| 867 | - * Normalizes the slash style in a file or folder path, |
|
| 868 | - * by replacing any antislashes with forward slashes. |
|
| 869 | - * |
|
| 870 | - * @param string $path |
|
| 871 | - * @return string |
|
| 872 | - */ |
|
| 866 | + /** |
|
| 867 | + * Normalizes the slash style in a file or folder path, |
|
| 868 | + * by replacing any antislashes with forward slashes. |
|
| 869 | + * |
|
| 870 | + * @param string $path |
|
| 871 | + * @return string |
|
| 872 | + */ |
|
| 873 | 873 | public static function normalizePath(string $path) : string |
| 874 | 874 | { |
| 875 | 875 | return str_replace(array('\\', '//'), array('/', '/'), $path); |
| 876 | 876 | } |
| 877 | 877 | |
| 878 | - /** |
|
| 879 | - * Saves the specified data to a file, JSON encoded. |
|
| 880 | - * |
|
| 881 | - * @param mixed $data |
|
| 882 | - * @param string $file |
|
| 883 | - * @param bool $pretty |
|
| 884 | - * @throws FileHelper_Exception |
|
| 885 | - * |
|
| 886 | - * @see FileHelper::ERROR_JSON_ENCODE_ERROR |
|
| 887 | - * @see FileHelper::ERROR_SAVE_FOLDER_NOT_WRITABLE |
|
| 888 | - * @see FileHelper::ERROR_SAVE_FILE_NOT_WRITABLE |
|
| 889 | - * @see FileHelper::ERROR_SAVE_FILE_WRITE_FAILED |
|
| 890 | - */ |
|
| 878 | + /** |
|
| 879 | + * Saves the specified data to a file, JSON encoded. |
|
| 880 | + * |
|
| 881 | + * @param mixed $data |
|
| 882 | + * @param string $file |
|
| 883 | + * @param bool $pretty |
|
| 884 | + * @throws FileHelper_Exception |
|
| 885 | + * |
|
| 886 | + * @see FileHelper::ERROR_JSON_ENCODE_ERROR |
|
| 887 | + * @see FileHelper::ERROR_SAVE_FOLDER_NOT_WRITABLE |
|
| 888 | + * @see FileHelper::ERROR_SAVE_FILE_NOT_WRITABLE |
|
| 889 | + * @see FileHelper::ERROR_SAVE_FILE_WRITE_FAILED |
|
| 890 | + */ |
|
| 891 | 891 | public static function saveAsJSON($data, string $file, bool $pretty=false) |
| 892 | 892 | { |
| 893 | 893 | $options = null; |
@@ -911,18 +911,18 @@ discard block |
||
| 911 | 911 | self::saveFile($file, $json); |
| 912 | 912 | } |
| 913 | 913 | |
| 914 | - /** |
|
| 915 | - * Saves the specified content to the target file, creating |
|
| 916 | - * the file and the folder as necessary. |
|
| 917 | - * |
|
| 918 | - * @param string $filePath |
|
| 919 | - * @param string $content |
|
| 920 | - * @throws FileHelper_Exception |
|
| 921 | - * |
|
| 922 | - * @see FileHelper::ERROR_SAVE_FOLDER_NOT_WRITABLE |
|
| 923 | - * @see FileHelper::ERROR_SAVE_FILE_NOT_WRITABLE |
|
| 924 | - * @see FileHelper::ERROR_SAVE_FILE_WRITE_FAILED |
|
| 925 | - */ |
|
| 914 | + /** |
|
| 915 | + * Saves the specified content to the target file, creating |
|
| 916 | + * the file and the folder as necessary. |
|
| 917 | + * |
|
| 918 | + * @param string $filePath |
|
| 919 | + * @param string $content |
|
| 920 | + * @throws FileHelper_Exception |
|
| 921 | + * |
|
| 922 | + * @see FileHelper::ERROR_SAVE_FOLDER_NOT_WRITABLE |
|
| 923 | + * @see FileHelper::ERROR_SAVE_FILE_NOT_WRITABLE |
|
| 924 | + * @see FileHelper::ERROR_SAVE_FILE_WRITE_FAILED |
|
| 925 | + */ |
|
| 926 | 926 | public static function saveFile(string $filePath, string $content='') : void |
| 927 | 927 | { |
| 928 | 928 | $filePath = self::normalizePath($filePath); |
@@ -1072,16 +1072,16 @@ discard block |
||
| 1072 | 1072 | return $result; |
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | - /** |
|
| 1076 | - * Validates a PHP file's syntax. |
|
| 1077 | - * |
|
| 1078 | - * NOTE: This will fail silently if the PHP command line |
|
| 1079 | - * is not available. Use {@link FileHelper::canMakePHPCalls()} |
|
| 1080 | - * to check this beforehand as needed. |
|
| 1081 | - * |
|
| 1082 | - * @param string $path |
|
| 1083 | - * @return boolean|array A boolean true if the file is valid, an array with validation messages otherwise. |
|
| 1084 | - */ |
|
| 1075 | + /** |
|
| 1076 | + * Validates a PHP file's syntax. |
|
| 1077 | + * |
|
| 1078 | + * NOTE: This will fail silently if the PHP command line |
|
| 1079 | + * is not available. Use {@link FileHelper::canMakePHPCalls()} |
|
| 1080 | + * to check this beforehand as needed. |
|
| 1081 | + * |
|
| 1082 | + * @param string $path |
|
| 1083 | + * @return boolean|array A boolean true if the file is valid, an array with validation messages otherwise. |
|
| 1084 | + */ |
|
| 1085 | 1085 | public static function checkPHPFileSyntax($path) |
| 1086 | 1086 | { |
| 1087 | 1087 | if(!self::canMakePHPCalls()) { |
@@ -1105,14 +1105,14 @@ discard block |
||
| 1105 | 1105 | return $output; |
| 1106 | 1106 | } |
| 1107 | 1107 | |
| 1108 | - /** |
|
| 1109 | - * Retrieves the last modified date for the specified file or folder. |
|
| 1110 | - * |
|
| 1111 | - * Note: If the target does not exist, returns null. |
|
| 1112 | - * |
|
| 1113 | - * @param string $path |
|
| 1114 | - * @return \DateTime|NULL |
|
| 1115 | - */ |
|
| 1108 | + /** |
|
| 1109 | + * Retrieves the last modified date for the specified file or folder. |
|
| 1110 | + * |
|
| 1111 | + * Note: If the target does not exist, returns null. |
|
| 1112 | + * |
|
| 1113 | + * @param string $path |
|
| 1114 | + * @return \DateTime|NULL |
|
| 1115 | + */ |
|
| 1116 | 1116 | public static function getModifiedDate($path) |
| 1117 | 1117 | { |
| 1118 | 1118 | $time = filemtime($path); |
@@ -1125,24 +1125,24 @@ discard block |
||
| 1125 | 1125 | return null; |
| 1126 | 1126 | } |
| 1127 | 1127 | |
| 1128 | - /** |
|
| 1129 | - * Retrieves the names of all subfolders in the specified path. |
|
| 1130 | - * |
|
| 1131 | - * Available options: |
|
| 1132 | - * |
|
| 1133 | - * - recursive: true/false |
|
| 1134 | - * Whether to search for subfolders recursively. |
|
| 1135 | - * |
|
| 1136 | - * - absolute-paths: true/false |
|
| 1137 | - * Whether to return a list of absolute paths. |
|
| 1138 | - * |
|
| 1139 | - * @param string $targetFolder |
|
| 1140 | - * @param array $options |
|
| 1141 | - * @throws FileHelper_Exception |
|
| 1142 | - * @return string[] |
|
| 1143 | - * |
|
| 1144 | - * @todo Move this to a separate class. |
|
| 1145 | - */ |
|
| 1128 | + /** |
|
| 1129 | + * Retrieves the names of all subfolders in the specified path. |
|
| 1130 | + * |
|
| 1131 | + * Available options: |
|
| 1132 | + * |
|
| 1133 | + * - recursive: true/false |
|
| 1134 | + * Whether to search for subfolders recursively. |
|
| 1135 | + * |
|
| 1136 | + * - absolute-paths: true/false |
|
| 1137 | + * Whether to return a list of absolute paths. |
|
| 1138 | + * |
|
| 1139 | + * @param string $targetFolder |
|
| 1140 | + * @param array $options |
|
| 1141 | + * @throws FileHelper_Exception |
|
| 1142 | + * @return string[] |
|
| 1143 | + * |
|
| 1144 | + * @todo Move this to a separate class. |
|
| 1145 | + */ |
|
| 1146 | 1146 | public static function getSubfolders($targetFolder, $options = array()) |
| 1147 | 1147 | { |
| 1148 | 1148 | if(!is_dir($targetFolder)) |
@@ -1203,16 +1203,16 @@ discard block |
||
| 1203 | 1203 | return $result; |
| 1204 | 1204 | } |
| 1205 | 1205 | |
| 1206 | - /** |
|
| 1207 | - * Retrieves the maximum allowed upload file size, in bytes. |
|
| 1208 | - * Takes into account the PHP ini settings <code>post_max_size</code> |
|
| 1209 | - * and <code>upload_max_filesize</code>. Since these cannot |
|
| 1210 | - * be modified at runtime, they are the hard limits for uploads. |
|
| 1211 | - * |
|
| 1212 | - * NOTE: Based on binary values, where 1KB = 1024 Bytes. |
|
| 1213 | - * |
|
| 1214 | - * @return int Will return <code>-1</code> if no limit. |
|
| 1215 | - */ |
|
| 1206 | + /** |
|
| 1207 | + * Retrieves the maximum allowed upload file size, in bytes. |
|
| 1208 | + * Takes into account the PHP ini settings <code>post_max_size</code> |
|
| 1209 | + * and <code>upload_max_filesize</code>. Since these cannot |
|
| 1210 | + * be modified at runtime, they are the hard limits for uploads. |
|
| 1211 | + * |
|
| 1212 | + * NOTE: Based on binary values, where 1KB = 1024 Bytes. |
|
| 1213 | + * |
|
| 1214 | + * @return int Will return <code>-1</code> if no limit. |
|
| 1215 | + */ |
|
| 1216 | 1216 | public static function getMaxUploadFilesize() : int |
| 1217 | 1217 | { |
| 1218 | 1218 | static $max_size = -1; |
@@ -1250,16 +1250,16 @@ discard block |
||
| 1250 | 1250 | return round($size); |
| 1251 | 1251 | } |
| 1252 | 1252 | |
| 1253 | - /** |
|
| 1254 | - * Makes a path relative using a folder depth: will reduce the |
|
| 1255 | - * length of the path so that only the amount of folders defined |
|
| 1256 | - * in the <code>$depth</code> attribute are shown below the actual |
|
| 1257 | - * folder or file in the path. |
|
| 1258 | - * |
|
| 1259 | - * @param string $path The absolute or relative path |
|
| 1260 | - * @param int $depth The folder depth to reduce the path to |
|
| 1261 | - * @return string |
|
| 1262 | - */ |
|
| 1253 | + /** |
|
| 1254 | + * Makes a path relative using a folder depth: will reduce the |
|
| 1255 | + * length of the path so that only the amount of folders defined |
|
| 1256 | + * in the <code>$depth</code> attribute are shown below the actual |
|
| 1257 | + * folder or file in the path. |
|
| 1258 | + * |
|
| 1259 | + * @param string $path The absolute or relative path |
|
| 1260 | + * @param int $depth The folder depth to reduce the path to |
|
| 1261 | + * @return string |
|
| 1262 | + */ |
|
| 1263 | 1263 | public static function relativizePathByDepth(string $path, int $depth=2) : string |
| 1264 | 1264 | { |
| 1265 | 1265 | $path = self::normalizePath($path); |
@@ -1297,23 +1297,23 @@ discard block |
||
| 1297 | 1297 | return trim(implode('/', $tokens), '/'); |
| 1298 | 1298 | } |
| 1299 | 1299 | |
| 1300 | - /** |
|
| 1301 | - * Makes the specified path relative to another path, |
|
| 1302 | - * by removing one from the other if found. Also |
|
| 1303 | - * normalizes the path to use forward slashes. |
|
| 1304 | - * |
|
| 1305 | - * Example: |
|
| 1306 | - * |
|
| 1307 | - * <pre> |
|
| 1308 | - * relativizePath('c:\some\folder\to\file.txt', 'c:\some\folder'); |
|
| 1309 | - * </pre> |
|
| 1310 | - * |
|
| 1311 | - * Result: <code>to/file.txt</code> |
|
| 1312 | - * |
|
| 1313 | - * @param string $path |
|
| 1314 | - * @param string $relativeTo |
|
| 1315 | - * @return string |
|
| 1316 | - */ |
|
| 1300 | + /** |
|
| 1301 | + * Makes the specified path relative to another path, |
|
| 1302 | + * by removing one from the other if found. Also |
|
| 1303 | + * normalizes the path to use forward slashes. |
|
| 1304 | + * |
|
| 1305 | + * Example: |
|
| 1306 | + * |
|
| 1307 | + * <pre> |
|
| 1308 | + * relativizePath('c:\some\folder\to\file.txt', 'c:\some\folder'); |
|
| 1309 | + * </pre> |
|
| 1310 | + * |
|
| 1311 | + * Result: <code>to/file.txt</code> |
|
| 1312 | + * |
|
| 1313 | + * @param string $path |
|
| 1314 | + * @param string $relativeTo |
|
| 1315 | + * @return string |
|
| 1316 | + */ |
|
| 1317 | 1317 | public static function relativizePath(string $path, string $relativeTo) : string |
| 1318 | 1318 | { |
| 1319 | 1319 | $path = self::normalizePath($path); |
@@ -1325,17 +1325,17 @@ discard block |
||
| 1325 | 1325 | return $relative; |
| 1326 | 1326 | } |
| 1327 | 1327 | |
| 1328 | - /** |
|
| 1329 | - * Checks that the target file exists, and throws an exception |
|
| 1330 | - * if it does not. |
|
| 1331 | - * |
|
| 1332 | - * @param string $path |
|
| 1333 | - * @param int|NULL $errorCode Optional custom error code |
|
| 1334 | - * @throws FileHelper_Exception |
|
| 1335 | - * @return string The real path to the file |
|
| 1336 | - * |
|
| 1337 | - * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 1338 | - */ |
|
| 1328 | + /** |
|
| 1329 | + * Checks that the target file exists, and throws an exception |
|
| 1330 | + * if it does not. |
|
| 1331 | + * |
|
| 1332 | + * @param string $path |
|
| 1333 | + * @param int|NULL $errorCode Optional custom error code |
|
| 1334 | + * @throws FileHelper_Exception |
|
| 1335 | + * @return string The real path to the file |
|
| 1336 | + * |
|
| 1337 | + * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 1338 | + */ |
|
| 1339 | 1339 | public static function requireFileExists(string $path, ?int $errorCode=null) : string |
| 1340 | 1340 | { |
| 1341 | 1341 | $result = realpath($path); |
@@ -1373,18 +1373,18 @@ discard block |
||
| 1373 | 1373 | ); |
| 1374 | 1374 | } |
| 1375 | 1375 | |
| 1376 | - /** |
|
| 1377 | - * Reads a specific line number from the target file and returns its |
|
| 1378 | - * contents, if the file has such a line. Does so with little memory |
|
| 1379 | - * usage, as the file is not read entirely into memory. |
|
| 1380 | - * |
|
| 1381 | - * @param string $path |
|
| 1382 | - * @param int $lineNumber Note: 1-based; the first line is number 1. |
|
| 1383 | - * @return string|NULL Will return null if the requested line does not exist. |
|
| 1384 | - * @throws FileHelper_Exception |
|
| 1385 | - * |
|
| 1386 | - * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 1387 | - */ |
|
| 1376 | + /** |
|
| 1377 | + * Reads a specific line number from the target file and returns its |
|
| 1378 | + * contents, if the file has such a line. Does so with little memory |
|
| 1379 | + * usage, as the file is not read entirely into memory. |
|
| 1380 | + * |
|
| 1381 | + * @param string $path |
|
| 1382 | + * @param int $lineNumber Note: 1-based; the first line is number 1. |
|
| 1383 | + * @return string|NULL Will return null if the requested line does not exist. |
|
| 1384 | + * @throws FileHelper_Exception |
|
| 1385 | + * |
|
| 1386 | + * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 1387 | + */ |
|
| 1388 | 1388 | public static function getLineFromFile(string $path, int $lineNumber) : ?string |
| 1389 | 1389 | { |
| 1390 | 1390 | self::requireFileExists($path); |
@@ -1400,19 +1400,19 @@ discard block |
||
| 1400 | 1400 | $file->seek($targetLine); |
| 1401 | 1401 | |
| 1402 | 1402 | if($file->key() !== $targetLine) { |
| 1403 | - return null; |
|
| 1403 | + return null; |
|
| 1404 | 1404 | } |
| 1405 | 1405 | |
| 1406 | 1406 | return $file->current(); |
| 1407 | 1407 | } |
| 1408 | 1408 | |
| 1409 | - /** |
|
| 1410 | - * Retrieves the total amount of lines in the file, without |
|
| 1411 | - * reading the whole file into memory. |
|
| 1412 | - * |
|
| 1413 | - * @param string $path |
|
| 1414 | - * @return int |
|
| 1415 | - */ |
|
| 1409 | + /** |
|
| 1410 | + * Retrieves the total amount of lines in the file, without |
|
| 1411 | + * reading the whole file into memory. |
|
| 1412 | + * |
|
| 1413 | + * @param string $path |
|
| 1414 | + * @return int |
|
| 1415 | + */ |
|
| 1416 | 1416 | public static function countFileLines(string $path) : int |
| 1417 | 1417 | { |
| 1418 | 1418 | self::requireFileExists($path); |
@@ -1442,26 +1442,26 @@ discard block |
||
| 1442 | 1442 | return $number+1; |
| 1443 | 1443 | } |
| 1444 | 1444 | |
| 1445 | - /** |
|
| 1446 | - * Parses the target file to detect any PHP classes contained |
|
| 1447 | - * within, and retrieve information on them. Does not use the |
|
| 1448 | - * PHP reflection API. |
|
| 1449 | - * |
|
| 1450 | - * @param string $filePath |
|
| 1451 | - * @return FileHelper_PHPClassInfo |
|
| 1452 | - */ |
|
| 1445 | + /** |
|
| 1446 | + * Parses the target file to detect any PHP classes contained |
|
| 1447 | + * within, and retrieve information on them. Does not use the |
|
| 1448 | + * PHP reflection API. |
|
| 1449 | + * |
|
| 1450 | + * @param string $filePath |
|
| 1451 | + * @return FileHelper_PHPClassInfo |
|
| 1452 | + */ |
|
| 1453 | 1453 | public static function findPHPClasses(string $filePath) : FileHelper_PHPClassInfo |
| 1454 | 1454 | { |
| 1455 | 1455 | return new FileHelper_PHPClassInfo($filePath); |
| 1456 | 1456 | } |
| 1457 | 1457 | |
| 1458 | - /** |
|
| 1459 | - * Detects the end of line style used in the target file, if any. |
|
| 1460 | - * Can be used with large files, because it only reads part of it. |
|
| 1461 | - * |
|
| 1462 | - * @param string $filePath The path to the file. |
|
| 1463 | - * @return NULL|ConvertHelper_EOL The end of line character information, or NULL if none is found. |
|
| 1464 | - */ |
|
| 1458 | + /** |
|
| 1459 | + * Detects the end of line style used in the target file, if any. |
|
| 1460 | + * Can be used with large files, because it only reads part of it. |
|
| 1461 | + * |
|
| 1462 | + * @param string $filePath The path to the file. |
|
| 1463 | + * @return NULL|ConvertHelper_EOL The end of line character information, or NULL if none is found. |
|
| 1464 | + */ |
|
| 1465 | 1465 | public static function detectEOLCharacter(string $filePath) : ?ConvertHelper_EOL |
| 1466 | 1466 | { |
| 1467 | 1467 | // 20 lines is enough to get a good picture of the newline style in the file. |
@@ -1474,18 +1474,18 @@ discard block |
||
| 1474 | 1474 | return ConvertHelper::detectEOLCharacter($string); |
| 1475 | 1475 | } |
| 1476 | 1476 | |
| 1477 | - /** |
|
| 1478 | - * Reads the specified amount of lines from the target file. |
|
| 1479 | - * Unicode BOM compatible: any byte order marker is stripped |
|
| 1480 | - * from the resulting lines. |
|
| 1481 | - * |
|
| 1482 | - * @param string $filePath |
|
| 1483 | - * @param int $amount Set to 0 to read all lines. |
|
| 1484 | - * @return array |
|
| 1485 | - * |
|
| 1486 | - * @see FileHelper::ERROR_CANNOT_OPEN_FILE_TO_READ_LINES |
|
| 1487 | - * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 1488 | - */ |
|
| 1477 | + /** |
|
| 1478 | + * Reads the specified amount of lines from the target file. |
|
| 1479 | + * Unicode BOM compatible: any byte order marker is stripped |
|
| 1480 | + * from the resulting lines. |
|
| 1481 | + * |
|
| 1482 | + * @param string $filePath |
|
| 1483 | + * @param int $amount Set to 0 to read all lines. |
|
| 1484 | + * @return array |
|
| 1485 | + * |
|
| 1486 | + * @see FileHelper::ERROR_CANNOT_OPEN_FILE_TO_READ_LINES |
|
| 1487 | + * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 1488 | + */ |
|
| 1489 | 1489 | public static function readLines(string $filePath, int $amount=0) : array |
| 1490 | 1490 | { |
| 1491 | 1491 | self::requireFileExists($filePath); |
@@ -1538,16 +1538,16 @@ discard block |
||
| 1538 | 1538 | return $result; |
| 1539 | 1539 | } |
| 1540 | 1540 | |
| 1541 | - /** |
|
| 1542 | - * Reads all content from a file. |
|
| 1543 | - * |
|
| 1544 | - * @param string $filePath |
|
| 1545 | - * @throws FileHelper_Exception |
|
| 1546 | - * @return string |
|
| 1547 | - * |
|
| 1548 | - * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 1549 | - * @see FileHelper::ERROR_CANNOT_READ_FILE_CONTENTS |
|
| 1550 | - */ |
|
| 1541 | + /** |
|
| 1542 | + * Reads all content from a file. |
|
| 1543 | + * |
|
| 1544 | + * @param string $filePath |
|
| 1545 | + * @throws FileHelper_Exception |
|
| 1546 | + * @return string |
|
| 1547 | + * |
|
| 1548 | + * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
| 1549 | + * @see FileHelper::ERROR_CANNOT_READ_FILE_CONTENTS |
|
| 1550 | + */ |
|
| 1551 | 1551 | public static function readContents(string $filePath) : string |
| 1552 | 1552 | { |
| 1553 | 1553 | self::requireFileExists($filePath); |
@@ -1568,18 +1568,18 @@ discard block |
||
| 1568 | 1568 | ); |
| 1569 | 1569 | } |
| 1570 | 1570 | |
| 1571 | - /** |
|
| 1572 | - * Ensures that the target path exists on disk, and is a folder. |
|
| 1573 | - * |
|
| 1574 | - * @param string $path |
|
| 1575 | - * @return string The real path, with normalized slashes. |
|
| 1576 | - * @throws FileHelper_Exception |
|
| 1577 | - * |
|
| 1578 | - * @see FileHelper::normalizePath() |
|
| 1579 | - * |
|
| 1580 | - * @see FileHelper::ERROR_FOLDER_DOES_NOT_EXIST |
|
| 1581 | - * @see FileHelper::ERROR_PATH_IS_NOT_A_FOLDER |
|
| 1582 | - */ |
|
| 1571 | + /** |
|
| 1572 | + * Ensures that the target path exists on disk, and is a folder. |
|
| 1573 | + * |
|
| 1574 | + * @param string $path |
|
| 1575 | + * @return string The real path, with normalized slashes. |
|
| 1576 | + * @throws FileHelper_Exception |
|
| 1577 | + * |
|
| 1578 | + * @see FileHelper::normalizePath() |
|
| 1579 | + * |
|
| 1580 | + * @see FileHelper::ERROR_FOLDER_DOES_NOT_EXIST |
|
| 1581 | + * @see FileHelper::ERROR_PATH_IS_NOT_A_FOLDER |
|
| 1582 | + */ |
|
| 1583 | 1583 | public static function requireFolderExists(string $path) : string |
| 1584 | 1584 | { |
| 1585 | 1585 | $actual = realpath($path); |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | |
| 85 | 85 | $contents = file_get_contents($file); |
| 86 | 86 | |
| 87 | - if($contents === false) |
|
| 87 | + if ($contents === false) |
|
| 88 | 88 | { |
| 89 | 89 | throw new FileHelper_Exception( |
| 90 | 90 | 'Cannot load serialized content from file.', |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | |
| 99 | 99 | $result = @unserialize($contents); |
| 100 | 100 | |
| 101 | - if($result !== false) { |
|
| 101 | + if ($result !== false) { |
|
| 102 | 102 | return $result; |
| 103 | 103 | } |
| 104 | 104 | |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | public static function deleteTree($rootFolder) |
| 116 | 116 | { |
| 117 | - if(!file_exists($rootFolder)) { |
|
| 117 | + if (!file_exists($rootFolder)) { |
|
| 118 | 118 | return true; |
| 119 | 119 | } |
| 120 | 120 | |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | */ |
| 156 | 156 | public static function createFolder($path) |
| 157 | 157 | { |
| 158 | - if(is_dir($path) || mkdir($path, 0777, true)) { |
|
| 158 | + if (is_dir($path) || mkdir($path, 0777, true)) { |
|
| 159 | 159 | return; |
| 160 | 160 | } |
| 161 | 161 | |
@@ -190,11 +190,11 @@ discard block |
||
| 190 | 190 | |
| 191 | 191 | if ($item->isDir()) |
| 192 | 192 | { |
| 193 | - FileHelper::copyTree($itemPath, $target . '/' . $baseName); |
|
| 193 | + FileHelper::copyTree($itemPath, $target.'/'.$baseName); |
|
| 194 | 194 | } |
| 195 | - else if($item->isFile()) |
|
| 195 | + else if ($item->isFile()) |
|
| 196 | 196 | { |
| 197 | - self::copyFile($itemPath, $target . '/' . $baseName); |
|
| 197 | + self::copyFile($itemPath, $target.'/'.$baseName); |
|
| 198 | 198 | } |
| 199 | 199 | } |
| 200 | 200 | } |
@@ -222,11 +222,11 @@ discard block |
||
| 222 | 222 | |
| 223 | 223 | $targetFolder = dirname($targetPath); |
| 224 | 224 | |
| 225 | - if(!file_exists($targetFolder)) |
|
| 225 | + if (!file_exists($targetFolder)) |
|
| 226 | 226 | { |
| 227 | 227 | self::createFolder($targetFolder); |
| 228 | 228 | } |
| 229 | - else if(!is_writable($targetFolder)) |
|
| 229 | + else if (!is_writable($targetFolder)) |
|
| 230 | 230 | { |
| 231 | 231 | throw new FileHelper_Exception( |
| 232 | 232 | sprintf('Target folder [%s] is not writable.', basename($targetFolder)), |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | ); |
| 239 | 239 | } |
| 240 | 240 | |
| 241 | - if(copy($sourcePath, $targetPath)) { |
|
| 241 | + if (copy($sourcePath, $targetPath)) { |
|
| 242 | 242 | return; |
| 243 | 243 | } |
| 244 | 244 | |
@@ -265,11 +265,11 @@ discard block |
||
| 265 | 265 | */ |
| 266 | 266 | public static function deleteFile(string $filePath) : void |
| 267 | 267 | { |
| 268 | - if(!file_exists($filePath)) { |
|
| 268 | + if (!file_exists($filePath)) { |
|
| 269 | 269 | return; |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - if(unlink($filePath)) { |
|
| 272 | + if (unlink($filePath)) { |
|
| 273 | 273 | return; |
| 274 | 274 | } |
| 275 | 275 | |
@@ -293,10 +293,10 @@ discard block |
||
| 293 | 293 | * @return Csv |
| 294 | 294 | * @see CSVHelper::createParser() |
| 295 | 295 | */ |
| 296 | - public static function createCSVParser(string $delimiter = ';', string $enclosure = '"', string $escape = '\\', bool $heading=false) : Csv |
|
| 296 | + public static function createCSVParser(string $delimiter = ';', string $enclosure = '"', string $escape = '\\', bool $heading = false) : Csv |
|
| 297 | 297 | { |
| 298 | - if($delimiter==='') { $delimiter = ';'; } |
|
| 299 | - if($enclosure==='') { $enclosure = '"'; } |
|
| 298 | + if ($delimiter === '') { $delimiter = ';'; } |
|
| 299 | + if ($enclosure === '') { $enclosure = '"'; } |
|
| 300 | 300 | |
| 301 | 301 | $parser = CSVHelper::createParser($delimiter); |
| 302 | 302 | $parser->enclosure = $enclosure; |
@@ -320,11 +320,11 @@ discard block |
||
| 320 | 320 | * @see parseCSVFile() |
| 321 | 321 | * @see FileHelper::ERROR_PARSING_CSV |
| 322 | 322 | */ |
| 323 | - public static function parseCSVString(string $csv, string $delimiter = ';', string $enclosure = '"', string $escape = '\\', bool $heading=false) : array |
|
| 323 | + public static function parseCSVString(string $csv, string $delimiter = ';', string $enclosure = '"', string $escape = '\\', bool $heading = false) : array |
|
| 324 | 324 | { |
| 325 | 325 | $parser = self::createCSVParser($delimiter, $enclosure, '\\', $heading); |
| 326 | 326 | |
| 327 | - if($parser->parse($csv)) |
|
| 327 | + if ($parser->parse($csv)) |
|
| 328 | 328 | { |
| 329 | 329 | return $parser->data; |
| 330 | 330 | } |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
| 353 | 353 | * @see FileHelper::ERROR_CANNOT_READ_FILE_CONTENTS |
| 354 | 354 | */ |
| 355 | - public static function parseCSVFile(string $filePath, string $delimiter = ';', string $enclosure = '"', string $escape = '\\', bool $heading=false) : array |
|
| 355 | + public static function parseCSVFile(string $filePath, string $delimiter = ';', string $enclosure = '"', string $escape = '\\', bool $heading = false) : array |
|
| 356 | 356 | { |
| 357 | 357 | $content = self::readContents($filePath); |
| 358 | 358 | |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | public static function detectMimeType(string $fileName) : ?string |
| 370 | 370 | { |
| 371 | 371 | $ext = self::getExtension($fileName); |
| 372 | - if(empty($ext)) { |
|
| 372 | + if (empty($ext)) { |
|
| 373 | 373 | return null; |
| 374 | 374 | } |
| 375 | 375 | |
@@ -390,11 +390,11 @@ discard block |
||
| 390 | 390 | * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
| 391 | 391 | * @see FileHelper::ERROR_UNKNOWN_FILE_MIME_TYPE |
| 392 | 392 | */ |
| 393 | - public static function sendFile(string $filePath, $fileName = null, bool $asAttachment=true) |
|
| 393 | + public static function sendFile(string $filePath, $fileName = null, bool $asAttachment = true) |
|
| 394 | 394 | { |
| 395 | 395 | self::requireFileExists($filePath); |
| 396 | 396 | |
| 397 | - if(empty($fileName)) { |
|
| 397 | + if (empty($fileName)) { |
|
| 398 | 398 | $fileName = basename($filePath); |
| 399 | 399 | } |
| 400 | 400 | |
@@ -412,10 +412,10 @@ discard block |
||
| 412 | 412 | |
| 413 | 413 | header("Cache-Control: public", true); |
| 414 | 414 | header("Content-Description: File Transfer", true); |
| 415 | - header("Content-Type: " . $mime, true); |
|
| 415 | + header("Content-Type: ".$mime, true); |
|
| 416 | 416 | |
| 417 | 417 | $disposition = 'inline'; |
| 418 | - if($asAttachment) { |
|
| 418 | + if ($asAttachment) { |
|
| 419 | 419 | $disposition = 'attachment'; |
| 420 | 420 | } |
| 421 | 421 | |
@@ -441,7 +441,7 @@ discard block |
||
| 441 | 441 | public static function downloadFile($url) |
| 442 | 442 | { |
| 443 | 443 | $ch = curl_init(); |
| 444 | - if(!is_resource($ch)) |
|
| 444 | + if (!is_resource($ch)) |
|
| 445 | 445 | { |
| 446 | 446 | throw new FileHelper_Exception( |
| 447 | 447 | 'Could not initialize a new cURL instance.', |
@@ -462,7 +462,7 @@ discard block |
||
| 462 | 462 | |
| 463 | 463 | $output = curl_exec($ch); |
| 464 | 464 | |
| 465 | - if($output === false) { |
|
| 465 | + if ($output === false) { |
|
| 466 | 466 | throw new FileHelper_Exception( |
| 467 | 467 | 'Unable to open URL', |
| 468 | 468 | sprintf( |
@@ -476,7 +476,7 @@ discard block |
||
| 476 | 476 | |
| 477 | 477 | curl_close($ch); |
| 478 | 478 | |
| 479 | - if(is_string($output)) |
|
| 479 | + if (is_string($output)) |
|
| 480 | 480 | { |
| 481 | 481 | return $output; |
| 482 | 482 | } |
@@ -498,7 +498,7 @@ discard block |
||
| 498 | 498 | */ |
| 499 | 499 | public static function isPHPFile($pathOrDirIterator) |
| 500 | 500 | { |
| 501 | - if(self::getExtension($pathOrDirIterator) == 'php') { |
|
| 501 | + if (self::getExtension($pathOrDirIterator) == 'php') { |
|
| 502 | 502 | return true; |
| 503 | 503 | } |
| 504 | 504 | |
@@ -515,14 +515,14 @@ discard block |
||
| 515 | 515 | */ |
| 516 | 516 | public static function getExtension($pathOrDirIterator, bool $lowercase = true) : string |
| 517 | 517 | { |
| 518 | - if($pathOrDirIterator instanceof \DirectoryIterator) { |
|
| 518 | + if ($pathOrDirIterator instanceof \DirectoryIterator) { |
|
| 519 | 519 | $filename = $pathOrDirIterator->getFilename(); |
| 520 | 520 | } else { |
| 521 | 521 | $filename = basename($pathOrDirIterator); |
| 522 | 522 | } |
| 523 | 523 | |
| 524 | 524 | $ext = pathinfo($filename, PATHINFO_EXTENSION); |
| 525 | - if($lowercase) { |
|
| 525 | + if ($lowercase) { |
|
| 526 | 526 | $ext = mb_strtolower($ext); |
| 527 | 527 | } |
| 528 | 528 | |
@@ -544,13 +544,13 @@ discard block |
||
| 544 | 544 | public static function getFilename($pathOrDirIterator, $extension = true) |
| 545 | 545 | { |
| 546 | 546 | $path = $pathOrDirIterator; |
| 547 | - if($pathOrDirIterator instanceof \DirectoryIterator) { |
|
| 547 | + if ($pathOrDirIterator instanceof \DirectoryIterator) { |
|
| 548 | 548 | $path = $pathOrDirIterator->getFilename(); |
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | $path = self::normalizePath($path); |
| 552 | 552 | |
| 553 | - if(!$extension) { |
|
| 553 | + if (!$extension) { |
|
| 554 | 554 | return pathinfo($path, PATHINFO_FILENAME); |
| 555 | 555 | } |
| 556 | 556 | |
@@ -568,12 +568,12 @@ discard block |
||
| 568 | 568 | * @see FileHelper::ERROR_CANNOT_FIND_JSON_FILE |
| 569 | 569 | * @see FileHelper::ERROR_CANNOT_DECODE_JSON_FILE |
| 570 | 570 | */ |
| 571 | - public static function parseJSONFile(string $file, $targetEncoding=null, $sourceEncoding=null) |
|
| 571 | + public static function parseJSONFile(string $file, $targetEncoding = null, $sourceEncoding = null) |
|
| 572 | 572 | { |
| 573 | 573 | self::requireFileExists($file, self::ERROR_CANNOT_FIND_JSON_FILE); |
| 574 | 574 | |
| 575 | 575 | $content = file_get_contents($file); |
| 576 | - if(!$content) { |
|
| 576 | + if (!$content) { |
|
| 577 | 577 | throw new FileHelper_Exception( |
| 578 | 578 | 'Cannot get file contents', |
| 579 | 579 | sprintf( |
@@ -584,12 +584,12 @@ discard block |
||
| 584 | 584 | ); |
| 585 | 585 | } |
| 586 | 586 | |
| 587 | - if(isset($targetEncoding)) { |
|
| 587 | + if (isset($targetEncoding)) { |
|
| 588 | 588 | $content = mb_convert_encoding($content, $targetEncoding, $sourceEncoding); |
| 589 | 589 | } |
| 590 | 590 | |
| 591 | 591 | $json = json_decode($content, true); |
| 592 | - if($json === false || $json === NULL) { |
|
| 592 | + if ($json === false || $json === NULL) { |
|
| 593 | 593 | throw new FileHelper_Exception( |
| 594 | 594 | 'Cannot decode json data', |
| 595 | 595 | sprintf( |
@@ -629,13 +629,13 @@ discard block |
||
| 629 | 629 | |
| 630 | 630 | $name = str_replace(array_keys($replaces), array_values($replaces), $name); |
| 631 | 631 | |
| 632 | - while(strstr($name, ' ')) { |
|
| 632 | + while (strstr($name, ' ')) { |
|
| 633 | 633 | $name = str_replace(' ', ' ', $name); |
| 634 | 634 | } |
| 635 | 635 | |
| 636 | 636 | $name = str_replace(array_keys($replaces), array_values($replaces), $name); |
| 637 | 637 | |
| 638 | - while(strstr($name, '..')) { |
|
| 638 | + while (strstr($name, '..')) { |
|
| 639 | 639 | $name = str_replace('..', '.', $name); |
| 640 | 640 | } |
| 641 | 641 | |
@@ -667,7 +667,7 @@ discard block |
||
| 667 | 667 | * @return array An indexed array with files. |
| 668 | 668 | * @see FileHelper::createFileFinder() |
| 669 | 669 | */ |
| 670 | - public static function findHTMLFiles(string $targetFolder, array $options=array()) : array |
|
| 670 | + public static function findHTMLFiles(string $targetFolder, array $options = array()) : array |
|
| 671 | 671 | { |
| 672 | 672 | return self::findFiles($targetFolder, array('html'), $options); |
| 673 | 673 | } |
@@ -684,7 +684,7 @@ discard block |
||
| 684 | 684 | * @return array An indexed array of PHP files. |
| 685 | 685 | * @see FileHelper::createFileFinder() |
| 686 | 686 | */ |
| 687 | - public static function findPHPFiles(string $targetFolder, array $options=array()) : array |
|
| 687 | + public static function findPHPFiles(string $targetFolder, array $options = array()) : array |
|
| 688 | 688 | { |
| 689 | 689 | return self::findFiles($targetFolder, array('php'), $options); |
| 690 | 690 | } |
@@ -704,22 +704,22 @@ discard block |
||
| 704 | 704 | * @return array |
| 705 | 705 | * @see FileHelper::createFileFinder() |
| 706 | 706 | */ |
| 707 | - public static function findFiles(string $targetFolder, array $extensions=array(), array $options=array(), array $files=array()) : array |
|
| 707 | + public static function findFiles(string $targetFolder, array $extensions = array(), array $options = array(), array $files = array()) : array |
|
| 708 | 708 | { |
| 709 | 709 | $finder = self::createFileFinder($targetFolder); |
| 710 | 710 | |
| 711 | 711 | $finder->setPathmodeStrip(); |
| 712 | 712 | |
| 713 | - if(isset($options['relative-path']) && $options['relative-path'] === true) |
|
| 713 | + if (isset($options['relative-path']) && $options['relative-path'] === true) |
|
| 714 | 714 | { |
| 715 | 715 | $finder->setPathmodeRelative(); |
| 716 | 716 | } |
| 717 | - else if(isset($options['absolute-path']) && $options['absolute-path'] === true) |
|
| 717 | + else if (isset($options['absolute-path']) && $options['absolute-path'] === true) |
|
| 718 | 718 | { |
| 719 | 719 | $finder->setPathmodeAbsolute(); |
| 720 | 720 | } |
| 721 | 721 | |
| 722 | - if(isset($options['strip-extension'])) |
|
| 722 | + if (isset($options['strip-extension'])) |
|
| 723 | 723 | { |
| 724 | 724 | $finder->stripExtensions(); |
| 725 | 725 | } |
@@ -737,12 +737,12 @@ discard block |
||
| 737 | 737 | * @param bool $keepPath Whether to keep the path component, if any. Default PHP pathinfo behavior is not to. |
| 738 | 738 | * @return string |
| 739 | 739 | */ |
| 740 | - public static function removeExtension(string $filename, bool $keepPath=false) : string |
|
| 740 | + public static function removeExtension(string $filename, bool $keepPath = false) : string |
|
| 741 | 741 | { |
| 742 | 742 | // normalize paths to allow windows style slashes even on nix servers |
| 743 | 743 | $filename = self::normalizePath($filename); |
| 744 | 744 | |
| 745 | - if(!$keepPath) |
|
| 745 | + if (!$keepPath) |
|
| 746 | 746 | { |
| 747 | 747 | return pathinfo($filename, PATHINFO_FILENAME); |
| 748 | 748 | } |
@@ -775,7 +775,7 @@ discard block |
||
| 775 | 775 | public static function detectUTFBom(string $filename) : ?string |
| 776 | 776 | { |
| 777 | 777 | $fp = fopen($filename, 'r'); |
| 778 | - if($fp === false) |
|
| 778 | + if ($fp === false) |
|
| 779 | 779 | { |
| 780 | 780 | throw new FileHelper_Exception( |
| 781 | 781 | 'Cannot open file for reading', |
@@ -790,10 +790,10 @@ discard block |
||
| 790 | 790 | |
| 791 | 791 | $boms = self::getUTFBOMs(); |
| 792 | 792 | |
| 793 | - foreach($boms as $bom => $value) |
|
| 793 | + foreach ($boms as $bom => $value) |
|
| 794 | 794 | { |
| 795 | 795 | $length = mb_strlen($value); |
| 796 | - if(mb_substr($text, 0, $length) == $value) { |
|
| 796 | + if (mb_substr($text, 0, $length) == $value) { |
|
| 797 | 797 | return $bom; |
| 798 | 798 | } |
| 799 | 799 | } |
@@ -812,13 +812,13 @@ discard block |
||
| 812 | 812 | */ |
| 813 | 813 | public static function getUTFBOMs() |
| 814 | 814 | { |
| 815 | - if(!isset(self::$utfBoms)) { |
|
| 815 | + if (!isset(self::$utfBoms)) { |
|
| 816 | 816 | self::$utfBoms = array( |
| 817 | - 'UTF32-BE' => chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF), |
|
| 818 | - 'UTF32-LE' => chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00), |
|
| 819 | - 'UTF16-BE' => chr(0xFE) . chr(0xFF), |
|
| 820 | - 'UTF16-LE' => chr(0xFF) . chr(0xFE), |
|
| 821 | - 'UTF8' => chr(0xEF) . chr(0xBB) . chr(0xBF) |
|
| 817 | + 'UTF32-BE' => chr(0x00).chr(0x00).chr(0xFE).chr(0xFF), |
|
| 818 | + 'UTF32-LE' => chr(0xFF).chr(0xFE).chr(0x00).chr(0x00), |
|
| 819 | + 'UTF16-BE' => chr(0xFE).chr(0xFF), |
|
| 820 | + 'UTF16-LE' => chr(0xFF).chr(0xFE), |
|
| 821 | + 'UTF8' => chr(0xEF).chr(0xBB).chr(0xBF) |
|
| 822 | 822 | ); |
| 823 | 823 | } |
| 824 | 824 | |
@@ -839,7 +839,7 @@ discard block |
||
| 839 | 839 | $encodings = self::getKnownUnicodeEncodings(); |
| 840 | 840 | |
| 841 | 841 | $keep = array(); |
| 842 | - foreach($encodings as $string) |
|
| 842 | + foreach ($encodings as $string) |
|
| 843 | 843 | { |
| 844 | 844 | $withHyphen = str_replace('UTF', 'UTF-', $string); |
| 845 | 845 | |
@@ -886,16 +886,16 @@ discard block |
||
| 886 | 886 | * @see FileHelper::ERROR_SAVE_FILE_NOT_WRITABLE |
| 887 | 887 | * @see FileHelper::ERROR_SAVE_FILE_WRITE_FAILED |
| 888 | 888 | */ |
| 889 | - public static function saveAsJSON($data, string $file, bool $pretty=false) |
|
| 889 | + public static function saveAsJSON($data, string $file, bool $pretty = false) |
|
| 890 | 890 | { |
| 891 | 891 | $options = null; |
| 892 | - if($pretty) { |
|
| 892 | + if ($pretty) { |
|
| 893 | 893 | $options = JSON_PRETTY_PRINT; |
| 894 | 894 | } |
| 895 | 895 | |
| 896 | 896 | $json = json_encode($data, $options); |
| 897 | 897 | |
| 898 | - if($json===false) |
|
| 898 | + if ($json === false) |
|
| 899 | 899 | { |
| 900 | 900 | $errorCode = json_last_error(); |
| 901 | 901 | |
@@ -921,14 +921,14 @@ discard block |
||
| 921 | 921 | * @see FileHelper::ERROR_SAVE_FILE_NOT_WRITABLE |
| 922 | 922 | * @see FileHelper::ERROR_SAVE_FILE_WRITE_FAILED |
| 923 | 923 | */ |
| 924 | - public static function saveFile(string $filePath, string $content='') : void |
|
| 924 | + public static function saveFile(string $filePath, string $content = '') : void |
|
| 925 | 925 | { |
| 926 | 926 | $filePath = self::normalizePath($filePath); |
| 927 | 927 | |
| 928 | 928 | // target file already exists |
| 929 | - if(file_exists($filePath)) |
|
| 929 | + if (file_exists($filePath)) |
|
| 930 | 930 | { |
| 931 | - if(!is_writable($filePath)) |
|
| 931 | + if (!is_writable($filePath)) |
|
| 932 | 932 | { |
| 933 | 933 | throw new FileHelper_Exception( |
| 934 | 934 | sprintf('Cannot save file: target file [%s] exists, but is not writable.', basename($filePath)), |
@@ -948,7 +948,7 @@ discard block |
||
| 948 | 948 | // create the folder as needed |
| 949 | 949 | self::createFolder($targetFolder); |
| 950 | 950 | |
| 951 | - if(!is_writable($targetFolder)) |
|
| 951 | + if (!is_writable($targetFolder)) |
|
| 952 | 952 | { |
| 953 | 953 | throw new FileHelper_Exception( |
| 954 | 954 | sprintf('Cannot save file: target folder [%s] is not writable.', basename($targetFolder)), |
@@ -961,7 +961,7 @@ discard block |
||
| 961 | 961 | } |
| 962 | 962 | } |
| 963 | 963 | |
| 964 | - if(is_dir($filePath)) |
|
| 964 | + if (is_dir($filePath)) |
|
| 965 | 965 | { |
| 966 | 966 | throw new FileHelper_Exception( |
| 967 | 967 | sprintf('Cannot save file: the target [%s] is a directory.', basename($filePath)), |
@@ -973,7 +973,7 @@ discard block |
||
| 973 | 973 | ); |
| 974 | 974 | } |
| 975 | 975 | |
| 976 | - if(file_put_contents($filePath, $content) !== false) { |
|
| 976 | + if (file_put_contents($filePath, $content) !== false) { |
|
| 977 | 977 | return; |
| 978 | 978 | } |
| 979 | 979 | |
@@ -1012,7 +1012,7 @@ discard block |
||
| 1012 | 1012 | { |
| 1013 | 1013 | static $checked = array(); |
| 1014 | 1014 | |
| 1015 | - if(isset($checked[$command])) { |
|
| 1015 | + if (isset($checked[$command])) { |
|
| 1016 | 1016 | return $checked[$command]; |
| 1017 | 1017 | } |
| 1018 | 1018 | |
@@ -1025,7 +1025,7 @@ discard block |
||
| 1025 | 1025 | |
| 1026 | 1026 | $os = strtolower(PHP_OS_FAMILY); |
| 1027 | 1027 | |
| 1028 | - if(!isset($osCommands[$os])) |
|
| 1028 | + if (!isset($osCommands[$os])) |
|
| 1029 | 1029 | { |
| 1030 | 1030 | throw new FileHelper_Exception( |
| 1031 | 1031 | 'Unsupported OS for CLI commands', |
@@ -1051,7 +1051,7 @@ discard block |
||
| 1051 | 1051 | $pipes |
| 1052 | 1052 | ); |
| 1053 | 1053 | |
| 1054 | - if($process === false) { |
|
| 1054 | + if ($process === false) { |
|
| 1055 | 1055 | $checked[$command] = false; |
| 1056 | 1056 | return false; |
| 1057 | 1057 | } |
@@ -1082,7 +1082,7 @@ discard block |
||
| 1082 | 1082 | */ |
| 1083 | 1083 | public static function checkPHPFileSyntax($path) |
| 1084 | 1084 | { |
| 1085 | - if(!self::canMakePHPCalls()) { |
|
| 1085 | + if (!self::canMakePHPCalls()) { |
|
| 1086 | 1086 | return true; |
| 1087 | 1087 | } |
| 1088 | 1088 | |
@@ -1093,7 +1093,7 @@ discard block |
||
| 1093 | 1093 | // when the validation is successful, the first entry |
| 1094 | 1094 | // in the array contains the success message. When it |
| 1095 | 1095 | // is invalid, the first entry is always empty. |
| 1096 | - if(!empty($output[0])) { |
|
| 1096 | + if (!empty($output[0])) { |
|
| 1097 | 1097 | return true; |
| 1098 | 1098 | } |
| 1099 | 1099 | |
@@ -1114,7 +1114,7 @@ discard block |
||
| 1114 | 1114 | public static function getModifiedDate($path) |
| 1115 | 1115 | { |
| 1116 | 1116 | $time = filemtime($path); |
| 1117 | - if($time !== false) { |
|
| 1117 | + if ($time !== false) { |
|
| 1118 | 1118 | $date = new \DateTime(); |
| 1119 | 1119 | $date->setTimestamp($time); |
| 1120 | 1120 | return $date; |
@@ -1143,7 +1143,7 @@ discard block |
||
| 1143 | 1143 | */ |
| 1144 | 1144 | public static function getSubfolders($targetFolder, $options = array()) |
| 1145 | 1145 | { |
| 1146 | - if(!is_dir($targetFolder)) |
|
| 1146 | + if (!is_dir($targetFolder)) |
|
| 1147 | 1147 | { |
| 1148 | 1148 | throw new FileHelper_Exception( |
| 1149 | 1149 | 'Target folder does not exist', |
@@ -1167,29 +1167,29 @@ discard block |
||
| 1167 | 1167 | |
| 1168 | 1168 | $d = new \DirectoryIterator($targetFolder); |
| 1169 | 1169 | |
| 1170 | - foreach($d as $item) |
|
| 1170 | + foreach ($d as $item) |
|
| 1171 | 1171 | { |
| 1172 | - if($item->isDir() && !$item->isDot()) |
|
| 1172 | + if ($item->isDir() && !$item->isDot()) |
|
| 1173 | 1173 | { |
| 1174 | 1174 | $name = $item->getFilename(); |
| 1175 | 1175 | |
| 1176 | - if(!$options['absolute-path']) { |
|
| 1176 | + if (!$options['absolute-path']) { |
|
| 1177 | 1177 | $result[] = $name; |
| 1178 | 1178 | } else { |
| 1179 | 1179 | $result[] = $targetFolder.'/'.$name; |
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | - if(!$options['recursive']) |
|
| 1182 | + if (!$options['recursive']) |
|
| 1183 | 1183 | { |
| 1184 | 1184 | continue; |
| 1185 | 1185 | } |
| 1186 | 1186 | |
| 1187 | 1187 | $subs = self::getSubfolders($targetFolder.'/'.$name, $options); |
| 1188 | - foreach($subs as $sub) |
|
| 1188 | + foreach ($subs as $sub) |
|
| 1189 | 1189 | { |
| 1190 | 1190 | $relative = $name.'/'.$sub; |
| 1191 | 1191 | |
| 1192 | - if(!$options['absolute-path']) { |
|
| 1192 | + if (!$options['absolute-path']) { |
|
| 1193 | 1193 | $result[] = $relative; |
| 1194 | 1194 | } else { |
| 1195 | 1195 | $result[] = $targetFolder.'/'.$relative; |
@@ -1239,7 +1239,7 @@ discard block |
||
| 1239 | 1239 | $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size. |
| 1240 | 1240 | $size = floatval(preg_replace('/[^0-9\.]/', '', $size)); // Remove the non-numeric characters from the size. |
| 1241 | 1241 | |
| 1242 | - if($unit) |
|
| 1242 | + if ($unit) |
|
| 1243 | 1243 | { |
| 1244 | 1244 | // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by. |
| 1245 | 1245 | return round($size * pow(1024, stripos('bkmgtpezy', $unit[0]))); |
@@ -1258,7 +1258,7 @@ discard block |
||
| 1258 | 1258 | * @param int $depth The folder depth to reduce the path to |
| 1259 | 1259 | * @return string |
| 1260 | 1260 | */ |
| 1261 | - public static function relativizePathByDepth(string $path, int $depth=2) : string |
|
| 1261 | + public static function relativizePathByDepth(string $path, int $depth = 2) : string |
|
| 1262 | 1262 | { |
| 1263 | 1263 | $path = self::normalizePath($path); |
| 1264 | 1264 | |
@@ -1266,17 +1266,17 @@ discard block |
||
| 1266 | 1266 | $tokens = array_filter($tokens); // remove empty entries (trailing slash for example) |
| 1267 | 1267 | $tokens = array_values($tokens); // re-index keys |
| 1268 | 1268 | |
| 1269 | - if(empty($tokens)) { |
|
| 1269 | + if (empty($tokens)) { |
|
| 1270 | 1270 | return ''; |
| 1271 | 1271 | } |
| 1272 | 1272 | |
| 1273 | 1273 | // remove the drive if present |
| 1274 | - if(strstr($tokens[0], ':')) { |
|
| 1274 | + if (strstr($tokens[0], ':')) { |
|
| 1275 | 1275 | array_shift($tokens); |
| 1276 | 1276 | } |
| 1277 | 1277 | |
| 1278 | 1278 | // path was only the drive |
| 1279 | - if(count($tokens) == 0) { |
|
| 1279 | + if (count($tokens) == 0) { |
|
| 1280 | 1280 | return ''; |
| 1281 | 1281 | } |
| 1282 | 1282 | |
@@ -1285,8 +1285,8 @@ discard block |
||
| 1285 | 1285 | |
| 1286 | 1286 | // reduce the path to the specified depth |
| 1287 | 1287 | $length = count($tokens); |
| 1288 | - if($length > $depth) { |
|
| 1289 | - $tokens = array_slice($tokens, $length-$depth); |
|
| 1288 | + if ($length > $depth) { |
|
| 1289 | + $tokens = array_slice($tokens, $length - $depth); |
|
| 1290 | 1290 | } |
| 1291 | 1291 | |
| 1292 | 1292 | // append the last element again |
@@ -1334,14 +1334,14 @@ discard block |
||
| 1334 | 1334 | * |
| 1335 | 1335 | * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
| 1336 | 1336 | */ |
| 1337 | - public static function requireFileExists(string $path, ?int $errorCode=null) : string |
|
| 1337 | + public static function requireFileExists(string $path, ?int $errorCode = null) : string |
|
| 1338 | 1338 | { |
| 1339 | 1339 | $result = realpath($path); |
| 1340 | - if($result !== false) { |
|
| 1340 | + if ($result !== false) { |
|
| 1341 | 1341 | return $result; |
| 1342 | 1342 | } |
| 1343 | 1343 | |
| 1344 | - if($errorCode === null) { |
|
| 1344 | + if ($errorCode === null) { |
|
| 1345 | 1345 | $errorCode = self::ERROR_FILE_DOES_NOT_EXIST; |
| 1346 | 1346 | } |
| 1347 | 1347 | |
@@ -1352,15 +1352,15 @@ discard block |
||
| 1352 | 1352 | ); |
| 1353 | 1353 | } |
| 1354 | 1354 | |
| 1355 | - public static function requireFileReadable(string $path, ?int $errorCode=null) : string |
|
| 1355 | + public static function requireFileReadable(string $path, ?int $errorCode = null) : string |
|
| 1356 | 1356 | { |
| 1357 | 1357 | $path = self::requireFileExists($path, $errorCode); |
| 1358 | 1358 | |
| 1359 | - if(is_readable($path)) { |
|
| 1359 | + if (is_readable($path)) { |
|
| 1360 | 1360 | return $path; |
| 1361 | 1361 | } |
| 1362 | 1362 | |
| 1363 | - if($errorCode === null) { |
|
| 1363 | + if ($errorCode === null) { |
|
| 1364 | 1364 | $errorCode = self::ERROR_FILE_NOT_READABLE; |
| 1365 | 1365 | } |
| 1366 | 1366 | |
@@ -1389,15 +1389,15 @@ discard block |
||
| 1389 | 1389 | |
| 1390 | 1390 | $file = new \SplFileObject($path); |
| 1391 | 1391 | |
| 1392 | - if($file->eof()) { |
|
| 1392 | + if ($file->eof()) { |
|
| 1393 | 1393 | return ''; |
| 1394 | 1394 | } |
| 1395 | 1395 | |
| 1396 | - $targetLine = $lineNumber-1; |
|
| 1396 | + $targetLine = $lineNumber - 1; |
|
| 1397 | 1397 | |
| 1398 | 1398 | $file->seek($targetLine); |
| 1399 | 1399 | |
| 1400 | - if($file->key() !== $targetLine) { |
|
| 1400 | + if ($file->key() !== $targetLine) { |
|
| 1401 | 1401 | return null; |
| 1402 | 1402 | } |
| 1403 | 1403 | |
@@ -1423,7 +1423,7 @@ discard block |
||
| 1423 | 1423 | $number = $spl->key(); |
| 1424 | 1424 | |
| 1425 | 1425 | // if seeking to the end the cursor is still at 0, there are no lines. |
| 1426 | - if($number === 0) |
|
| 1426 | + if ($number === 0) |
|
| 1427 | 1427 | { |
| 1428 | 1428 | // since it's a very small file, to get reliable results, |
| 1429 | 1429 | // we read its contents and use that to determine what |
@@ -1431,13 +1431,13 @@ discard block |
||
| 1431 | 1431 | // that this is not pactical to solve with the SplFileObject. |
| 1432 | 1432 | $content = file_get_contents($path); |
| 1433 | 1433 | |
| 1434 | - if(empty($content)) { |
|
| 1434 | + if (empty($content)) { |
|
| 1435 | 1435 | return 0; |
| 1436 | 1436 | } |
| 1437 | 1437 | } |
| 1438 | 1438 | |
| 1439 | 1439 | // return the line number we were able to reach + 1 (key is zero-based) |
| 1440 | - return $number+1; |
|
| 1440 | + return $number + 1; |
|
| 1441 | 1441 | } |
| 1442 | 1442 | |
| 1443 | 1443 | /** |
@@ -1484,13 +1484,13 @@ discard block |
||
| 1484 | 1484 | * @see FileHelper::ERROR_CANNOT_OPEN_FILE_TO_READ_LINES |
| 1485 | 1485 | * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
| 1486 | 1486 | */ |
| 1487 | - public static function readLines(string $filePath, int $amount=0) : array |
|
| 1487 | + public static function readLines(string $filePath, int $amount = 0) : array |
|
| 1488 | 1488 | { |
| 1489 | 1489 | self::requireFileExists($filePath); |
| 1490 | 1490 | |
| 1491 | 1491 | $fn = fopen($filePath, "r"); |
| 1492 | 1492 | |
| 1493 | - if($fn === false) |
|
| 1493 | + if ($fn === false) |
|
| 1494 | 1494 | { |
| 1495 | 1495 | throw new FileHelper_Exception( |
| 1496 | 1496 | 'Could not open file for reading.', |
@@ -1506,19 +1506,19 @@ discard block |
||
| 1506 | 1506 | $counter = 0; |
| 1507 | 1507 | $first = true; |
| 1508 | 1508 | |
| 1509 | - while(!feof($fn)) |
|
| 1509 | + while (!feof($fn)) |
|
| 1510 | 1510 | { |
| 1511 | 1511 | $counter++; |
| 1512 | 1512 | |
| 1513 | 1513 | $line = fgets($fn); |
| 1514 | 1514 | |
| 1515 | 1515 | // can happen with zero length files |
| 1516 | - if($line === false) { |
|
| 1516 | + if ($line === false) { |
|
| 1517 | 1517 | continue; |
| 1518 | 1518 | } |
| 1519 | 1519 | |
| 1520 | 1520 | // the first line may contain a unicode BOM marker. |
| 1521 | - if($first) |
|
| 1521 | + if ($first) |
|
| 1522 | 1522 | { |
| 1523 | 1523 | $line = ConvertHelper::stripUTFBom($line); |
| 1524 | 1524 | $first = false; |
@@ -1526,7 +1526,7 @@ discard block |
||
| 1526 | 1526 | |
| 1527 | 1527 | $result[] = $line; |
| 1528 | 1528 | |
| 1529 | - if($amount > 0 && $counter == $amount) { |
|
| 1529 | + if ($amount > 0 && $counter == $amount) { |
|
| 1530 | 1530 | break; |
| 1531 | 1531 | } |
| 1532 | 1532 | } |
@@ -1552,7 +1552,7 @@ discard block |
||
| 1552 | 1552 | |
| 1553 | 1553 | $result = file_get_contents($filePath); |
| 1554 | 1554 | |
| 1555 | - if($result !== false) { |
|
| 1555 | + if ($result !== false) { |
|
| 1556 | 1556 | return $result; |
| 1557 | 1557 | } |
| 1558 | 1558 | |
@@ -1582,7 +1582,7 @@ discard block |
||
| 1582 | 1582 | { |
| 1583 | 1583 | $actual = realpath($path); |
| 1584 | 1584 | |
| 1585 | - if($actual === false) |
|
| 1585 | + if ($actual === false) |
|
| 1586 | 1586 | { |
| 1587 | 1587 | throw new FileHelper_Exception( |
| 1588 | 1588 | 'Folder does not exist', |
@@ -1594,7 +1594,7 @@ discard block |
||
| 1594 | 1594 | ); |
| 1595 | 1595 | } |
| 1596 | 1596 | |
| 1597 | - if(is_dir($path)) |
|
| 1597 | + if (is_dir($path)) |
|
| 1598 | 1598 | { |
| 1599 | 1599 | return self::normalizePath($actual); |
| 1600 | 1600 | } |
@@ -1616,7 +1616,7 @@ discard block |
||
| 1616 | 1616 | * @param string[] $paths |
| 1617 | 1617 | * @return FileHelper_PathsReducer |
| 1618 | 1618 | */ |
| 1619 | - public static function createPathsReducer(array $paths=array()) : FileHelper_PathsReducer |
|
| 1619 | + public static function createPathsReducer(array $paths = array()) : FileHelper_PathsReducer |
|
| 1620 | 1620 | { |
| 1621 | 1621 | return new FileHelper_PathsReducer(); |
| 1622 | 1622 | } |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | $autoload = $root.'/vendor/autoload.php'; |
| 16 | 16 | |
| 17 | 17 | // we need the autoloader to be present |
| 18 | - if($autoload === false) |
|
| 18 | + if ($autoload === false) |
|
| 19 | 19 | { |
| 20 | 20 | die('<b>ERROR:</b> Autoloader not present. Run composer update first.'); |
| 21 | 21 | } |
@@ -29,7 +29,7 @@ |
||
| 29 | 29 | |
| 30 | 30 | // In the case of tel URLs, we convert the syntax to use double |
| 31 | 31 | // slashes to make them parsable. |
| 32 | - if(strstr($url, 'tel:') !== false && strstr($url, 'tel://') === false) { |
|
| 32 | + if (strstr($url, 'tel:') !== false && strstr($url, 'tel://') === false) { |
|
| 33 | 33 | $url = str_replace('tel:', 'tel://', $url); |
| 34 | 34 | } |
| 35 | 35 | |
@@ -15,9 +15,9 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | public static function removeKeys(array &$sourceArray, array $keys) : void |
| 17 | 17 | { |
| 18 | - foreach($keys as $key) |
|
| 18 | + foreach ($keys as $key) |
|
| 19 | 19 | { |
| 20 | - if(array_key_exists($key, $sourceArray)) { |
|
| 20 | + if (array_key_exists($key, $sourceArray)) { |
|
| 21 | 21 | unset($sourceArray[$key]); |
| 22 | 22 | } |
| 23 | 23 | } |
@@ -30,18 +30,18 @@ discard block |
||
| 30 | 30 | * @param bool $keepKeys Whether to maintain index association |
| 31 | 31 | * @return array |
| 32 | 32 | */ |
| 33 | - public static function removeValues(array $sourceArray, array $values, bool $keepKeys=false) : array |
|
| 33 | + public static function removeValues(array $sourceArray, array $values, bool $keepKeys = false) : array |
|
| 34 | 34 | { |
| 35 | 35 | $result = array(); |
| 36 | 36 | $values = array_values($values); |
| 37 | 37 | |
| 38 | - foreach($sourceArray as $key => $value) |
|
| 38 | + foreach ($sourceArray as $key => $value) |
|
| 39 | 39 | { |
| 40 | - if(in_array($value, $values, true)) { |
|
| 40 | + if (in_array($value, $values, true)) { |
|
| 41 | 41 | continue; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - if($keepKeys) { |
|
| 44 | + if ($keepKeys) { |
|
| 45 | 45 | $result[$key] = $value; |
| 46 | 46 | continue; |
| 47 | 47 | } |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | public static function toStyleString(array $subject) : string |
| 75 | 75 | { |
| 76 | 76 | $tokens = array(); |
| 77 | - foreach($subject as $name => $value) { |
|
| 77 | + foreach ($subject as $name => $value) { |
|
| 78 | 78 | $tokens[] = $name.':'.strval($value); |
| 79 | 79 | } |
| 80 | 80 | |
@@ -103,18 +103,18 @@ discard block |
||
| 103 | 103 | public static function toAttributeString(array $array) : string |
| 104 | 104 | { |
| 105 | 105 | $tokens = array(); |
| 106 | - foreach($array as $attr => $value) |
|
| 106 | + foreach ($array as $attr => $value) |
|
| 107 | 107 | { |
| 108 | 108 | $value = strval($value); |
| 109 | 109 | |
| 110 | - if($value === '') { |
|
| 110 | + if ($value === '') { |
|
| 111 | 111 | continue; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | $tokens[] = $attr.'="'.htmlspecialchars($value, ENT_QUOTES, 'UTF-8').'"'; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - if(empty($tokens)) { |
|
| 117 | + if (empty($tokens)) { |
|
| 118 | 118 | return ''; |
| 119 | 119 | } |
| 120 | 120 | |
@@ -135,17 +135,17 @@ discard block |
||
| 135 | 135 | */ |
| 136 | 136 | public static function implodeWithAnd(array $list, string $sep = ', ', string $conjunction = '') : string |
| 137 | 137 | { |
| 138 | - if(empty($list)) { |
|
| 138 | + if (empty($list)) { |
|
| 139 | 139 | return ''; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - if(empty($conjunction)) { |
|
| 142 | + if (empty($conjunction)) { |
|
| 143 | 143 | $conjunction = ' '.t('and').' '; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | $last = array_pop($list); |
| 147 | - if($list) { |
|
| 148 | - return implode($sep, $list) . $conjunction . $last; |
|
| 147 | + if ($list) { |
|
| 148 | + return implode($sep, $list).$conjunction.$last; |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | return $last; |
@@ -29,17 +29,17 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | public static function fromString($string) : bool |
| 31 | 31 | { |
| 32 | - if($string === '' || $string === null || !is_scalar($string)) |
|
| 32 | + if ($string === '' || $string === null || !is_scalar($string)) |
|
| 33 | 33 | { |
| 34 | 34 | return false; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - if(is_bool($string)) |
|
| 37 | + if (is_bool($string)) |
|
| 38 | 38 | { |
| 39 | 39 | return $string; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - if(array_key_exists($string, self::$booleanStrings)) |
|
| 42 | + if (array_key_exists($string, self::$booleanStrings)) |
|
| 43 | 43 | { |
| 44 | 44 | return self::$booleanStrings[$string]; |
| 45 | 45 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | public static function toString($boolean, bool $yesno = false) : string |
| 68 | 68 | { |
| 69 | 69 | // allow 'yes', 'true', 'no', 'false' string notations as well |
| 70 | - if(!is_bool($boolean)) { |
|
| 70 | + if (!is_bool($boolean)) { |
|
| 71 | 71 | $boolean = self::fromString($boolean); |
| 72 | 72 | } |
| 73 | 73 | |
@@ -97,11 +97,11 @@ discard block |
||
| 97 | 97 | */ |
| 98 | 98 | public static function isBoolean($value) : bool |
| 99 | 99 | { |
| 100 | - if(is_bool($value)) { |
|
| 100 | + if (is_bool($value)) { |
|
| 101 | 101 | return true; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - if(!is_scalar($value)) { |
|
| 104 | + if (!is_scalar($value)) { |
|
| 105 | 105 | return false; |
| 106 | 106 | } |
| 107 | 107 | |