-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
396 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
Oops, something went wrong.