Advertisement
lignite0

PHP Useful functions

May 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.89 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Zapisuje zadane dane do pliku w formie łatwego do odczytu pliku PHP
  5.  *
  6.  * @param $file string Ścieżka absolutna do pliku, który zostanie zapisany
  7.  * @param $data mixed Dane, które zostaną wyeksportowane do pliku
  8.  * @return void
  9.  */
  10. function exportVariable2PHPFile(array $data, $file)
  11. {
  12.     makeFile($file);
  13.  
  14.     $date = date('Y-m-d H:i:s');
  15.     $export = var_export($data, true);
  16.  
  17.     $content = "<?php\n\n/** @generated {$date} */\n\nreturn {$export};";
  18.     file_put_contents($file, $content);
  19. }
  20.  
  21. /**
  22.  * Wyszukuje plików na dysku
  23.  *
  24.  * @param $pattern string Wyrażenie, tak samo jak w glob
  25.  * @param int $flags Flagi, tak samo jak w przypadku glob
  26.  * @return array Zwraca płaską tablicę z ścieżkami do znalezionych plików
  27.  */
  28. function globRecursive($pattern, $flags = 0)
  29. {
  30.     $files = glob($pattern, $flags);
  31.     foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
  32.         $files = array_merge($files, globRecursive($dir . '/' . basename($pattern), $flags));
  33.     }
  34.  
  35.     return $files;
  36. }
  37.  
  38. /**
  39.  * Tworzy rekursywnie katalogi w ścieżce
  40.  *
  41.  * @param $path string Ścieżka absolutna do katalogów które chcesz stworzyć
  42.  * @param int $mode Prawa dostępu do katalogu
  43.  * @return void
  44.  */
  45. function makeDirsRecursive($path, $mode = 0777)
  46. {
  47.     $path = rtrim($path, '/');
  48.     if (empty($path)) {
  49.         return;
  50.     }
  51.  
  52.     $dirs = explode('/', $path);
  53.     $dir = '';
  54.  
  55.     foreach ($dirs as $part) {
  56.         $dir .= $part . '/';
  57.         if (is_dir($dir)) {
  58.             continue;
  59.         }
  60.         mkdir($dir, $mode);
  61.     }
  62. }
  63.  
  64. /**
  65.  * Tworzy plik oraz katalogi, jeżeli ich brakuje
  66.  *
  67.  * @param $filepath string Ścieżka absolutna do pliku, który chcesz stworzyć
  68.  * @param int $mode Prawa dostępu do pliku
  69.  * @return void
  70.  */
  71. function makeFile($filepath, $mode = 0775)
  72. {
  73.     makeDirsRecursive(dirname($filepath), 0777);
  74.     if (!file_exists($filepath)) {
  75.         touch($filepath);
  76.     }
  77.     chmod($filepath, $mode);
  78. }
  79.  
  80. /**
  81.  * Zamienia litery za średnikami na wielkie. Usuwa myśliki
  82.  *
  83.  * /example-request => /exampleRequest
  84.  *
  85.  * @param $string string Dowolny ciąg znaków
  86.  * @return string Zamieniony tekst
  87.  */
  88. function transformHyphen2CamelCase($string)
  89. {
  90.     return preg_replace_callback('~\-([.])~', function ($matches) {
  91.         return strtoupper($matches[1]);
  92.     }, $string);
  93. }
  94.  
  95. /**
  96.  * Zamienia wielkie litery na małe i dodaje przed nimi średniki
  97.  *
  98.  * /exampleRequest => /example-request
  99.  *
  100.  * @param $string string Dowolny ciąg znaków
  101.  * @return string Zamieniony tekst
  102.  */
  103. function transformCamelCase2Hyphen($string)
  104. {
  105.     return trim(preg_replace_callback('~([A-Z])~', function ($matches) {
  106.         return '-'.strtolower($matches[1]);
  107.     }, $string), '-');
  108. }
  109.  
  110.  
  111. function debugXMLReader(XMLReader $xml)
  112. {
  113.     $nodeTypes = [
  114.         0 => 'NONE',
  115.         1 => 'ELEMENT',
  116.         2 => 'ATTRIBUTE',
  117.         3 => 'TEXT',
  118.         4 => 'CDATA',
  119.         5 => 'ENTITY_REF',
  120.         6 => 'ENTITY',
  121.         7 => 'PI',
  122.         8 => 'COMMENT',
  123.         9 => 'DOC',
  124.         10 => 'DOC_TYPE',
  125.         11 => 'DOC_FRAGMENT',
  126.         12 => 'NOTATION',
  127.         13 => 'WHITESPACE',
  128.         14 => 'SIGNIFICANT_WHITESPACE',
  129.         15 => 'END_ELEMENT',
  130.         16 => 'END_ENTITY',
  131.         17 => 'XML_DECLARATION',
  132.     ];
  133.  
  134.     $properties = [
  135.         'name',
  136.         'localName',
  137.         'attributeCount',
  138.         'depth',
  139.         'hasAttributes',
  140.         'hasValue',
  141.         'isDefault',
  142.         'isEmptyElement',
  143.         'prefix',
  144.         'xmlLang',
  145.         'namespaceURI',
  146.         'baseURI',
  147.         'value',
  148.     ];
  149.  
  150.     echo "==================================================\n";
  151.     echo "nodeType       : ({$xml->nodeType}) {$nodeTypes[$xml->nodeType]}\n";
  152.     foreach ($properties as $property) {
  153.         printf("%-14s : %s(%s)\n", $property, gettype($xml->{$property}), var_export($xml->{$property}, true));
  154.     }
  155.  
  156.     echo "readInnerXML() : {$xml->readInnerXml()}\n";
  157.     echo "readOuterXML() : {$xml->readOuterXml()}\n";
  158.     echo "readString()   : {$xml->readString()}\n";
  159.     echo "\n\n";
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement