Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
k-holy committed Dec 5, 2013
2 parents cb6fe04 + 4606f8a commit 0c6e23a
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 104 deletions.
189 changes: 106 additions & 83 deletions AbstractPropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,6 @@
abstract class AbstractPropertyAccessor implements \ArrayAccess, \IteratorAggregate
{

/**
* ArrayAccess::offsetSet()
*
* @param mixed
* @param mixed
*/
public function offsetSet($name, $value)
{
if (!property_exists($this, $name)) {
throw new \InvalidArgumentException(
sprintf('The attribute "%s" does not exists.', $name)
);
}
return $this->{$name} = $value;
}

/**
* ArrayAccess::offsetGet()
*
* @param mixed
* @return mixed
*/
public function offsetGet($name)
{
if (!property_exists($this, $name)) {
throw new \InvalidArgumentException(
sprintf('The attribute "%s" does not exists.', $name)
);
}
return $this->{$name};
}

/**
* ArrayAccess::offsetUnset()
*
* @param mixed
*/
public function offsetUnset($name)
{
if (!property_exists($this, $name)) {
throw new \InvalidArgumentException(
sprintf('The attribute "%s" does not exists.', $name)
);
}
$this->{$name} = null;
}

/**
* ArrayAccess::offsetExists()
*
* @param mixed
* @return bool
*/
public function offsetExists($name)
{
return (property_exists($this, $name) && !is_null($this->{$name}));
}

/**
* 引数なしの場合は全てのプロパティを配列で返します。
* 引数ありの場合は全てのプロパティを引数の配列からセットして$thisを返します。
Expand All @@ -90,49 +32,66 @@ public function properties()
$properties = func_get_arg(0);
if (!is_array($properties) && !($properties instanceof \Traversable)) {
throw new \InvalidArgumentException(
'The properties is not Array and not Traversable.');
sprintf('The properties is not an Array and not Traversable. type:%s',
is_object($properties)
? get_class($properties)
: gettype($properties)
)
);
}
foreach ($properties as $name => $value) {
$this->offsetSet($name, $value);
if (!property_exists($this, $name)) {
throw new \InvalidArgumentException(
sprintf('The property "%s" does not exists.', $name)
);
}
$this->{$name} = $value;
}
return $this;
}
throw new \InvalidArgumentException('Invalid argument count.');
}

/**
* IteratorAggregate::getIterator()
* ArrayAccess::offsetExists()
*
* @return \ArrayIterator
* @param string プロパティ名
* @return bool
*/
public function getIterator()
public function offsetExists($name)
{
return new \ArrayIterator($this->properties());
return (property_exists($this, $name) && $this->{$name} !== null);
}

/**
* 配列に変換して返します。
* __isset
*
* @return array
* @param string プロパティ名
* @return bool
*/
public function toArray()
public function __isset($name)
{
return $this->properties();
return $this->offsetExists($name);
}

/**
* magic setter
* ArrayAccess::offsetGet()
*
* @param string プロパティ名
* @param mixed プロパティ値
* @return mixed
*/
public function __set($name, $value)
public function offsetGet($name)
{
$this->offsetSet($name, $value);
if (!property_exists($this, $name)) {
throw new \InvalidArgumentException(
sprintf('The property "%s" does not exists.', $name)
);
}
return $this->{$name};
}

/**
* magic getter
* __get
*
* @param string プロパティ名
*/
Expand All @@ -142,18 +101,49 @@ public function __get($name)
}

/**
* magic isset
* ArrayAccess::offsetSet()
*
* @param mixed
* @param mixed
*/
public function offsetSet($name, $value)
{
if (!property_exists($this, $name)) {
throw new \InvalidArgumentException(
sprintf('The property "%s" does not exists.', $name)
);
}
return $this->{$name} = $value;
}

/**
* __set
*
* @param string プロパティ名
* @return bool
* @param mixed プロパティ値
*/
public function __isset($name)
public function __set($name, $value)
{
return $this->offsetExists($name);
$this->offsetSet($name, $value);
}

/**
* ArrayAccess::offsetUnset()
*
* @param mixed
*/
public function offsetUnset($name)
{
if (!property_exists($this, $name)) {
throw new \InvalidArgumentException(
sprintf('The property "%s" does not exists.', $name)
);
}
$this->{$name} = null;
}

/**
* magic unset
* __unset
*
* @param string プロパティ名
*/
Expand All @@ -164,21 +154,44 @@ public function __unset($name)

/**
* __toString
*
* @return string
*/
public function __toString()
{
return var_export($this->properties(), true);
}

/**
* IteratorAggregate::getIterator()
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->properties());
}

/**
* __sleep
*
* @param void
* @return void
* @return array
*/
public function __sleep()
{
return array_keys($this->properties(), true);
return array_keys($this->properties());
}

/**
* __clone
*/
public function __clone()
{
foreach ($this->properties() as $name => $value) {
if (is_object($value)) {
$this->{$name} = clone $value;
}
}
}

/**
Expand All @@ -187,11 +200,21 @@ public function __sleep()
* @param array
* @return object
*/
public static function __set_state($attributes)
public static function __set_state($properties)
{
$instance = new static();
$instance->properties($attributes);
$instance->properties($properties);
return $instance;
}

/**
* 配列に変換して返します。
*
* @return array
*/
public function toArray()
{
return $this->properties();
}

}
Loading

0 comments on commit 0c6e23a

Please sign in to comment.