Skip to content

Commit

Permalink
Exception related changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mekras committed Jul 14, 2016
1 parent 0cbad3b commit 3eb1bb2
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class Document extends AtomPubDocument
* @param Extensions $extensions Extension registry.
* @param \DOMDocument|null $document Source document.
*
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException If $document root node has invalid name.
*
* @since 1.0
*/
Expand Down
25 changes: 9 additions & 16 deletions src/EDM/Primitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Primitive extends Element
* @param \DOMElement|string $element DOM element or node name.
* @param string|null $type Primitive type if $element is a string.
*
* @since 1.0
* @throws \InvalidArgumentException If $element has invalid namespace.
*
* @throws \InvalidArgumentException
* @since 1.0
*/
public function __construct(Node $parent, $element, $type = null)
{
Expand All @@ -61,6 +61,7 @@ public function __construct(Node $parent, $element, $type = null)
$this->nodeName = (string) $element;
parent::__construct($parent);
if ($type) {
// Prefix "m" registered — no exception
$this->setAttribute('m:type', $type);
}
}
Expand Down Expand Up @@ -104,49 +105,40 @@ public function getName()
*
* @return string
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function getType()
{
return $this->getCachedProperty(
'type',
function () {
return $this->getAttribute('m:type');
}
);
// Prefix "m" registered — no exception
return $this->getAttribute('m:type');
}

/**
* Set type.
*
* @param string $type
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function setType($type)
{
// Prefix "m" registered — no exception
$this->setAttribute('m:type', $type);
$this->setCachedProperty('type', $type);
}

/**
* Return value
*
* @return mixed
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function getValue()
{
return $this->getCachedProperty(
'value',
function () {
// Prefix "m" registered — no exception
if ($this->getAttribute('m:null') === 'true') {
return null;
}
Expand Down Expand Up @@ -198,14 +190,15 @@ function () {
*
* @param mixed $value
*
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException If the $value type does not match type set via setType()
*
* @since 1.0
*/
public function setValue($value)
{
$element = $this->getDomElement();
if (null === $value) {
// Prefix "m" registered — no exception
$this->setAttribute('m:null', 'true');
$element->nodeValue = '';
}
Expand Down
23 changes: 7 additions & 16 deletions src/Element/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Entry extends BaseEntry implements \ArrayAccess
*
* @return string|null
*
* @throws \InvalidArgumentException
* @throws \Mekras\Atom\Exception\MalformedNodeException
*
* @since 1.0
Expand All @@ -54,8 +53,7 @@ function () {
*
* @param string $type
*
* @throws \InvalidArgumentException
* @throws \Mekras\Atom\Exception\MalformedNodeException
* @throws \Mekras\OData\Client\Exception\LogicException
*
* @since 1.0
*/
Expand All @@ -71,24 +69,26 @@ public function setEntityType($type)
}
}

$this->addCategory($type)->setScheme(OData::SCHEME);
try {
$this->addCategory($type)->setScheme(OData::SCHEME);
} catch (\InvalidArgumentException $e) {
throw new LogicException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* Return entity properties.
*
* @return Properties
*
* @throws \InvalidArgumentException
* @throws \Mekras\Atom\Exception\MalformedNodeException
*
* @since 1.0
*/
public function getProperties()
{
return $this->getCachedProperty(
'properties',
function () {
// No REQUIRED — no exception
$node = $this->query('atom:content/m:properties', self::SINGLE);
if (null === $node) {
return $this->getExtensions()
Expand Down Expand Up @@ -169,9 +169,6 @@ public function addRelation($resource, $type = null)
* @param string $offset An offset to check for.
*
* @return boolean true on success or false on failure.
*
* @throws \InvalidArgumentException
* @throws \Mekras\Atom\Exception\MalformedNodeException
*/
public function offsetExists($offset)
{
Expand All @@ -184,9 +181,6 @@ public function offsetExists($offset)
* @param string $offset The offset to retrieve.
*
* @return Primitive
*
* @throws \InvalidArgumentException
* @throws \Mekras\Atom\Exception\MalformedNodeException
*/
public function offsetGet($offset)
{
Expand All @@ -200,7 +194,6 @@ public function offsetGet($offset)
* @param mixed $value The value to set.
*
* @throws \InvalidArgumentException
* @throws \Mekras\Atom\Exception\MalformedNodeException
* @throws \Mekras\OData\Client\Exception\LogicException
*/
public function offsetSet($offset, $value)
Expand All @@ -223,8 +216,6 @@ public function offsetSet($offset, $value)
*
* @param string $offset The offset to unset.
*
* @throws \InvalidArgumentException
* @throws \Mekras\Atom\Exception\MalformedNodeException
* @throws \Mekras\OData\Client\Exception\LogicException
*
* @SuppressWarnings(PMD.UnusedFormalParameter)
Expand Down
6 changes: 4 additions & 2 deletions src/Element/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ class Properties extends Element implements \Iterator
*
* @since 1.0
*
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException If $element has invalid namespace.
*/
public function __construct(Content $parent, $element = null)
{
parent::__construct($parent, $element);

if (null === $element) {
// No prefix — no exception
$parent->setAttribute('type', 'application/xml');
}

/** @var \DOMNodeList $nodes */
// No REQUIRED — no exception
$nodes = $this->query('d:*');
foreach ($nodes as $node) {
$primitive = new Primitive($this, $node);
Expand All @@ -60,8 +62,8 @@ public function __construct(Content $parent, $element = null)
* @param string $type Primitive type (default is Primitive::STRING).
*
* @return Primitive
* @throws \InvalidArgumentException
*
* @throws \InvalidArgumentException
* @throws \Mekras\OData\Client\Exception\LogicException If property already exist.
*
* @since 1.0
Expand Down
12 changes: 5 additions & 7 deletions src/ODataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ class ODataExtension implements DocumentExtension, ElementExtension, NamespaceEx
*
* @return Document|null
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function parseDocument(Extensions $extensions, \DOMDocument $document)
{
if (OData::META === $document->documentElement->namespaceURI) {
switch ($document->documentElement->localName) {
case 'error':
// Node name already checked
return new ErrorDocument($extensions, $document);
}
}
Expand All @@ -57,8 +56,6 @@ public function parseDocument(Extensions $extensions, \DOMDocument $document)
*
* @return Document|null
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function createDocument(Extensions $extensions, $name)
Expand All @@ -74,21 +71,21 @@ public function createDocument(Extensions $extensions, $name)
*
* @return Element|null
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function parseElement(Node $parent, \DOMElement $element)
{
if (Atom::NS === $element->namespaceURI) {
switch ($element->localName) {
case 'entry':
// Node name already checked
return new Entry($parent, $element);
}
} elseif (OData::META === $element->namespaceURI) {
switch ($element->localName) {
case 'properties':
/** @var Content $parent */
// Node name already checked
return new Properties($parent, $element);
}
}
Expand All @@ -103,7 +100,8 @@ public function parseElement(Node $parent, \DOMElement $element)
* @param string $name Element name.
*
* @return Element|null
* @throws \InvalidArgumentException
*
* @throws \InvalidArgumentException If $element has invalid namespace.
*
* @since 1.0
*/
Expand Down
23 changes: 17 additions & 6 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Mekras\OData\Client\Document\ErrorDocument;
use Mekras\OData\Client\Element\Entry;
use Mekras\OData\Client\Exception\ClientErrorException;
use Mekras\OData\Client\Exception\LogicException;
use Mekras\OData\Client\Exception\RuntimeException;
use Mekras\OData\Client\Exception\ServerErrorException;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -87,7 +88,8 @@ public function __construct(
*
* @return Document
*
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException If given document is not supported.
* @throws \Mekras\Atom\Exception\RuntimeException In case of XML errors.
* @throws \Mekras\OData\Client\Exception\ClientErrorException
* @throws \Mekras\OData\Client\Exception\RuntimeException
* @throws \Mekras\OData\Client\Exception\ServerErrorException
Expand Down Expand Up @@ -140,18 +142,27 @@ public function sendRequest($method, $uri, Document $document = null)
*
* @return EntryDocument
*
* @throws \InvalidArgumentException
* @throws \Mekras\Atom\Exception\MalformedNodeException
* @throws \Mekras\OData\Client\Exception\LogicException
*
* @since 1.0
*/
public function createEntityDocument($type)
{
/** @var EntryDocument $document */
$document = $this->documentFactory->createDocument('atom:entry');
try {
$document = $this->documentFactory->createDocument('atom:entry');
} catch (\InvalidArgumentException $e) {
throw new LogicException('Can not create entry document', 0, $e);
}

if (!$document instanceof EntryDocument) {
throw new LogicException('Unexpected document type: ' . get_class($document));
}

/** @var Entry $entry */
$entry = $document->getEntry();
if (!$entry instanceof Entry) {
throw new LogicException('Unexpected entry type: ' . get_class($entry));
}

$entry->setEntityType($type);
$entry->addAuthor('');
$entry->addContent('');
Expand Down
4 changes: 2 additions & 2 deletions src/URI/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static function lte($arg1, $arg2)
/**
* Logical and.
*
* @param string ...$args
* @param array|string ...$args
*
* @return string
*/
Expand All @@ -109,7 +109,7 @@ public static function lAnd(...$args)
/**
* Logical or.
*
* @param string ...$args
* @param array|string ...$args
*
* @return string
*/
Expand Down
1 change: 1 addition & 0 deletions src/URI/KeyPredicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __toString()
{
if (is_array($this->keyValue)) {
$string = [];
/** @noinspection ForeachSourceInspection */
foreach ($this->keyValue as $key => $value) {
$string[] = $key . '=' . $value;
}
Expand Down

0 comments on commit 3eb1bb2

Please sign in to comment.