-
-
Save rmccullagh/da48753127b8628318e291628fe14be8 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 | |
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