Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Александр Бутылкин
php_result
Merge requests
!1
An error occurred while fetching the assigned milestone of the selected merge_request.
Revert "Upload New File"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Александр Бутылкин
requested to merge
revert-86166f3d
into
master
2 years ago
Overview
0
Commits
1
Pipelines
0
Changes
1
This reverts commit
86166f3d
0
0
Compare
master
master (HEAD)
and
latest version
latest version
8577e2ff
1 commit,
2 years ago
1 file
+
0
−
96
Expand all files
Preferences
Preferences
File browser
List view
Tree view
Compare changes
Inline
Side-by-side
Show whitespace changes
Show one file at a time
solution.php deleted
100644 → 0
+
0
−
96
Options
View file @ 86166f3d
<?php
// This code works in PHP.
//
// Run builtin PHP web server like so:
//
// php -S localhost:8080 ./main.php
//
// Then visit this page in your browser:
//
// http://localhost:8080/?accounts[]=Rulon_User&accounts[]=Bidon_Admin
//
// The output should be:
//
// name=Rulon
// name=Bidon
//
// TODO: Make this code compile and run with KPHP with minimal
// changes while trying to keep the code as idiomatic as possible.
class
AccountFactory
{
public
static
function
newAccount
(
string
$name
,
string
$account_type
)
{
if
(
$account_type
==
'Admin'
)
{
return
new
Admin
(
$name
);
}
else
if
(
$account_type
==
'User'
)
{
return
new
User
(
$name
);
}
return
null
;
}
}
class
Admin
{
public
string
$name
;
public
bool
$is_valid
;
public
function
__construct
(
$name
)
{
$this
->
name
=
$name
;
}
}
class
User
extends
Admin
{
public
function
__construct
(
$name
)
{
$this
->
name
=
$name
;
}
}
/** @param Admin[] $accounts */
function
validate_accounts
(
$accounts
)
{
foreach
(
$accounts
as
$a
)
{
$a
->
is_valid
=
!
empty
(
$a
->
name
);
}
}
/** @param Admin[] $accounts */
function
collect_names
(
$accounts
)
{
return
array_map
(
function
(
$a
)
{
return
$a
->
name
;
},
$accounts
);
}
/** @param string[] $names */
function
print_names
(
$names
)
{
foreach
(
$names
as
$name
)
{
echo
"name=
$name
<br>"
;
}
}
/**
* @kphp-required
*/
function
callback
(
Admin
$a
)
{
return
$a
->
is_valid
;
}
function
main
()
{
$raw_accounts
=
$_GET
[
'accounts'
];
if
(
!
$raw_accounts
)
{
die
(
'$_GET["accounts"] is empty'
);
}
$accounts
=
[];
foreach
(
$raw_accounts
as
$raw
)
{
$parts
=
explode
(
'_'
,
$raw
);
if
(
count
(
$parts
)
!==
2
)
{
continue
;
}
$accounts
[]
=
AccountFactory
::
newAccount
(
$parts
[
0
],
$parts
[
1
]);
}
validate_accounts
(
$accounts
);
$validated_accounts
=
array_filter
(
$accounts
,
"callback"
);
$names
=
collect_names
(
$validated_accounts
);
print_names
(
$names
);
}
main
();
?>
\ No newline at end of file
Menu
Explore
Projects
Groups
Topics
Snippets