Skip to content
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ find_binary
```bash
php find_binary.php <integer>
```
Decompose an integer into powers of two
Decompose an integer into powers of two

compare_hashmaps
----------------

```bash
php compare_yaml_hashmaps.php file1 file2
```
Compare 2 YAML hashmap files (such as symfony parameters.yml files)
2 changes: 1 addition & 1 deletion Util/TextColorWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function textColor($string, $colorID)
*
* @return array
*/
private function getKnownColors()
private static function getKnownColors()
{
$colors = array(
static::BASH_PROMPT_BLACK,
Expand Down
88 changes: 88 additions & 0 deletions compare_yaml_hashmaps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\Yaml\Yaml;

/**
* Validate console input
*
* Die if input is not valid
*
* @param array $argv user inputs
*/
function validateInput($argv)
{
if (count($argv) !== 4) {
echo 'php compare_hashmaps <file1> <file2> <root_node>' . PHP_EOL;
die(1);
}

if (!file_exists($argv[1])) {
echo "Error: " . $argv[1] . " is not a valid file" . PHP_EOL;
die(1);
}
if (!file_exists($argv[2])) {
echo "Error: " . $argv[2] . " is not a valid file" . PHP_EOL;
die(1);
}
}

/**
* @param string $filepath
* @param string $rootNode
*
* @return array
* @throws Exception
*/
function extractArrayFromFile($filepath, $rootNode)
{
$value = Yaml::parse(file_get_contents($filepath));

if (!array_key_exists($rootNode, $value)) {
$showKeys = implode(', ', array_keys($value));
throw new \RuntimeException("Expects root node '$rootNode'' in file $filepath, got: $showKeys");
}

return $value[$rootNode];
}

validateInput($argv);

require 'Util/TextColorWriter.php';

$filepath1 = $argv[1];
$filepath2 = $argv[2];
$rootNode = $argv[3];

$list1 = extractArrayFromFile(realpath($filepath1), $rootNode);
$list2 = extractArrayFromFile(realpath($filepath2), $rootNode);

$inFile1ButNotInFile2 = [];
$inFile2ButNotInFile1 = [];

foreach ($list1 as $key => $value) {
if (!array_key_exists($key, $list2)) {
$inFile1ButNotInFile2[$key] = $value;
}
}
foreach ($list2 as $key => $value) {
if (!array_key_exists($key, $list1)) {
$inFile2ButNotInFile1[$key] = $value;
}
}


// echo result
echo TextColorWriter::textColor('Done', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
echo TextColorWriter::textColor('Found in file 1 but not in file 2:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;

foreach ($inFile1ButNotInFile2 as $key => $value) {
echo ' - ' . $key . PHP_EOL;
}

echo TextColorWriter::textColor('Found in file 2 but not in file 1:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;

foreach ($inFile2ButNotInFile1 as $key => $value) {
echo ' - ' . $key . PHP_EOL;
}
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "matks/php_scripts",
"description": "Useful php scripts",
"type": "library",
"require": {
"symfony/yaml": "^3.2"
},
"license": "MIT"
}
74 changes: 74 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.