| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- namespace Dcat\Admin\Grid;
- use Closure;
- use Dcat\Admin\Grid;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Contracts\Support\Htmlable;
- use Illuminate\Contracts\Support\Jsonable;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Support\Arr;
- class Row implements Arrayable
- {
- /**
- * @var Grid
- */
- protected $grid;
- /**
- * Row data.
- *
- * @var
- */
- protected $data;
- /**
- * Attributes of row.
- *
- * @var array
- */
- protected $attributes = [];
- public function __construct(Grid $grid, $data)
- {
- $this->grid = $grid;
- $this->data = $data;
- }
- /**
- * Get the value of the model's primary key.
- *
- * @return mixed
- */
- public function getKey()
- {
- return $this->model->getKey();
- }
- /**
- * Get attributes in html format.
- *
- * @return string
- */
- public function getRowAttributes()
- {
- return $this->formatHtmlAttributes($this->attributes);
- }
- /**
- * Get column attributes.
- *
- * @param string $column
- *
- * @return string
- */
- public function getColumnAttributes($column)
- {
- if (
- ($column = $this->grid->getColumns()->get($column))
- && ($attributes = $column->getAttributes())
- ) {
- return $this->formatHtmlAttributes($attributes);
- }
- return '';
- }
- /**
- * Format attributes to html.
- *
- * @param array $attributes
- *
- * @return string
- */
- private function formatHtmlAttributes($attributes = [])
- {
- $attrArr = [];
- foreach ($attributes as $name => $val) {
- $attrArr[] = "$name=\"$val\"";
- }
- return implode(' ', $attrArr);
- }
- /**
- * Set attributes.
- *
- * @param array $attributes
- */
- public function setAttributes(array $attributes)
- {
- $this->attributes = $attributes;
- }
- /**
- * Set style of the row.
- *
- * @param array|string $style
- */
- public function style($style)
- {
- if (is_array($style)) {
- $style = implode('', array_map(function ($key, $val) {
- return "$key:$val";
- }, array_keys($style), array_values($style)));
- }
- if (is_string($style)) {
- $this->attributes['style'] = $style;
- }
- }
- /**
- * Get data of this row.
- *
- * @return mixed
- */
- public function model()
- {
- return $this->data;
- }
- /**
- * Getter.
- *
- * @param mixed $attr
- *
- * @return mixed
- */
- public function __get($attr)
- {
- return Arr::get($this->data, $attr);
- }
- /**
- * Setter.
- *
- * @param mixed $attr
- * @param mixed $value
- *
- * @return void
- */
- public function __set($attr, $value)
- {
- Arr::set($this->data, $attr, $value);
- }
- /**
- * Get or set value of column in this row.
- *
- * @param string $name
- * @param mixed $value
- *
- * @return $this|mixed
- */
- public function column($name, $value = null)
- {
- if (is_null($value)) {
- $column = Arr::get($this->data, $name);
- return $this->output($column);
- }
- if ($value instanceof Closure) {
- $value = $value->call($this, $this->column($name));
- }
- Arr::set($this->data, $name, $value);
- return $this;
- }
- /**
- * @return array
- */
- public function toArray()
- {
- if ($this->data instanceof Arrayable) {
- return $this->data->toArray();
- }
- return (array) $this->data;
- }
- /**
- * Output column value.
- *
- * @param mixed $value
- *
- * @return mixed|string
- */
- protected function output($value)
- {
- if ($value instanceof Renderable) {
- $value = $value->render();
- }
- if ($value instanceof Htmlable) {
- $value = $value->toHtml();
- }
- if ($value instanceof Jsonable) {
- $value = $value->toJson();
- }
- if (! is_null($value) && ! is_scalar($value)) {
- return sprintf('<pre class="dump">%s</pre>', json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
- }
- return $value;
- }
- }
|