public function testCanSetUserObject() { require_once __DIR__ . '/fields-test/TestUser.php'; $user = new \DewdropTest\TestUser(); $this->fields->setUser($user); $this->assertInstanceOf('Dewdrop\\Fields\\UserInterface', $this->fields->getUser()); }
/** * Filter the supplied Fields object using the filter's callback. * * @param Fields $fields * @return Fields */ public function apply(Fields $fields) { $filteredFields = new Fields([], $fields->getUser()); foreach ($fields as $field) { if (true === call_user_func($this->callback, $field)) { $filteredFields->add($field); } } return $filteredFields; }
/** * Apply the field to the supplied set of fields. If after filtering no * fields are left, we'll return the full set of fields as a fallback. * If no preferences are saved to the DB, we will use either pre-defined * defaults (@see setDefaults()) or the first 4 fields. * * @param Fields $fields * @return Fields */ public function apply(Fields $fields) { if (!$this->selections) { $this->selections = $this->load(); } if (0 !== count($this->defaultFields)) { $defaults = $this->defaultFields; } else { $defaults = []; foreach ($fields as $field) { if (4 > count($defaults)) { $defaults[] = $field->getId(); } } } $output = new Fields([], $fields->getUser()); foreach ($fields as $field) { if (in_array($field->getId(), $this->selections) || !count($this->selections) && in_array($field->getId(), $defaults)) { $output[] = $field; } } // If there are no fields found in the output, return the original fields array as a fallback if (!count($output)) { return $fields; } return $output; }
/** * Apply the filter to the supplied set of fields. In return, you'll end * up with a \Dewdrop\Fields\GroupedFields object that reflects the sort * order and grouping preferences contained in this filter. You can use * that object either as a normal \Dewdrop\Fields object or you can call * getGroups() on it to get the fields back in their assigned groups. * * @param Fields $currentFields * @return GroupedFields */ public function apply(Fields $currentFields) { if (!$this->loadedDbData) { $this->loadedDbData = $this->load(); } $groupedFields = new GroupedFields([], $currentFields->getUser()); foreach ($this->loadedDbData as $fieldConfig) { // Ungrouped fields come after grouped fields if (!$fieldConfig['group_id']) { continue; } $fieldId = $fieldConfig['field_id']; $groupId = $fieldConfig['group_id']; if ($currentFields->has($fieldId)) { if (!$groupedFields->hasGroup($groupId)) { $group = $groupedFields->addGroup($groupId); $group->setTitle($fieldConfig['group_title']); } $groupedFields->getGroup($groupId)->add($currentFields->get($fieldId)); } } $ungrouped = $groupedFields->addGroup('ungrouped'); $ungrouped->setTitle('Other'); foreach ($this->loadedDbData as $fieldConfig) { if (!$fieldConfig['group_id']) { $id = $fieldConfig['field_id']; if ($currentFields->has($id)) { $ungrouped->add($currentFields->get($id)); } } } foreach ($currentFields as $field) { if (!$groupedFields->has($field->getId())) { $ungrouped->add($field); } } return $groupedFields; }