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 into cach…
Browse files Browse the repository at this point in the history
…e_interfaces
  • Loading branch information
Show file tree
Hide file tree
Showing 13 changed files with 586 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis/run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
travisdir=$(dirname $(readlink /proc/$$/fd/255))
travisdir=$(dirname "$0")
testdir="$travisdir/../tests"
testedcomponents=(`cat "$travisdir/tested-components"`)
result=0
Expand Down
1 change: 1 addition & 0 deletions .travis/skipped-components
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Zend/Amf
Zend/Date
Zend/Dojo
Zend/Queue
Zend/Service
Zend/Test
Expand Down
4 changes: 3 additions & 1 deletion .travis/tested-components
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ Zend/Form
Zend/GData
Zend/Http
Zend/InfoCard
Zend/InputFilter
Zend/Json
Zend/Ldap
Zend/Loader
Zend/Locale
Zend/Log
Zend/Mail
Zend/Markup
Zend/Math
Zend/Measure
Zend/Memory
Zend/Mime
Zend/Module
Zend/ModuleManager
Zend/Mvc
Zend/Navigation
Zend/OAuth
Expand Down
45 changes: 45 additions & 0 deletions src/ArraySerializableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib;

/**
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface ArraySerializableInterface
{
/**
* Exchange internal values from provided array
*
* @param array $array
* @return void
*/
public function exchangeArray(array $array);

/**
* Return an array representation of the object
*
* @return array
*/
public function getArrayCopy();
}
5 changes: 5 additions & 0 deletions src/CallbackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ public function call(array $args = array())
{
$callback = $this->getCallback();

// WeakRef object will return null if the real object was disposed
if (null === $callback) {
return null;
}

$isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>=');

if ($isPhp54 && is_string($callback)) {
Expand Down
80 changes: 80 additions & 0 deletions src/Hydrator/ArraySerializable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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
* @subpackage Hydrator
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Hydrator;

use Zend\Stdlib\Exception;

/**
* @category Zend
* @package Zend_Stdlib
* @subpackage Hydrator
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class ArraySerializable implements HydratorInterface
{
/**
* Extract values from the provided object
*
* Extracts values via the object's getArrayCopy() method.
*
* @param object $object
* @return array
* @throws Exception\BadMethodCallException for an $object not implementing getArrayCopy()
*/
public function extract($object)
{
if (!is_callable(array($object, 'getArrayCopy'))) {
throw new Exception\BadMethodCallException(sprintf(
'%s expects the provided object to implement getArrayCopy()',
__METHOD__
));
}
return $object->getArrayCopy();
}

/**
* Hydrate an object
*
* Hydrates an object by passing $data to either its exchangeArray() or
* populate() method.
*
* @param array $data
* @param object $object
* @return object
* @throws Exception\BadMethodCallException for an $object not implementing exchangeArray() or populate()
*/
public function hydrate(array $data, $object)
{
if (is_callable(array($object, 'exchangeArray'))) {
$object->exchangeArray($data);
} else if (is_callable(array($object, 'populate'))) {
$object->populate($data);
} else {
throw new Exception\BadMethodCallException(sprintf(
'%s expects the provided object to implement exchangeArray() or populate()',
__METHOD__
));
}
return $object;
}
}
127 changes: 127 additions & 0 deletions src/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?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
* @subpackage Hydrator
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Hydrator;

use Zend\Stdlib\Exception;

/**
* @category Zend
* @package Zend_Stdlib
* @subpackage Hydrator
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class ClassMethods implements HydratorInterface
{
/**
* CamelCase usage to extract attribute with getter/setter method name
* @var boolean
*/
protected $useCamelCase;

/**
* Define if extract values will use camel case or name with underscore
* @param boolean $useCamelCase
*/
public function __construct($useCamelCase = true)
{
$this->useCamelCase = $useCamelCase;
}

/**
* Extract values from an object with class methods
*
* Extracts the getter/setter of the given $object.
*
* @param object $object
* @return array
* @throws Exception\BadMethodCallException for a non-object $object
*/
public function extract($object)
{
if (!is_object($object)) {
throw new Exception\BadMethodCallException(sprintf(
'%s expects the provided $object to be a PHP object)',
__METHOD__
));
}

$transform = function($letters)
{
$letter = array_shift($letters);
return '_' . strtolower($letter);
};
$attributes = array();
$methods = get_class_methods($object);
foreach($methods as $method) {
if(preg_match('/^get[A-Z]\w*/', $method)) {
// setter verification
$setter = preg_replace('/^get/', 'set', $method);
if(!in_array($setter, $methods)) {
continue;
}
$attribute = substr($method, 3);
$attribute = lcfirst($attribute);
if (!$this->useCamelCase) {
$attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
}
$attributes[$attribute] = $object->$method();
}
}

return $attributes;
}

/**
* Hydrate an object by populating getter/setter methods
*
* Hydrates an object by getter/setter methods of the object.
*
* @param array $data
* @param object $object
* @return object
* @throws Exception\BadMethodCallException for a non-object $object
*/
public function hydrate(array $data, $object)
{
if (!is_object($object)) {
throw new Exception\BadMethodCallException(sprintf(
'%s expects the provided $object to be a PHP object)',
__METHOD__
));
}

$transform = function($letters)
{
$letter = substr(array_shift($letters), 1, 1);
return ucfirst($letter);
};
foreach ($data as $property => $value) {
if (!$this->useCamelCase) {
$property = preg_replace_callback('/(_[a-z])/', $transform, $property);
}
$method = 'set' . ucfirst($property);
$object->$method($value);
}
return $object;
}
}
49 changes: 49 additions & 0 deletions src/Hydrator/HydratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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
* @subpackage Hydrator
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Hydrator;

/**
* @category Zend
* @package Zend_Stdlib
* @subpackage Hydrator
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface HydratorInterface
{
/**
* Extract values from an object
*
* @param object $object
* @return array
*/
public function extract($object);

/**
* Hydrate $object with the provided $data.
*
* @param array $data
* @param object $object
* @return object
*/
public function hydrate(array $data, $object);
}
Loading

0 comments on commit 738f2e6

Please sign in to comment.