. */ namespace SP\Mvc\View\Components; use RuntimeException; use SP\DataModel\DataModelInterface; use SP\Http\Json; /** * Class SelectItemAdapter * * @package SP\Mvc\View\Components */ class SelectItemAdapter implements ItemAdapterInterface { /** * @var array */ protected $items; /** * SelectItemAdapter constructor. * * @param array $items */ public function __construct(array $items) { $this->items = $items; } /** * @param array $items * * @return static */ public static function factory(array $items) { return new static($items); } /** * Returns an array of ids from the given array of objects * * @param array $items * * @return array */ public static function getIdFromArrayOfObjects(array $items) { $ids = []; foreach ($items as $item) { if (is_object($item) && null !== $item->id) { $ids[] = $item->id; } } return $ids; } /** * Returns a JSON like collection of items for a select component * * @return string * @throws \SP\Core\Exceptions\SPException */ public function getJsonItemsFromModel() { $out = []; foreach ($this->items as $item) { if (!$item instanceof DataModelInterface) { throw new RuntimeException(__u('Tipo de objeto incorrecto')); } $out[] = ['id' => $item->getId(), 'name' => $item->getName()]; } return Json::getJson($out); } /** * Returns a collection of items for a select component * * @return string * @throws \SP\Core\Exceptions\SPException */ public function getJsonItemsFromArray() { $out = []; foreach ($this->items as $key => $value) { $out[] = ['id' => $key, 'name' => $value]; } return Json::getJson($out); } /** * Returns a collection of items for a select component and set selected ones from an array * * @param array $selected * @param null $skip * * @return SelectItem[] */ public function getItemsFromModelSelected(array $selected, $skip = null) { $items = $this->getItemsFromModel(); foreach ($items as $item) { if ($skip !== null && $item->getId() === $skip) { $item->setSkip(true); } if (in_array($item->getId(), $selected, false)) { $item->setSelected(true); } } return $items; } /** * Returns a collection of items for a select component * * @return SelectItem[] */ public function getItemsFromModel() { $out = []; foreach ($this->items as $item) { if (!$item instanceof DataModelInterface) { throw new RuntimeException(__u('Tipo de objeto incorrecto')); } $out[] = new SelectItem($item->getId(), $item->getName(), $item); } return $out; } /** * Returns a collection of items for a select component and set selected ones from an array * * @param array $selected * @param bool $useValueAsKey * * @return SelectItem[] */ public function getItemsFromArraySelected(array $selected, $useValueAsKey = false) { $items = $this->getItemsFromArray(); foreach ($items as $item) { if (($useValueAsKey === false && in_array($item->getId(), $selected, false)) || ($useValueAsKey === true && in_array($item->getName(), $selected, false)) ) { $item->setSelected(true); } } return $items; } /** * Returns a collection of items for a select component * * @return SelectItem[] */ public function getItemsFromArray() { $out = []; foreach ($this->items as $key => $value) { $out[] = new SelectItem($key, $value); } return $out; } }