Skip to content

Instantly share code, notes, and snippets.

@littlefuntik
Last active November 18, 2016 07:44
Show Gist options
  • Save littlefuntik/706ea49a21d2383c87fc7e0c6aebbeb5 to your computer and use it in GitHub Desktop.
Save littlefuntik/706ea49a21d2383c87fc7e0c6aebbeb5 to your computer and use it in GitHub Desktop.
<?php
class PmlMatchType implements \JsonSerializable
{
const EXACT = 0b001;
const BROAD = 0b010;
const EXACT_COMBO = 0b100;
const EXACT_TEXT = 'Exact';
const BROAD_TEXT = 'Broad';
const EXACT_COMBO_TEXT = 'Exact Combo';
/** @var int */
private $value;
/**
* PmlMatchType constructor.
*/
public function __construct()
{
$this->value = 0;
}
/**
* @return array
*/
public function toArray()
{
$result = [];
if ($this->isBroadEnabled()) {
$result[] = static::BROAD_TEXT;
}
if ($this->isExactEnabled()) {
$result[] = static::EXACT_TEXT;
}
if ($this->isExactComboEnabled()) {
$result[] = static::EXACT_COMBO_TEXT;
}
return $result;
}
/**
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}
/**
* @param bool $enabled
*/
public function broadEnabled($enabled)
{
if (true === (bool) $enabled) {
$this->value |= static::BROAD; // enable
} else {
$this->value &= ~static::BROAD; // disable
}
}
/**
* @return bool
*/
public function isBroadEnabled()
{
return (bool) ($this->value & static::BROAD);
}
/**
* @param bool $enabled
*/
public function exactEnabled($enabled)
{
if (true === (bool) $enabled) {
$this->value |= static::EXACT; // enable
} else {
$this->value &= ~static::EXACT; // disable
}
}
/**
* @return bool
*/
public function isExactEnabled()
{
return (bool) ($this->value & static::EXACT);
}
/**
* @param bool $enabled
*/
public function exactComboEnabled($enabled)
{
if (true === (bool) $enabled) {
$this->value |= static::EXACT_COMBO; // enable
} else {
$this->value &= ~static::EXACT_COMBO; // disable
}
}
/**
* @return bool
*/
public function isExactComboEnabled()
{
return (bool) ($this->value & static::EXACT_COMBO);
}
}
$matchType = new PmlMatchType();
$matchType->broadEnabled(true);
$matchType->exactEnabled(true);
$matchType->exactComboEnabled(true);
$matchType->exactEnabled(false);
print_r($matchType->toArray());
/*
Array
(
[0] => Broad
[1] => Exact Combo
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment