Inspired by Symfony OptionsResolver component: http://symfony.com/doc/current/components/options_resolver.html.
Author: Victor Puertas (vpgugr@gmail.com)
Example #1
0
 /**
  * Gets the attributes or option resolver for configuration values.
  *
  * @return \Yosymfony\Spress\Core\Support\AttributesResolver
  */
 protected function getConfigResolver()
 {
     if (is_null($this->configResolver) === false) {
         return $this->configResolver;
     }
     $resolver = new AttributesResolver();
     $resolver->setDefault('host', '0.0.0.0', 'string', true)->setDefault('port', 4000, 'integer', true)->setValidator('port', function ($value) {
         return $value >= 0;
     })->setDefault('server_watch_ext', ['html'], 'array', true)->setDefault('parsedown_activated', false, 'bool', true);
     $this->configResolver = $resolver;
     return $this->configResolver;
 }
Example #2
0
 protected function getAttributesResolver(ItemInterface $templateItem)
 {
     $resolver = new AttributesResolver();
     $resolver->setDefault('max_page', 5, 'int')->setDefault('provider', 'site.posts', 'string')->setDefault('permalink', '/page:num')->setDefault('sort_by', '', 'string')->setDefault('sort_type', 'descending', 'string')->setValidator('sort_type', function ($value) {
         switch ($value) {
             case 'descending':
             case 'ascending':
                 return true;
             default:
                 return false;
         }
     });
     $attributes = $templateItem->getAttributes();
     return $resolver->resolve($attributes);
 }
Example #3
0
 private function getResolver()
 {
     $resolver = new AttributesResolver();
     $resolver->setDefault('source_root', '', 'string', true)->setDefault('include', [], 'array')->setDefault('exclude', [], 'array')->setDefault('text_extensions', [], 'array', true)->setDefault('attribute_syntax', 'yaml', 'string')->setValidator('attribute_syntax', function ($value) {
         switch ($value) {
             case 'yaml':
             case 'json':
                 return true;
                 break;
             default:
                 return false;
                 break;
         }
     })->setDefault('avoid_renderizer_path', [], 'array')->setDefault('avoid_renderizer_extension', [], 'array');
     return $resolver;
 }
 /**
  * Gets the attribute resolver.
  *
  * @return \Yosymfony\Spress\Core\Support\AttributesResolver\AttributesResolver
  */
 protected function getResolver()
 {
     $resolver = new AttributesResolver();
     $resolver->setDefault('name', '', 'string')->setDefault('description', '', 'string')->setDefault('author', '', 'string')->setDefault('license', '', 'string');
     return $resolver;
 }
Example #5
0
 protected function getAttributesResolver(ItemInterface $templateItem)
 {
     $resolver = new AttributesResolver();
     $resolver->setDefault('taxonomy_attribute', 'categories', 'string')->setDefault('permalink', '/:name')->setDefault('pagination_permalink', '/page:num', 'string');
     $attributes = $templateItem->getAttributes();
     return $resolver->resolve($attributes);
 }
Example #6
0
 private function getConfigurationResolver()
 {
     $resolver = new AttributesResolver();
     $resolver->setDefault('debug', false, 'bool', true)->setDefault('env', 'dev', 'string', true)->setValidator('env', function ($value) {
         return strlen($value) > 0;
     })->setDefault('drafts', false, 'bool', true)->setDefault('preserve_path_title', false, 'bool', true)->setDefault('no_html_extension', false, 'bool', true)->setDefault('timezone', 'UTC', 'string', true)->setDefault('url', '', 'string', true)->setDefault('safe', false, 'bool', true)->setDefault('layout_ext', [], 'array', true)->setDefault('data_sources', [], 'array', true)->setDefault('collections', [], 'array')->setDefault('permalink', 'pretty', 'string', true)->setDefault('markdown_ext', [], 'array', true);
     return $resolver;
 }
 /**
  * @expectedException \Yosymfony\Spress\Core\ContentManager\Exception\AttributeValueException
  */
 public function testInvalidValue()
 {
     $a = new AttributesResolver();
     $a->setDefault('port', 4000, 'int', false, false)->setValidator('port', function ($value) {
         return $value > 0 && $value <= 65535;
     });
     $result = $a->resolve(['port' => -1]);
 }
Example #8
0
 private function sortItems()
 {
     $resolver = new AttributesResolver();
     $resolver->setDefault('sort_by', '', 'string')->setDefault('sort_type', 'descending', 'string')->setValidator('sort_type', function ($value) {
         switch ($value) {
             case 'descending':
             case 'ascending':
                 return true;
             default:
                 return false;
         }
     });
     foreach ($this->CollectionManager->getCollectionItemCollection() as $collection) {
         $attributes = $resolver->resolve($collection->getAttributes());
         if (empty($attributes['sort_by']) === true) {
             continue;
         }
         $sortBy = $attributes['sort_by'];
         $isDescending = $attributes['sort_type'] === 'descending';
         $this->itemCollection->sortItems($sortBy, $isDescending, [$collection->getName()]);
         $this->setRelationships($collection->getName());
     }
 }
Example #9
0
 protected function getAttributesResolver(ItemInterface $templateItem)
 {
     $resolver = new AttributesResolver();
     $resolver->setDefault('max_page', 5, 'int')->setDefault('provider', 'site.posts', 'string')->setDefault('permalink', '/page:num');
     $attributes = $templateItem->getAttributes();
     return $resolver->resolve($attributes);
 }
Example #10
0
 protected function getPostAttributesResolver()
 {
     $resolver = new AttributesResolver();
     $resolver->setDefault('categories', [], 'array')->setDefault('tags', [], 'array');
     return $resolver;
 }