Although my code doesn't support a lot of data types (only one), it's enough to see the general design, which concerns me the most.
Range interface:
<?php
namespace Range;
require_once 'vendor/autoload.php';
interface Range {
function __construct(string $range);
IntRange - implementation of range interface for integer
namespace Range;
require_once 'vendor\autoload.php';
class IntRange implements Range{
private $bounds,
$rangeType,
$range;
public function __construct(string $range) {
$this->range = $range;
$this->getType();
$this->getBounds();
}
private function boundsToInt(){
for($i = 0; $i < 2; $i++){
$this->bounds[$i] = intval($this->bounds[$i]);
}
}
private function getType(){
$rangeTypeExtractor = new RangeTypeExtractor($this->range);
$this->rangeType = $rangeTypeExtractor->rangeType();
}
private function getBounds(){
$boundExtractor = new BoundExtractor($this->range, $this->rangeType);
$this->bounds = $boundExtractor->extractBound();
}
private function getBoundForLooping() : array{
if($this->rangeType == 'inclusive'){
$start_bound = $this->bounds[0];
$end_bound = $this->bounds[1] +1;
}
else if($this->rangeType == 'exclusive'){
$start_bound = $this->bounds[0] + 1;
$end_bound = $this->bounds[1];
}
return [$start_bound, $end_bound];
}
public function toArray() : array {
$arr = array();
$bounds = $this->getBoundForLooping();
for($i = $bounds[0]; $i != $bounds[1]; $i++){
array_push($arr, $i);
}
return $arr;
}
public function toIterator() {
return new \ArrayIterator($this->toArray());
}
}
function toIterator();
function toArray();
}
IBoundExtractor
- extracts bounds from string
<?php
namespace Range;
require_once 'vendor/autoload.php';
interface IBoundExtractor {
function extractBound();
}
BoundExtractor
- generic class for extracting bounds, it returns the bound of string type, in form of array
<?php
namespace Range;
require_once 'vendor/autoload.php';
class BoundExtractor implements IBoundExtractor {
private $rangeType;
private $range;
public function __construct(string $range, string $rangeType) {
$this->rangeType = $rangeType;
$this->isBoundValid($range);
$this->range = $range;
}
public function extractBound() : array {
switch ( $this->rangeType){
case 'inclusive':
return $this->getInclusiveRange();
case 'exclusive':
return $this->getExclusiveRange();
}
}
private function isBoundValid(string $range){
if( substr_count($range, ".") > 3 || substr_count($range, ".") < 2){
throw new InvalidBoundException();
}
}
private function getInclusiveRange() : array{
return [
substr($this->range, 0, strpos($this->range, "...")), // until ...
substr($this->range, strpos($this->range, "...") + 3, strlen($this->range)) // after ...
];
}
private function getExclusiveRange() : array {
return [
substr($this->range, 0, strpos($this->range, "..")), // until ..
substr($this->range, strpos($this->range, "..") + 2, strlen($this->range)) // after ..
];
}
}
RangeTypeExtractor
- it's extracting the range type, inclusive or exclusive, not to be confused with the data type (e.g. int, string, date). I don't like the name; it's misleading. If somebody can give recommendations for a better name I will be very grateful. On the other hand, the name isn't that bad. If I need a class to determine the date type of the range, I can call it DateTypeExtractor
or something similar.
<?php
namespace Range;
require_once 'vendor/autoload.php';
class RangeTypeExtractor {
private $range;
public function __construct(string $range) {
$this->range = $range;
}
public function rangeType() : string {
if (strpos($this->range, "...") != FALSE) {
return "inclusive";
} else if (strpos($this->range, "..") != FALSE) {
return "exclusive";
} else {
throw new InvalidRangeException();
}
}
}
InvalidBound
and InvalidRange
exceptions
<?php
namespace Range;
require_once 'vendor/autoload.php';
class InvalidBoundException extends \Exception{
public function errorMessage() {
return "Invalid Bound exception. Check if the ../... is correct";
}
}
<?php
namespace Range;
require_once 'vendor/autoload.php';
class InvalidRangeException extends \Exception{
public function errorMessage() {
return "Invalid Range exception.";
}
}
RangeTypeExtractor
, you pastedBoundExtractor
twice, by mistake. \$\endgroup\$ – janos Jul 9 '17 at 18:20