Skip to content

Instantly share code, notes, and snippets.

@ad3n
Created April 10, 2016 03:57
Show Gist options
  • Save ad3n/e325ffa730bdb07c6b570394c1ccfd71 to your computer and use it in GitHub Desktop.
Save ad3n/e325ffa730bdb07c6b570394c1ccfd71 to your computer and use it in GitHub Desktop.
[PHPID QUIZ] Jajar Genjang OOP
<?php
class JajarGenjang
{
private $panjang = 18;
private $increment = 1;
private function createLine()
{
$limit = $this->panjang + $this->increment;
for ($i = $this->increment; $i <= $limit; $i++) {
echo '|';
}
echo PHP_EOL;
}
private function createEmptyLine()
{
$limit = $this->panjang + $this->increment;
for ($i = $this->increment; $i <= $limit; $i++) {
if ($this->increment === $i || $limit === $i) {
echo '|';
} else {
echo ' ';
}
}
echo PHP_EOL;
$this->increment++;
}
private function createSpace()
{
for ($i = 1; $i <= $this->increment; $i++) {
echo ' ';
}
}
private function printILovePhp()
{
$string = 'ILOVEPHP';
$printed = false;
$length = strlen($string);
$limit = $this->panjang + $this->increment;
for ($i = $this->increment; $i <= $limit; $i++) {
if ($this->increment === $i || $limit === $i) {
echo '|';
} elseif (!$printed && $i === (int) round(($limit - ($length - 4)) / 2)) {
echo $string;
$printed = true;
$i += $length - 1;
} else {
echo ' ';
}
}
echo PHP_EOL;
$this->increment++;
}
public function draw()
{
$this->createLine();
$this->createSpace();
$this->createEmptyLine();
$this->createSpace();
$this->printILovePhp();
$this->createSpace();
$this->printILovePhp();
$this->createSpace();
$this->printILovePhp();
$this->createSpace();
$this->printILovePhp();
$this->createSpace();
$this->createEmptyLine();
$this->createSpace();
$this->createLine();
}
}
$jajarGenjang = new JajarGenjang();
$jajarGenjang->draw();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment