Last active
September 16, 2023 16:40
-
-
Save fatihgune/59384169bf4c8f06df91de9331882a0f to your computer and use it in GitHub Desktop.
Write data into file in php array format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('write_contents_as_php_array_into_file')) { | |
/** | |
* Writes the contents of given string/object/array to the file in php array format. | |
* | |
* @param mixed $contents | |
* @param bool $die | |
* @return string | |
*/ | |
function write_contents_as_php_array_into_file($contents, $die = false) | |
{ | |
$filePath = base_path() . '/tests/fixtures/test.php'; | |
$folderPath = base_path() . '/tests/fixtures'; | |
$startWith = '<?php $array = '; | |
if (!file_exists($filePath)) { | |
mkdir($folderPath); | |
} | |
if (file_exists($filePath)) { | |
file_put_contents($filePath, ''); | |
} | |
if (getType($contents) === 'string' || getType($contents) === 'object') { | |
$contents = str_replace('{', '[', $contents); | |
$contents = str_replace('}', ']', $contents); | |
$contents = str_replace(':', '=>', $contents); | |
$contents = str_replace("'", '', $contents); | |
file_put_contents($filePath, var_export($startWith. $contents . ';', true)); | |
} | |
if (getType($contents) === 'array') { | |
$contents = var_export($contents, true); | |
$contents = str_replace('array', '', $contents); | |
$contents = str_replace('(', '[', $contents); | |
$contents = str_replace(')', ']', $contents); | |
for ($i = 0; $i < 10; $i++) { | |
$contents = str_replace($i .' =>', '', $contents); | |
} | |
for ($i = 0; $i <= 10; $i++) { | |
$contents = str_replace($i, '', $contents); | |
} | |
file_put_contents($filePath, $startWith . $contents . ';'); | |
} | |
if ($die) { | |
die(var_dump('Array written to the file')); | |
} | |
} | |
} | |
// Pass the variable as the first parameter and it will create a file (test.php) under /tests/fixtures folder | |
// and write the contents of the variable as a PHP array. Supports arrays, objects (collections, json) and string | |
// (json_encode). Second dependency will make the code stop at that line and dump a message. | |
// I got this together in a Laravel 6.x project but it should be good enough in a vanilla PHP environnent. | |
// The basic idea is, dumping a response of an external source (think Guzzle response of an API), to a file, | |
// making the changes and using it in your mock tests. I am sure there are better ways to do this but, I think that | |
// the ability of accessing contents of a variable and interacting with it manually can be very convenient. | |
// Example I/O // | |
// Array Input | |
$exampleArray = ['foo' => ['bar' => ['baz']]]; | |
write_contents_as_php_array_into_file($exampleArray, true); | |
// Output | |
<?php $array = [ | |
'foo' => | |
[ | |
'bar' => | |
[ | |
'baz', | |
], | |
], | |
]; | |
// String Input (Json, using json_encode) | |
$exampleArray = ['foo' => ['bar' => ['baz']]]; | |
$exampleJson = json_encode($exampleArray, 128); | |
write_contents_as_php_array_into_file($exampleJson, true); | |
// Output | |
'<?php $array = [ | |
"foo"=> [ | |
"bar"=> [ | |
"baz" | |
] | |
] | |
];' | |
// Object Input (Object, using collections) | |
$exampleArray = ['foo' => ['bar' => ['baz']]]; | |
$exampleObject = collect($exampleArray); | |
write_contents_as_php_array_into_file($exampleObject, true); | |
// Output | |
'<?php $array = ["foo"=>["bar"=>["baz"]]];' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment