-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a43faa
commit f002b3e
Showing
6 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "nathansalter/verona", | ||
"autoload": { | ||
"psr-4": { | ||
"Verona\\": "src/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "5.0" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Verona\Item; | ||
|
||
use Verona\Value\ItemValue; | ||
|
||
interface ItemFactoryInterface | ||
{ | ||
|
||
/** | ||
* | ||
* @param string $name | ||
* @return ItemValue | ||
*/ | ||
public function getItem(string $name) : ItemValue; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Verona\Value; | ||
|
||
class DateValue extends \DateTime { | ||
|
||
/** | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function __construct($value) | ||
{ | ||
parent::__construct($value); | ||
$this-setTime(0, 0, 0); | ||
} | ||
|
||
/** | ||
* | ||
* @param \DateTime $dateTime | ||
* @return DateValue | ||
*/ | ||
public static function createFromDateTime(\DateTime $dateTime) : self | ||
{ | ||
return new self($dateTime->format('Y-m-d')); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
namespace Verona\Value; | ||
|
||
class ItemValue | ||
{ | ||
|
||
/** | ||
* | ||
* @var array | ||
*/ | ||
protected $definitions; | ||
|
||
/** | ||
* | ||
* @var array | ||
*/ | ||
protected $attributes; | ||
|
||
public function __construct() | ||
{ | ||
$this->definitions = []; | ||
$this->attributes = []; | ||
} | ||
|
||
/** | ||
* Should be in the format name => type | ||
* Allowable types are either the main scalar/complex types | ||
* or any fully qualified class name | ||
* | ||
* @param array $definition | ||
* @return $this | ||
*/ | ||
public function defineItem(array $definitions) : self | ||
{ | ||
$this->definitions = $definitions; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function hasDefinition(string $key) : bool | ||
{ | ||
return array_key_exists($key, $this->definitions); | ||
} | ||
|
||
/** | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @throws \InvalidArgumentException | ||
* @return bool | ||
*/ | ||
public function isDefinitionType(string $key, $value) : bool | ||
{ | ||
if(! $this->hasDefinition($key)) { | ||
throw new \InvalidArgumentException(sprintf('%s(%s,...) is not a value in this | ||
item!', __METHOD__, $key)); | ||
} | ||
if(gettype($value) != 'object') { | ||
return $this->definitions[$key] == gettype($value); | ||
} | ||
return $this->definitions[$key] == get_class($value); | ||
} | ||
|
||
/** | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return $this | ||
*/ | ||
public function set(string $key, $value) : self | ||
{ | ||
if(! count($this->definitions)) { | ||
throw new \RuntimeException(sprintf('%s() must not be called before | ||
the item is defined', __METHOD__)); | ||
} | ||
|
||
if(! $this->isDefinitionType($key, $value)) { | ||
throw new \InvalidArgumentException(sprintf('%s() was provided the | ||
wrong type', __METHOD__)); | ||
} | ||
|
||
$this->attributes[$key] = $value; | ||
return $this; | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function has(string $key) : bool | ||
{ | ||
return in_array($key, $this->attributes); | ||
} | ||
|
||
/** | ||
* | ||
* @param string $key | ||
* @throws \RuntimeException | ||
* @return mixed | ||
*/ | ||
public function get(string $key) | ||
{ | ||
if(! $this->has($key)) { | ||
throw new \RuntimeException(sprintf('%s(%s) Check to make sure that this | ||
key is set', __METHOD__, $key)); | ||
} | ||
|
||
return $this->attributes[$key]; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Verona\Value; | ||
|
||
/** | ||
* Simple value to allow more explicit handling of arrays | ||
* | ||
* @author nathan | ||
* | ||
*/ | ||
class ListValue implements \IteratorAggregate, \Countable | ||
{ | ||
/** | ||
* | ||
* @var array | ||
*/ | ||
protected $list; | ||
|
||
/** | ||
* | ||
* @param mixed ...$values | ||
*/ | ||
public function __construct(...$values) { | ||
$this->list = []; | ||
if(count($values) > 0) { | ||
$this->add(...$values); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param mixed ...$values | ||
* @return $this | ||
*/ | ||
public function add(...$values) : self | ||
{ | ||
foreach($values as $val) { | ||
$this->list[] = $val; | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getAll() : array | ||
{ | ||
return array_values($this->list); | ||
} | ||
|
||
/** | ||
* | ||
* {@inheritDoc} | ||
* @see IteratorAggregate::getIterator() | ||
*/ | ||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->getAll()); | ||
} | ||
|
||
/** | ||
* | ||
* {@inheritDoc} | ||
* @see Countable::count() | ||
*/ | ||
public function count() | ||
{ | ||
return count($this->list); | ||
} | ||
|
||
/** | ||
* Returns true if this value contains the supplied value | ||
* | ||
* @param mixed $needle | ||
* @return bool | ||
*/ | ||
public function contains($needle) : bool | ||
{ | ||
return in_array($value, $this->getAll()); | ||
} | ||
|
||
/** | ||
* Sort the list | ||
* | ||
* @param callable|null $callable | ||
* @return $this | ||
*/ | ||
public function sort(callable $callable = null) : self | ||
{ | ||
if(! $callable) { | ||
sort($this->list); | ||
} else { | ||
usort($this->list); | ||
} | ||
return $this; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Verona\Value; | ||
|
||
/** | ||
* This is a very similar value to the list value but can only have one of each value | ||
* in it at a time | ||
* | ||
* @author Nathan Salter | ||
* | ||
*/ | ||
class SetValue extends ListValue | ||
{ | ||
|
||
/** | ||
* | ||
* {@inheritDoc} | ||
* @see \Verona\Value\ListValue::add() | ||
*/ | ||
public function add(...$values) : self | ||
{ | ||
foreach($values as $value) { | ||
if(! in_array($value, $this->getAll())) { | ||
parent::add($value); | ||
} | ||
} | ||
return $this; | ||
} | ||
|
||
} |