Returns various information on the properties of an object.
public static properties ( mixed $class, array $options = [] ) : mixed | ||
$class | mixed | A string class name or an object instance, from which to get methods. |
$options | array | Set of options applied directly (check `_items()` for more options): - `'properties'`: array of properties to gather information from. - `'self'`: If true (default), only returns properties defined in `$class`, excluding properties from inherited classes. |
return | mixed | Returns an array with information about the properties from the class given in $class or null on error. |
private function getMap($class) { $properties = Inspector::properties($class, array('public' => false)); //parse out fields $fields = array(); /** @var \ReflectionProperty $p */ foreach ($properties as $p) { if (($p->getModifiers() & \ReflectionProperty::IS_PROTECTED) == \ReflectionProperty::IS_PROTECTED) { $name = $p->getName(); if ($name[0] != '_') { $fields[$name] = Docblock::comment($p->getDocComment()); } } } //Parse out types $ret = array(); foreach ($fields as $field => $data) { if (isset($data['tags']['var'])) { $ret[$field] = $this->dynamicType($data['tags']['var']); } } return $ret; }
protected static function _class(array $object, array $data, array $options = array()) { $identifier = $object['identifier']; $proto = array('parent' => get_parent_class($identifier), 'methods' => Inspector::methods($identifier, null, array('public' => false)), 'properties' => Inspector::properties($identifier, array('public' => false))); $classes = Libraries::find($object['library'], array('recursive' => true)); $proto['subClasses'] = array_filter($classes, function ($class) use($identifier) { if (preg_match('/\\\\(libraries)\\\\|\\\\(mocks)\\\\|\\\\(tests)\\\\/', $class)) { return false; } try { return get_parent_class($class) == $identifier; } catch (Exception $e) { return false; } }); sort($proto['subClasses']); return $proto + $object + array('tags' => isset($data['tags']) ? $data['tags'] : array()); }
/** * Get the properties for the class * * @param string $class * @param array $options * @return array */ protected function _properties($class, $options = array()) { $defaults = array('name' => null); $options += $defaults; $properties = Inspector::properties($class); $results = array(); foreach ($properties as &$property) { $comment = Docblock::comment($property['docComment']); $description = trim($comment['description']); $type = isset($comment['tags']['var']) ? strtok($comment['tags']['var'], ' ') : null; $name = str_replace('_', '-', Inflector::underscore($property['name'])); $usage = $type == 'boolean' ? "-{$name}" : "--{$name}=" . strtoupper($name); $results[$name] = compact('name', 'description', 'type', 'usage'); if ($name == $options['name']) { return array($name => $results[$name]); } } return $results; }
/** * Tests getting static and non-static properties from various types of classes. */ public function testGetClassProperties() { $result = array_map(function ($property) { return $property['name']; }, Inspector::properties(__CLASS__)); $expected = array('test', 'test2'); $this->assertEqual($expected, $result); $result = array_map(function ($property) { return $property['name']; }, Inspector::properties(__CLASS__, array('public' => false))); $expected = array('test', 'test2', '_test'); $this->assertEqual($expected, $result); $result = Inspector::properties(__CLASS__); $expected = array(array('modifiers' => array('public'), 'docComment' => false, 'name' => 'test', 'value' => null), array('modifiers' => array('public', 'static'), 'docComment' => false, 'name' => 'test2', 'value' => 'bar')); $this->assertEqual($expected, $result); $result = array_map(function ($property) { return $property['name']; }, Inspector::properties('lithium\\action\\Controller')); $this->assertTrue(in_array('request', $result)); $this->assertTrue(in_array('response', $result)); $this->assertFalse(in_array('_render', $result)); $this->assertFalse(in_array('_classes', $result)); $result = array_map(function ($property) { return $property['name']; }, Inspector::properties('lithium\\action\\Controller', array('public' => false))); $this->assertTrue(in_array('request', $result)); $this->assertTrue(in_array('response', $result)); $this->assertTrue(in_array('_render', $result)); $this->assertTrue(in_array('_classes', $result)); $this->assertNull(Inspector::properties('\\lithium\\core\\Foo')); }
/** * Tests getting static and non-static properties from various types of classes. * * @return void */ public function testGetClassProperties() { $result = array_map(function ($property) { return $property['name']; }, Inspector::properties(__CLASS__)); $expected = array('test', 'test2'); $this->assertEqual($expected, $result); $result = array_map(function ($property) { return $property['name']; }, Inspector::properties(__CLASS__, array('public' => false))); $expected = array('test', 'test2', '_test'); $this->assertEqual($expected, $result); $result = Inspector::properties(__CLASS__); $expected = array(array('modifiers' => array('public'), 'docComment' => false, 'name' => 'test', 'value' => null), array('modifiers' => array('public', 'static'), 'docComment' => false, 'name' => 'test2', 'value' => 'bar')); $this->assertEqual($expected, $result); }