-
-
Save kamil161g/078bd7a315bbd59eb240b79f367fadf5 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class Product implements ArrayAccess | |
{ | |
private $number; | |
/** | |
* Product constructor. | |
* @param int $number | |
*/ | |
public function __construct(int $number) | |
{ | |
$this->number = str_split(decbin($number)); | |
} | |
/** | |
* @param mixed $offset | |
* @return bool | |
*/ | |
public function offsetExists($offset) | |
{ | |
return isset($this->number[$offset]); | |
} | |
/** | |
* @param mixed $offset | |
* @return mixed|null | |
*/ | |
public function offsetGet($offset) | |
{ | |
return isset($this->number[$offset]) ? $this->number[$offset] : null; | |
} | |
/** | |
* @param mixed $offset | |
* @param mixed $value | |
* @throws Exception | |
*/ | |
public function offsetSet($offset, $value) | |
{ | |
if ($value === 0 || $value === 1) { | |
if (is_null($offset)) { | |
$this->number[] = $value; | |
} else { | |
$this->number[$offset] = $value; | |
} | |
}else{ | |
throw new Exception('Podaj 0 lub 1'); | |
} | |
} | |
/** | |
* @param mixed $offset | |
*/ | |
public function offsetUnset($offset) | |
{ | |
unset($this->number[$offset]); | |
} | |
/** | |
* @return int | |
*/ | |
public function getInt() : int | |
{ | |
return implode("", $this->number); | |
} | |
} | |
$number = new Product(15); | |
$number->offsetSet(1, 1); | |
$number->offsetGet(2); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment