function testHTMLFormBuilder() { $formFields = ['Name', 'name2' => 'A label for name2', 'textarea' => ['type' => 'textarea', 'label' => 'A textarea']]; $form = \arc\html::formBuilder($formFields); $formHtml = (string) $form; $parsed = \arc\html::parse($formHtml); $this->assertEquals(3, count($parsed->children())); $Name = $parsed->find('#Name')[0]; $this->assertEquals($Name['id'], 'Name'); }
protected function getAttributes($attributes) { $result = ''; if (count($attributes)) { foreach ($attributes as $name => $value) { $result .= \arc\html::attribute($name, $value); } } return $result; }
function testEncoding() { $euro = "\\uc280"; $htmlString = "<html><head><title>Encodingtest</title></head><body>{$euro}</body></html>"; $html = \arc\html::parse($htmlString, "utf-8"); $euroEl = $html->body->nodeValue; $this->assertEquals($euro, (string) $euroEl); $htmlString = "<html><body>{$euro}</body></html>"; $html = \arc\html::parse($htmlString, "utf-8"); $euroEl = $html->body->nodeValue; $this->assertEquals($euro, (string) $euroEl); $htmlString = "<ul><li>{$euro}</li></ul>"; $html = \arc\html::parse($htmlString, "utf-8"); $euroEl = $html->ul->li->nodeValue; $this->assertEquals($euro, (string) $euroEl); }
function testEncoding() { $entities = 'Iñtërnâtiônàlizætiøn'; $i18n = html_entity_decode($entities, ENT_HTML5, 'UTF-8'); $htmlString = "<html><head><title>Encodingtest</title></head><body>{$i18n}</body></html>"; $html = \arc\html::parse($htmlString, "utf-8"); $i18nEl = $html->body->nodeValue; $this->assertEquals($i18n, (string) $i18nEl); $htmlString = "<html><body>{$i18n}</body></html>"; $html = \arc\html::parse($htmlString, "utf-8"); $i18nEl = $html->body->nodeValue; $this->assertEquals($i18n, (string) $i18nEl); $htmlString = "<ul><li>{$i18n}</li></ul>"; $html = \arc\html::parse($htmlString, "utf-8"); $i18nEl = $html->ul->li->nodeValue; $this->assertEquals($i18n, (string) $i18nEl); }
<?php require __DIR__ . '/../vendor/autoload.php'; use arc\html as h; use arc\xml as x; $client = \arc\cache::proxy(\arc\http::client(), function ($params) { return \arc\http\headers::parseCacheTime($params['target']->responseHeaders); }); $feed = $client->get('https://www.nasa.gov/rss/dyn/breaking_news.rss'); try { $rss = x::parse($feed); } catch (\Exception $e) { $rss = h::parse($feed); } $items = $rss->find('item'); foreach ($items as $item) { echo h::article(h::h2(h::a(['href' => $item->link->nodeValue], $item->title->nodeValue)), h::div(['class' => 'body'], $item->description->nodeValue)); }
function aggregator($client) { $aggregator = \arc\lambda::prototype(); $aggregator->renderItem = function ($item) { if ($item->enclosure['url']) { $img = h::a(['href' => $item->link->nodeValue], h::img(['src' => $item->enclosure['url'], 'alt' => ''])); } else { $img = ''; } $pubDate = h::time(relativeTime(strtotime(current($item->xpath('.//pubDate|.//dc:date'))))); return h::article(['class' => 'white-panel'], $img, h::h2(h::a(['href' => $item->link->nodeValue], $item->title->nodeValue)), $pubDate, h::p(['class' => 'description'], h::raw(strip_tags($item->description->nodeValue)))); }; $aggregator->renderItems = function ($items) { $itemsString = ''; if (count($items)) { foreach ($items as $item) { $itemsString .= $this->renderItem($item); } } else { $itemsString = h::div(['class' => 'warning'], 'No articles in this feed.'); } return h::raw($itemsString); }; $aggregator->renderFeeds = function ($feeds) { return implode(array_map(function ($feed, $index) { return h::div(['class' => 'feed'], h::h2($feed->channel->title->nodeValue), h::form(['class' => 'remove', 'method' => 'POST'], h::button(['name' => 'removeFeed', 'value' => $index], 'Remove')), h::a(['href' => $feed->channel->link->nodeValue], $feed->channel->link->nodeValue)); }, $feeds, array_keys($feeds))); }; $aggregator->getPubDate = function ($item) { return strtotime(current($item->xpath('.//pubDate|.//dc:date'))); }; $aggregator->getItems = function ($feeds) { $allitems = call_user_func_array('array_merge', array_map(function ($rss) { return array_map(function ($item) use($rss) { $item->appendChild($rss->channel->cloneNode()); return $item; }, $rss->getElementsByTagName('item')); }, $feeds)); usort($allitems, function ($a, $b) { return $this->getPubDate($a) < $this->getPubDate($b); }); return $allitems; }; $aggregator->getFeed = function ($url) use($client) { $feed = $client->get($url); try { $rss = \arc\xml::parse($feed); } catch (\Exception $e) { $rss = \arc\html::parse($feed); } $rss->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/'); return $rss; }; $aggregator->render = function ($feeds) { $feeds = array_map(function ($feed) { return $this->getFeed($feed); }, $feeds); $items = $this->getItems($feeds); return h::section(['class' => 'items'], h::raw($this->renderItems($items))) . h::section(['class' => 'feeds'], h::raw($this->renderFeeds($feeds))); }; return $aggregator; }
public static function formBuilder($fields, $formAttributes = []) { if (!self::$formBuilder) { self::$formBuilder = \arc\prototype::create(['fields' => [], 'attributes' => [], ':parseField' => function ($self, $field, $key) { if (!is_array($field)) { $field = ['name' => $field, 'label' => $field]; } $defaults = ['type' => 'text', 'name' => is_numeric($key) ? 'field_' . $key : $key, 'label' => is_numeric($key) ? false : $key, 'value' => '', 'default' => null, 'attributes' => [], 'inputAttributes' => []]; $defaults['label'] = $defaults['name']; return $field + $defaults; }, ':getValue' => function ($self, $field) { $selected = null; if ($field['value']) { $selected = $field['value']; } else { if ($field['default']) { $selected = $field['default']; } } return $selected; }, ':renderOptions' => function ($self, $field) { $selected = $self->getValue($field); $options = ''; foreach ((array) $field['options'] as $key => $option) { $attributes = ['value' => $key]; if ($key === $selected) { $attributes['selected'] = true; } $options .= \arc\html::option($attributes, $option); } return $options; }, ':renderInputSelect' => function ($self, $field) { return \arc\html::select(['id' => $field['name'], 'name' => $field['name']] + (array) $field['inputAttributes'], $self->renderOptions($field)); }, ':renderInputRadioGroup' => function ($self, $field) { $selected = $self->getValue($field); $radios = ''; foreach ((array) $field['options'] as $key => $option) { $attributes = $field['inputAttributes']; if ($key === $selected) { $attributes['checked'] = true; } $radios .= $self->renderInputRadio(['inputAttributes' => $attributes] + $field); } return \arc\html::div(['class' => 'arc-form-radiogroup'], $radios); }, ':renderInputRadio' => function ($self, $field) { return \arc\html::input(['type' => 'radio', 'id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']); }, ':renderInputTextarea' => function ($self, $field) { return \arc\html::textarea(['id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']); }, ':renderInputPassword' => function ($self, $field) { return \arc\html::input(['type' => 'password', 'id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']); }, ':renderInputText' => function ($self, $field) { return \arc\html::input(['type' => 'text', 'id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']); }, ':renderInputHidden' => function ($self, $field) { return \arc\html::input(['type' => 'hidden', 'id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']); }, ':renderInput' => function ($self, $field) { $renderMethod = 'renderInput' . ucfirst($field['type']); if (!isset($self->{$renderMethod})) { throw new \arc\ExceptionMethodNotFound('No render method for input type ' . $field['type'], 404); } return $self->{$renderMethod}($field); }, ':renderLabel' => function ($self, $field) { if ($field['label']) { return \arc\html::label(['for' => $field['name']], $field['label']); } else { return ''; } }, ':renderField' => function ($self, $field, $key = null) { $field = $self->parseField($field, $key); $contents = $self->renderLabel($field); $contents .= $self->renderInput($field); $attributes = isset($field['attributes']) ? $field['attributes'] : []; if (isset($field['class'])) { $attributes['class'] = $field['class']; } return \arc\html::div($attributes, $contents); }, ':__toString' => function ($self) { $fields = ''; foreach ($self->fields as $key => $field) { $fields .= $self->renderField($field, $key); } return \arc\html::form($self->attributes, $fields); }]); } return \arc\prototype::extend(self::$formBuilder, ['fields' => $fields, 'attributes' => $formAttributes]); }