Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various arrays [JS, PHP] #2

Merged
merged 1 commit into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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