@ 2015-12-16T19:41:39Z <?php
/**
* A simple user account database
*/
class UserAccountDatabase
{
private $data = [];
/**
* Save a user account
*
* @param string $username
* @param string $password
* @return void
* @throws Exception
*/
public function saveAccount($username, $password)
{
if ($this->getAccount($username) !== null) {
throw new \Exception('Username already exists in database.');
}
$this->data[$username] = $password;
}
/**
* Get a user account by username
*
* @param string $username
* @return array|null
*/
public function getAccount($username)
{
return isset($this->data[$username]) ? $this->data[$username] : null;
}
/**
* Delete a user account by username
*
* @param string $username
* @return void
*/
public function deleteAccount($username)
{
unset($this->data[$username]);
}
}
/**
* You must implement this interface
*/
interface UserAccountManagerInterface
{
/**
* @param UserAccountDatabase $db
*/
public function __construct(UserAccountDatabase $db);
/**
* Allow a user to create an account. Should return true if account creation
* was successful and false otherwise.
*
* @param string $username
* @param string $password
* @return bool
*/
public function create($username, $password);
/**
* Allow a user to authenticate using the same username and password that
* they used when creating their account. Should return true if the account
* exists and the password matches or false otherwise.
*
* @param string $username
* @param string $password
* @return bool
*/
public function authenticate($username, $password);
}
class UserAccountManager implements UserAccountManagerInterface
{
private $db;
public function __construct(UserAccountDatabase $db)
{
$this->db = $db;
}
public function create($username, $password)
{
try {
$this->db->saveAccount($username, $password);
return true;
} catch (Exception $e) {
return false;
}
}
public function authenticate($username, $password)
{
$stored_pass = $this->db->getAccount($username);
if ($stored_pass === null) {
return false;
}
return $password === $stored_pass;
}
}
///////////////////////////
$input = fopen("php://memory", "rw");
$data = '[
[
["abe","12345"],
["ben","67890"],
["ben","67890"]
],
[
["abe","12345"],
["ben","67890"],
["ben","67890"],
["carla","12345"]
]
]';
fwrite($input, $data);
fseek($input, 0);
// Setup Streams
//$input = fopen("php://stdin", "r");
$output = fopen("php://stdout","w");
// Input (JSON)
$arguments = json_decode(trim(stream_get_contents($input)), true, 16);
//var_dump($arguments); die();
$db = new UserAccountDatabase();
$manager = new UserAccountManager($db);
$result = [];
// Create Accounts
foreach ($arguments[0] as $account) {
$result[0][] = call_user_func_array([$manager, 'create'], $account);
}
// Authenticate Accounts
foreach ($arguments[1] as $account) {
$result[1][] = call_user_func_array([$manager, 'authenticate'], $account);
}
// Output (JSON)
fwrite($output, json_encode($result));
fclose($output);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.5.0 - 5.5.37 , 5.6.0 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.0 - 7.2.33 , 7.3.16 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 [[true,true,false],[true,true,true,false]] Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
[[true,true,false],[true,true,true,false]] preferences:dark mode live preview
205.93 ms | 402 KiB | 221 Q