From f002b3ee5ec4c8fb324231b9e134ac43f17c239e Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 8 Oct 2015 20:00:50 +0100 Subject: [PATCH] Adding base files --- composer.json | 11 +++ src/Item/ItemFactoryInterface.php | 17 +++++ src/Value/DateValue.php | 27 +++++++ src/Value/ItemValue.php | 119 ++++++++++++++++++++++++++++++ src/Value/ListValue.php | 98 ++++++++++++++++++++++++ src/Value/SetValue.php | 30 ++++++++ 6 files changed, 302 insertions(+) create mode 100644 composer.json create mode 100644 src/Item/ItemFactoryInterface.php create mode 100644 src/Value/DateValue.php create mode 100644 src/Value/ItemValue.php create mode 100644 src/Value/ListValue.php create mode 100644 src/Value/SetValue.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6bce071 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "name": "nathansalter/verona", + "autoload": { + "psr-4": { + "Verona\\": "src/" + } + }, + "require-dev": { + "phpunit/phpunit": "5.0" + } +} \ No newline at end of file diff --git a/src/Item/ItemFactoryInterface.php b/src/Item/ItemFactoryInterface.php new file mode 100644 index 0000000..5fbbf7e --- /dev/null +++ b/src/Item/ItemFactoryInterface.php @@ -0,0 +1,17 @@ +format('Y-m-d')); + } + +} \ No newline at end of file diff --git a/src/Value/ItemValue.php b/src/Value/ItemValue.php new file mode 100644 index 0000000..4b97439 --- /dev/null +++ b/src/Value/ItemValue.php @@ -0,0 +1,119 @@ +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]; + } + +} \ No newline at end of file diff --git a/src/Value/ListValue.php b/src/Value/ListValue.php new file mode 100644 index 0000000..5e5db46 --- /dev/null +++ b/src/Value/ListValue.php @@ -0,0 +1,98 @@ +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; + } + +} \ No newline at end of file diff --git a/src/Value/SetValue.php b/src/Value/SetValue.php new file mode 100644 index 0000000..20e5439 --- /dev/null +++ b/src/Value/SetValue.php @@ -0,0 +1,30 @@ +getAll())) { + parent::add($value); + } + } + return $this; + } + +} \ No newline at end of file