Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasweidner committed Aug 26, 2011
Show file tree
Hide file tree
Showing 14 changed files with 460 additions and 31 deletions.
7 changes: 7 additions & 0 deletions src/Dispatchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Zend\Stdlib;

interface Dispatchable
{
public function dispatch(RequestDescription $request, ResponseDescription $response = null);
}
33 changes: 2 additions & 31 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,7 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Stdlib;
namespace Zend\Stdlib;

/**
* Marker interface for exceptions
*
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface Exception
interface Exception
{
}
9 changes: 9 additions & 0 deletions src/Exception/DomainException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Zend\Stdlib\Exception;

use Zend\Stdlib\Exception;

class DomainException extends \DomainException implements Exception
{
}
11 changes: 11 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Zend\Stdlib\Exception;

use Zend\Stdlib\Exception;

class InvalidArgumentException
extends \InvalidArgumentException
implements Exception
{
}
110 changes: 110 additions & 0 deletions src/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Zend\Stdlib;

use Traversable;

class Message implements MessageDescription
{
/**
* @var array
*/
protected $metadata = array();

/**
* @var string
*/
protected $content = '';

/**
* Set message metadata
*
* Non-destructive setting of message metadata; always adds to the metadata, never overwrites
* the entire metadata container.
*
* @param string|int|array|Traversable $spec
* @param mixed $value
* @return Message
*/
public function setMetadata($spec, $value = null)
{
if (is_scalar($spec)) {
$this->metadata[$spec] = $value;
return $this;
}
if (!is_array($spec) && !$spec instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf(
'Expected a string, array, or Traversable argument in first position; received "%s"',
(is_object($spec) ? get_class($spec) : gettype($spec))
));
}
foreach ($spec as $key => $value) {
$this->metadata[$key] = $value;
}
return $this;
}

/**
* Retrieve all metadata or a single metadatum as specified by key
*
* @param null|string|int $key
* @param null|mixed $default
* @return mixed
*/
public function getMetadata($key = null, $default = null)
{
if (null === $key) {
return $this->metadata;
}

if (!is_scalar($key)) {
throw new Exception\InvalidArgumentException('Non-scalar argument provided for key');
}

if (array_key_exists($key, $this->metadata)) {
return $this->metadata[$key];
}

return $default;
}

/**
* Set message content
*
* @param mixed $value
* @return Message
*/
public function setContent($value)
{
$this->content = $value;
return $this;
}

/**
* Get message content
*
* @return mixed
*/
public function getContent()
{
return $this->content;
}

/**
* @return string
*/
public function toString()
{
$request = '';
foreach ($this->getMetadata() as $key => $value) {
$request .= sprintf(
"%s: %s\r\n",
(string) $key,
(string) $value
);
}
$request .= "\r\n" . $this->getContent();
return $request;
}

}
13 changes: 13 additions & 0 deletions src/MessageDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Zend\Stdlib;

interface MessageDescription
{
public function setMetadata($spec, $value = null);
public function getMetadata($key = null);

public function setContent($content);
public function getContent();

}
109 changes: 109 additions & 0 deletions src/Parameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Zend\Stdlib;

use ArrayObject;

class Parameters extends ArrayObject implements ParametersDescription
{
/**
* Constructor
*
* Enforces that we have an array, and enforces parameter access to array
* elements.
*
* @param array $values
* @return void
*/
public function __construct(array $values = null)
{
if (null === $values) {
$values = array();
}
parent::__construct($values, ArrayObject::ARRAY_AS_PROPS);
}

/**
* Populate from native PHP array
*
* @param array $values
* @return void
*/
public function fromArray(array $values)
{
$this->exchangeArray($values);
}

/**
* Populate from query string
*
* @param string $string
* @return void
*/
public function fromString($string)
{
$array = array();
parse_str($string, $array);
$this->fromArray($array);
}

/**
* Serialize to native PHP array
*
* @return array
*/
public function toArray()
{
return $this->getArrayCopy();
}

/**
* Serialize to query string
*
* @return string
*/
public function toString()
{
return http_build_query($this);
}

/**
* Retrieve by key
*
* Returns null if the key does not exist.
*
* @param string $name
* @return mixed
*/
public function offsetGet($name)
{
if (isset($this[$name])) {
return parent::offsetGet($name);
}
return null;
}

/**
* @param string $name
* @param mixed $default optional default value
* @return mixed
*/
public function get($name, $default = null)
{
if (isset($this[$name])) {
return parent::offsetGet($name);
}
return $default;
}

/**
* @param string $name
* @param mixed $value
* @return $this
*/
public function set($name, $value)
{
$this[$name] = $value;
return $this;
}
}
34 changes: 34 additions & 0 deletions src/ParametersDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Zend\Stdlib;

use ArrayAccess,
Countable,
Serializable,
Traversable;

/*
* Basically, an ArrayObject. You could simply define something like:
* class QueryParams extends ArrayObject implements Parameters {}
* and have 90% of the functionality
*/
interface ParametersDescription extends ArrayAccess, Countable, Serializable, Traversable
{
public function __construct(array $values = null);

/* Allow deserialization from standard array */
public function fromArray(array $values);

/* Allow deserialization from raw body; e.g., for PUT requests */
public function fromString($string);

/* Allow serialization back to standard array */
public function toArray();

/* Allow serialization to query format; e.g., for PUT or POST requests */
public function toString();

public function get($name, $default = null);

public function set($name, $value);
}
8 changes: 8 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Zend\Stdlib;

class Request extends Message implements RequestDescription
{
// generic request implementation
}
7 changes: 7 additions & 0 deletions src/RequestDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Zend\Stdlib;

interface RequestDescription extends MessageDescription
{
}
8 changes: 8 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Zend\Stdlib;

class Response extends Message implements ResponseDescription
{
// generic response implementation
}
8 changes: 8 additions & 0 deletions src/ResponseDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Zend\Stdlib;

interface ResponseDescription extends MessageDescription
{

}
Loading

0 comments on commit cc4782c

Please sign in to comment.