Skip to content

Instantly share code, notes, and snippets.

@miholeus
Created February 7, 2018 15:25
Show Gist options
  • Save miholeus/6624fa41ac260163b3b7fdbae3019d28 to your computer and use it in GitHub Desktop.
Save miholeus/6624fa41ac260163b3b7fdbae3019d28 to your computer and use it in GitHub Desktop.
quick quiz
<?php
1.
$x = true;
if ($x == 1 && $x==2 && $x==3){
echo("ok");
}
2.
class Helm
{
const DIRECTION_LEFT = 'left';
const DIRECTION_RIGHT = 'right';
const DIRECTION_CENTER = 'center';
private $direction;
private $angle = 0;
/**
* Checks possible angle values
*
* @param int $angle in Celcius
* @return bool
*/
private function checkAngle(int $angle): bool
{
if ($angle > 100 || $angle < 0) {
throw new \InvalidArgumentException("Invalid angle value");
}
return true;
}
/**
* Turn direction to left
*
* @param int $angle in Celcius
* @return void
*/
public function turnLeft(int $angle):void
{
$this->checkAngle($angle);
$this->direction = self::DIRECTION_LEFT;
$this->angle = $angle;
}
/**
* Turn direction to right
*
* @param int $angle in Celcius
* @return void
*/
public function turnRight(int $angle):void
{
$this->checkAngle($angle);
$this->direction = self::DIRECTION_RIGHT;
$this->angle = $angle;
}
/**
* Set direction as center
* Angle is 0
*
* @return void
*/
public function center():void
{
$this->direction = self::DIRECTION_CENTER;
$this->angle = 0;
}
public function getDirection():string
{
return $this->direction;
}
public function getAngle():int
{
return $this->angle;
}
}
class Bicycle
{
private $wheels = [];
private Helm $helm;
private $speed = 0;
public function __construct()
{
$this->helm = new Helm();
}
/**
* Turn bicycle to left
*
* @param integer $angle in Celcius
* @return void
*/
public function turnLeft($angle = 90):void
{
$this->helm->turnLeft($angle);
}
/**
* Turn bicycle to right
*
* @param integer $angle in Celcius
* @return void
*/
public function turnRight($angle = 90):void
{
$this->helm->turnRight($angle);
}
/**
* Ride bicycle by center
*
* @return void
*/
public function center():void
{
$this->helm->center();
}
/**
* Increase current speed
*
* @param int $speed km\h
* @return void
*/
public function increaseSpeed(int $speed):void
{
$this->speed += $speed;
if ($this->speed >= 60) {
throw new \InvalidArgumentException("You can't ride that fast");
}
}
/**
* Decrease current speed
*
* @param int $speed km\h
* @return void
*/
public function decreaseSpeed(int $speed):void
{
$this->speed -= $speed;
if ($this->speed <= 0) {
throw new \InvalidArgumentException("Speed can't be below 0");
}
}
/**
* Current speed
*
* @return int
*/
public function getCurrentSpeed():int
{
return $this->speed;
}
}
3.
interface FileFinder
{
public function getFiles();
}
class FinderByFilter implements FileFinder
{
/**
* Current folder path
* @var string
*/
private $path;
public function __construct(string $path)
{
$this->path = $path;
}
protected function getFilter(\RecursiveIteratorIterator $iterator): \IteratorIterator
{
return $iterator;
}
/**
* Get all files in folder
* @return generator object
*/
public function getFiles():\Iterator
{
$dir = new \RecursiveDirectoryIterator($this->path);
$iterator = new \RecursiveIteratorIterator($dir);
$iterator = $this->getFilter($iterator);
foreach ($iterator as $file) {
yield $file;
}
}
}
/**
* Find jpeg files in folder
*/
class JpegFinder extends FinderByFilter
{
/**
* Regex expression
*
* @var string
*/
protected $mask;
public function __construct(string $path)
{
parent::__construct($path);
$this->mask = '/^.+\.(?:jpg|jpeg)/i';// any jpeg files
}
protected function getFilter(\RecursiveIteratorIterator $iterator): \IteratorIterator
{
$iterator = new \RegexIterator($iterator, $this->getMask(), \RecursiveRegexIterator::GET_MATCH);
return $iterator;
}
/** Set mask for seaching */
public function applyMask(string $mask)
{
$this->mask = $mask;
}
public function getMask(): string
{
return $this->mask;
}
}
class PngConverter
{
protected $format = 'png';
/**
* Convert image to png
*
* @param string $path [description]
* @return string
*/
public function convert(string $path): string
{
if (!file_exists($path)) {
throw new \InvalidArgumentException("File does not exist");
}
$info = pathinfo($path);
$newFile = sprintf("%s/%s.%s", $info['dirname'], $info['filename'], $this->format);
// also it is possible to use command line for conversion
$image = new \Imagick($path);
$image->setImageFormat($this->format);
$image->writeImage($newFile);
return $newFile;
}
}
$finder = new JpegFinder(".");
// $finder->applyMask('/some\.jpg/i');
$converter = new PngConverter();
foreach($finder->getFiles() as $file) {
$converter->convert(array_pop($file));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment