public function testGetLabel() { $column = new Column('test_name', 'testField'); $this->assertEquals('test_name', $column->getLabel()); $column = new Column('test_name', 'testField', ['label' => 'Test label']); $this->assertEquals('Test label', $column->getLabel()); $column = new Column('test_name', 'testField', ['label' => false]); $this->assertEquals('', $column->getLabel()); $column = new Column('test_name', 'testField', ['label' => 42]); $this->setExpectedException('\\Exception', 'invalid label option: 42'); $column->getLabel(); }
/** * @param Column $column * @return $this * @throws \Exception */ public function addColumn(Column $column) { if (isset($this->columns[$column->getName()])) { throw new \Exception(sprintf('a column with the name "%s" already exists.', $column->getName())); } $this->columns[$column->getName()] = $column; if ($column instanceof EntitiesCountColumn) { $this->hasCountColumns = true; } elseif ($column instanceof EntitiesScalarColumn) { $this->hasScalarColumns = true; } if (false !== $column->getOptions()['filter']) { $this->hasColumnFilter = true; } $column->setTable($this); return $this; }
/** * @param Column $column * @return string */ private function getPrefixedField(Column $column) { if ($column instanceof EntitiesCountColumn) { return $column->getField() . '_count'; } elseif ($column instanceof EntitiesScalarColumn) { return $column->getField() . '_' . $column->getOperation(); } elseif ($column instanceof EntityColumn) { return $column->getEntityPrefix() . '.' . $column->getEntityField(); } return $this->table->getPrefix() . '.' . $column->getField(); }
/** * @param mixed $object * @param string $name * @param Column $column * @return mixed * @throws \Exception */ private static function callGetterByColumName($object, $name, Column $column) { $methods = []; $methods[] = 'get' . ucfirst($name); foreach ($methods as $method) { if (is_array($object) || $object instanceof \ArrayAccess) { if (!$column instanceof EntitiesColumn) { throw new \Exception(sprintf('unexpected array data for column "%s"', $column->getName())); } if (sizeof($object) > $column->getOptions()['display_join_max_entries']) { return '... ' . sizeof($object); } $result = []; foreach ($object as $entity) { if (method_exists($entity, $method)) { $result[] = $entity->{$method}(); } } return join($column->getOptions()['display_join_glue'], $result); } else { $sub = null; $pos = strpos($method, '.'); if (false !== $pos) { $sub = substr($method, $pos + 1); $method = substr($method, 0, $pos); } if (method_exists($object, $method)) { $result = $object->{$method}(); if (null !== $sub && null !== $result) { $result = self::callGetterByColumName($result, $sub, $column); } return $result; } } } throw new \Exception(sprintf('no getter found for property "%s" in object of class "%s".', $name, get_class($object))); }