http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 3 LOC: 92 Statements: 26
Legend: executednot executeddead code
Source file Statements Methods Total coverage
Evaluator.php 92.3% 66.7% 89.7%
   
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_Data
12
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
13
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
14
 * @version   $Id: Evaluator.php 418 2010-10-18 21:40:08Z jonathanhawk $
15
 */
16
namespace Xyster\Data\Symbol;
17
/**
18
 * An object that pulls the values out of an object or array given a field
19
 *
20
 * @category  Xyster
21
 * @package   Xyster_Data
22
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
23
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
24
 */
25
class Evaluator
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $_name;
31
32
    /**
33
     * Creates a new field getter
34
     *
35
     * @param string $name  The field name
36
     */
37
    public function __construct( $name )
38
    {
39 40
        if ( $name instanceof Field ) {
40 32
            $name = $name->getName();
41 32
        }
42 40
        $this->_name = (string)$name;
43
    }
44
45
    /**
46
     * Gets the named value out of an array or object
47
     *
48
     * @param mixed $object An array or object
49
     * @return mixed
50
     */
51
    public function evaluate( $object )
52
    {
53 40
        $value = null;
54 40
        if ( is_array($object) || $object instanceof \ArrayAccess ) {
55 29
            if ( !isset($object[$this->_name]) && !array_key_exists($this->_name, $object) ) {
56 1
                throw new InvalidArgumentException("Field name '{$this->_name}' is invalid");
57
            }
58 28
            $value = $object[$this->_name];
59 39
        } else if ( is_object($object) && method_exists($object, 'get'.ucfirst($this->_name)) ) {
60
            // JavaBean-style getter
61 1
            $method = 'get'.ucfirst($this->_name);
62 1
            $value = $object->$method();
63 12
        } else if ( is_object($object) && preg_match('/^[a-z_]\w*$/i', $this->_name) ) {
64
            // name of a real property or one caught by __get()
65 11
            $value = $object->{$this->_name};
66 12
        } else if ( is_object($object) ) {
67
            // this is eval'ed becuse $this->_name might be a method call
68
            // maybe sometime in the future we can do this better...
69
            eval("\$value = \$object->{$this->_name};");
70
        } else {
71 1
            throw new InvalidArgumentException("Only objects or arrays can be evaluated");
72
        }
73 38
        return $value;
74
    }
75
76
    /**
77
     * A shortcut instead of creating a new getter
78
     *
79
     * @param mixed $object An array or object
80
     * @param string $field The field name (or an object that will toString)
81
     * @return mixed
82
     */
83
    static public function get( $object, $field )
84
    {
85 19
        if ( $object instanceof \Xyster\Data\Set && $field instanceof AggregateField ) {
86 1
            return $object->aggregate($field);
87
        }
88
89 19
        $getter = new self($field);
90 19
        return $getter->evaluate($object);
91
    }
92 1
}


Report generated at 2010-10-18T17:19:49-04:00