Skip to content

Instantly share code, notes, and snippets.

@kamil161g
Created June 10, 2019 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamil161g/078bd7a315bbd59eb240b79f367fadf5 to your computer and use it in GitHub Desktop.
Save kamil161g/078bd7a315bbd59eb240b79f367fadf5 to your computer and use it in GitHub Desktop.
<?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