Skip to content

Commit

Permalink
Reworked grid item classes
Browse files Browse the repository at this point in the history
  • Loading branch information
pfilsx committed Oct 18, 2019
1 parent 5216263 commit 123c20c
Show file tree
Hide file tree
Showing 26 changed files with 396 additions and 237 deletions.
14 changes: 9 additions & 5 deletions src/Grid/Columns/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Exception;
use Pfilsx\DataGrid\DataGridException;
use Pfilsx\DataGrid\Grid\DataGridItem;
use Pfilsx\DataGrid\Grid\Items\DataGridItemInterface;
use Twig\Template;

class ActionColumn extends AbstractColumn
Expand Down Expand Up @@ -66,7 +66,7 @@ public function getCellContent($entity)
}

/**
* @param DataGridItem $item
* @param DataGridItemInterface $item
* @param string $action
* @return mixed|string
* @throws Exception
Expand All @@ -76,9 +76,13 @@ protected function generateUrl($item, $action)
if (is_callable($this->urlGenerator)) {
return call_user_func_array($this->urlGenerator, [$item, $action, $this->container['router']]);
} elseif (!empty($this->identifier) && $item->has($this->identifier)) {
return $this->container['router']->generate($this->pathPrefix . $action, ['id' => $item->get($this->identifier)]);
} elseif ($item->has('id')) {
return $this->container['router']->generate($this->pathPrefix . $action, ['id' => $item->get('id')]);
return $this->container['router']->generate($this->pathPrefix . $action, [
'id' => $item->get($this->identifier)
]);
} elseif ($item->hasIdentifier()) {
return $this->container['router']->generate($this->pathPrefix . $action, [
'id' => $item->get($item->getIdentifier())
]);
} else {
throw new DataGridException('Could not generate url for action: ' . $action);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Grid/DataGridFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function createGrid(string $gridType, $dataProvider): DataGrid
if (!is_subclass_of($gridType, AbstractGridType::class)) {
throw new InvalidArgumentException('Expected subclass of ' . AbstractGridType::class);
}
$provider = DataProvider::create($dataProvider, $this->container['doctrine']->getManager());
$provider = DataProvider::create($dataProvider, $this->container['doctrine']);
$this->gridBuilder->setProvider($provider);
$this->filterBuilder->setProvider($provider);

Expand Down
111 changes: 0 additions & 111 deletions src/Grid/DataGridItem.php

This file was deleted.

23 changes: 23 additions & 0 deletions src/Grid/Items/ArrayGridItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php


namespace Pfilsx\DataGrid\Grid\Items;


use Pfilsx\DataGrid\DataGridException;

class ArrayGridItem extends DataGridItem
{
public function has(string $attribute): bool
{
return array_key_exists($attribute, $this->data);
}

public function get(string $attribute)
{
if ($this->has($attribute)){
return $this->data[$attribute];
}
throw new DataGridException('Unknown property ' . $attribute);
}
}
130 changes: 130 additions & 0 deletions src/Grid/Items/DataGridItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php


namespace Pfilsx\DataGrid\Grid\Items;


use Pfilsx\DataGrid\DataGridException;

abstract class DataGridItem implements DataGridItemInterface
{

protected $data;
protected $identifier;

public function __construct($data, $identifier = null)
{
$this->data = $data;
$this->identifier = $identifier;
}

public final function getData()
{
return $this->data;
}

public final function setData($data)
{
$this->data = $data;
}

public final function hasIdentifier(): bool
{
return $this->identifier !== null;
}

public final function getIdentifier()
{
return $this->identifier;
}

public final function setIdentifier($identifier)
{
$this->identifier = $identifier;
}

/**
* Whether a offset exists
* @param string $offset - An offset to check for.
* @return boolean true on success or false on failure.
* The return value will be casted to boolean if non-boolean was returned.
*/
public final function offsetExists($offset)
{
return $this->has($offset);
}

/**
* Offset to retrieve
* @param string $offset - The offset to retrieve.
* @return mixed Can return all value types.
*/
public final function offsetGet($offset)
{
return $this->get($offset);
}

/**
* Offset to set
* @param string $offset - The offset to assign the value to.
* @param mixed $value - The value to set.
* @return void
* @throws DataGridException
*/
public final function offsetSet($offset, $value)
{
throw new DataGridException("Trying to set read-only property: $offset");
}

/**
* Offset to unset
* @param string $offset - The offset to unset.
* @return void
* @throws DataGridException
*/
public final function offsetUnset($offset)
{
throw new DataGridException("Trying to unset read-only property: $offset");
}

/**
* Magic getter
* @param string $attribute
* @return mixed
*/
public final function __get(string $attribute)
{
return $this->get($attribute);
}

/**
* Magic setter
* @param string $attribute
* @param $value
* @throws DataGridException
*/
public final function __set(string $attribute, $value)
{
throw new DataGridException("Trying to set read-only property: $attribute");
}

/**
* Magic unset
* @param string $attribute
* @throws DataGridException
*/
public final function __unset(string $attribute)
{
throw new DataGridException("Trying to unset read-only property: $attribute");
}

/**
* Magic isset
* @param string $attribute
* @return bool
*/
public function __isset(string $attribute)
{
return $this->has($attribute);
}
}
26 changes: 26 additions & 0 deletions src/Grid/Items/DataGridItemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


namespace Pfilsx\DataGrid\Grid\Items;


use ArrayAccess;

interface DataGridItemInterface extends ArrayAccess
{
public function __construct($data, $identifier = null);

public function has(string $attribute): bool;

public function get(string $attribute);

public function __get(string $attribute);
public function __isset(string $attribute);

public function getData();

public function hasIdentifier(): bool;

public function getIdentifier();

}
42 changes: 42 additions & 0 deletions src/Grid/Items/EntityGridItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php


namespace Pfilsx\DataGrid\Grid\Items;


use Pfilsx\DataGrid\DataGridException;

class EntityGridItem extends DataGridItem
{

public function has(string $attribute): bool
{
list($camelAttribute, $getter) = $this->getPropertyAccessVariations($attribute);
return method_exists($this->data, $getter)
|| property_exists($this->data, $attribute)
|| property_exists($this->data, $camelAttribute);
}

public function get(string $attribute)
{
list($camelAttribute, $getter) = $this->getPropertyAccessVariations($attribute);
if (method_exists($this->data, $getter)) {
return $this->data->$getter();
}
if (property_exists($this->data, $attribute)) {
return $this->data->$attribute;
}
if (property_exists($this->data, $camelAttribute)) {
return $this->data->$camelAttribute;
}
throw new DataGridException('Unknown property ' . $attribute . ' in ' . get_class($this->data));
}


private function getPropertyAccessVariations(string $attribute): array
{
$camelAttribute = str_replace('_', '', $attribute);
$getter = 'get' . $camelAttribute;
return [$camelAttribute, $getter];
}
}
Loading

0 comments on commit 123c20c

Please sign in to comment.