Skip to content

Commit

Permalink
Adding base files
Browse files Browse the repository at this point in the history
  • Loading branch information
nathansalter committed Oct 8, 2015
1 parent 8a43faa commit f002b3e
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 0 deletions.
11 changes: 11 additions & 0 deletions composer.json
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"
}
}
17 changes: 17 additions & 0 deletions src/Item/ItemFactoryInterface.php
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;

}
27 changes: 27 additions & 0 deletions src/Value/DateValue.php
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'));
}

}
119 changes: 119 additions & 0 deletions src/Value/ItemValue.php
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];
}

}
98 changes: 98 additions & 0 deletions src/Value/ListValue.php
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;
}

}
30 changes: 30 additions & 0 deletions src/Value/SetValue.php
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;
}

}

0 comments on commit f002b3e

Please sign in to comment.