Skip to content

Commit

Permalink
Merge pull request #2 from four-eyes-04-04/april
Browse files Browse the repository at this point in the history
various arrays [JS, PHP]
  • Loading branch information
aprilmintacpineda authored Jun 29, 2017
2 parents 6026a3d + 5c3ca25 commit 9777801
Show file tree
Hide file tree
Showing 9 changed files with 6,324 additions and 18 deletions.
81 changes: 81 additions & 0 deletions classes/Make.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

class Make {
public static function jsonFile(Array $collection) {
try {
if(!is_dir('combined/')) throw new Exception('Directory combined not found.');

$combined_file = fopen('combined/quotes.json', 'w+');
fwrite($combined_file, json_encode($collection, JSON_PRETTY_PRINT));
fclose($combined_file);

return true;
} catch(Exception $e) {
return $e->getMessage();
}
}

public static function textFile(Array $collection) {
try {
if(!is_dir('combined/')) throw new Exception('Directory **combined** not found.');

$stringed = '';

foreach($collection as $item) {
$stringed .= $item['quote']. ' ~ '. $item['name']. "\n";
}

$combined_file = fopen('combined/quotes.txt', 'w+');
fwrite($combined_file, $stringed);
fclose($combined_file);

return true;
} catch(Exception $e) {
return $e->getMessage();
}
}

public static function variousArrays(Array $collection) {
try {
if(!is_dir('combined/')) throw new Exception('Directory combined not found.');

// PHP arrays
$numeric = "// Numeric array of quotes\n// Each array is a quote\n// With the author after the `~`\n". '$numeric = ['. "\n";
$assoc = "// Associative array of quotes\n// Each array is an associative array of quotes\n// With keys `quote` for the body and `name` for the author\n". '$assoc = [' . "\n";
$javaScript = "// JavaScript array\nvar quotes = [\n";

foreach($collection as $item) {
$numeric .= "\t'". addslashes($item['quote']) ." ~ ". addslashes($item['name']) ."',\n";
$javaScript .= "\t'". addslashes($item['quote']) ." ~ ". addslashes($item['name']) ."',\n";
}

foreach($collection as $item) {
$assoc .= "\t[\n\t\t'quote' => '". addslashes($item['quote']) ."',\n\t\t'name' => '". addslashes($item['name']) ."'\n\t],\n";
}

$numeric .= '];';
$assoc .= '];';
$javaScript .= '];';

$numeric = "<?php\n\n{$numeric}\n";
$assoc = "<?php\n\n{$assoc}\n";

// create PHP numeric arrays
$combined_file = fopen('combined/quotes_numeric_array.php', 'w+');
fwrite($combined_file, $numeric);
fclose($combined_file);
// create PHP associative arrays
$combined_file = fopen('combined/quotes_assoc_array.php', 'w+');
fwrite($combined_file, $assoc);
fclose($combined_file);
// create JavaScript array
$combined_file = fopen('combined/quotes.js', 'w+');
fwrite($combined_file, $javaScript);
fclose($combined_file);

return true;
} catch(Exception $e) {
return $e->getMessage();
}
}
}
879 changes: 879 additions & 0 deletions combined/quotes.js

Large diffs are not rendered by default.

876 changes: 876 additions & 0 deletions combined/quotes.txt

Large diffs are not rendered by default.

Loading

0 comments on commit 9777801

Please sign in to comment.