| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* This source file is subject to the new BSD license that is bundled |
| 6 |
|
* with this package in the file LICENSE.txt. |
| 7 |
|
* It is also available through the world-wide-web at this URL: |
| 8 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 9 |
|
* |
| 10 |
|
* @category Xyster |
| 11 |
|
* @package Xyster_Type |
| 12 |
|
* @copyright Copyright LibreWorks, LLC (http://libreworks.net) |
| 13 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 14 |
|
* @version $Id: Factory.php 418 2010-10-18 21:40:08Z jonathanhawk $ |
| 15 |
|
*/ |
| 16 |
|
namespace Xyster\Type\Property; |
| 17 |
|
/** |
| 18 |
|
* Creates Xyster_Type_Property_Interface objects |
| 19 |
|
* |
| 20 |
|
* @category Xyster |
| 21 |
|
* @package Xyster_Type |
| 22 |
|
* @copyright Copyright LibreWorks, LLC (http://libreworks.net) |
| 23 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 24 |
|
*/ |
| 25 |
|
class Factory |
| 26 |
|
{ |
| 27 |
|
protected static $_props = array(); |
| 28 |
|
|
| 29 |
|
/** |
| 30 |
|
* Gets the property wrapper appropriate for the object |
| 31 |
|
* |
| 32 |
|
* @param object $target |
| 33 |
|
* @param string $property |
| 34 |
|
* @return IProperty |
| 35 |
|
*/ |
| 36 |
|
public static function get( $target, $property ) |
| 37 |
|
{ |
| 38 |
11 |
if ( is_array($target) ) { |
| 39 |
|
return new Map($property); |
| 40 |
11 |
} else if ( is_object($target) ) { |
| 41 |
11 |
$className = get_class($target); |
| 42 |
11 |
if ( !array_key_exists($className, self::$_props) ) { |
| 43 |
4 |
$magicCall = method_exists($target, '__call'); |
| 44 |
4 |
$methodExists = method_exists($target, 'set' . ucfirst($property)) |
| 45 |
4 |
|| method_exists($target, 'get' . ucfirst($property)); |
| 46 |
4 |
$propertyExists = property_exists($target, $property); |
| 47 |
4 |
if ( $target instanceof \ArrayAccess && !$propertyExists && !$methodExists ) { |
| 48 |
|
self::$_props[$className] = '\Xyster\Type\Property\Map'; |
| 49 |
4 |
} else if ( $methodExists || $magicCall ) { |
| 50 |
3 |
self::$_props[$className] = '\Xyster\Type\Property\Method'; |
| 51 |
3 |
} else { |
| 52 |
1 |
self::$_props[$className] = '\Xyster\Type\Property\Direct'; |
| 53 |
|
} |
| 54 |
4 |
} |
| 55 |
11 |
$propertyClassName = self::$_props[$className]; |
| 56 |
11 |
return new $propertyClassName($property); |
| 57 |
|
} else { |
| 58 |
|
throw new \Xyster\Type\InvalidTypeException('You can only create property wrappers for arrays or objects'); |
| 59 |
|
} |
| 60 |
|
} |
| 61 |
1 |
} |