Skip to content

Instantly share code, notes, and snippets.

@rmccullagh
Last active September 6, 2019 20:58
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 rmccullagh/da48753127b8628318e291628fe14be8 to your computer and use it in GitHub Desktop.
Save rmccullagh/da48753127b8628318e291628fe14be8 to your computer and use it in GitHub Desktop.
<?php
namespace App\Models;
class PhpVersionCollection implements \Countable, \ArrayAccess, \JsonSerializable, \IteratorAggregate
{
/** @var array */
private $versionNumbers = ['5.6', '7.0', '7.1', '7.2', '7.3'];
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return $this->exists($value);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Please choose a valid PHP version';
}
/**
* Check if an arbitrary string is a valid PHP version number
*
* @param string $version
*
* @return bool
*/
public function exists($version)
{
return $this[$version] ? true : false;
}
/**
* jsonSerialize
*
* @return array
*/
public function jsonSerialize()
{
return [
'versions' => $this->versionNumbers
];
}
public function offsetSet($k, $v)
{
throw new \RuntimeException('Cannot set a value at index because this is a read only collection');
}
public function offsetExists($key)
{
return in_array($key, $this->versionNumbers);
}
public function offsetUnset($key)
{
throw new \RuntimeException('Cannot unset an index because this is a read only collection');
}
public function offsetGet($key)
{
return $this[$key] ?? null;
}
public function count()
{
return \count($this->versionNumbers);
}
/**
* Undocumented function
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->versionNumbers);
}
}
$crasher = new PhpVersionCollection();
var_dump($crasher->exists('7.2'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment