1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Validator\Sitemap;
11
12use Zend\Validator\AbstractValidator;
13
14/**
15 * Validates whether a given value is valid as a sitemap <changefreq> value
16 *
17 * @link       http://www.sitemaps.org/protocol.php Sitemaps XML format
18 */
19class Changefreq extends AbstractValidator
20{
21    /**
22     * Validation key for not valid
23     *
24     */
25    const NOT_VALID = 'sitemapChangefreqNotValid';
26    const INVALID   = 'sitemapChangefreqInvalid';
27
28    /**
29     * Validation failure message template definitions
30     *
31     * @var array
32     */
33    protected $messageTemplates = [
34        self::NOT_VALID => "The input is not a valid sitemap changefreq",
35        self::INVALID   => "Invalid type given. String expected",
36    ];
37
38    /**
39     * Valid change frequencies
40     *
41     * @var array
42     */
43    protected $changeFreqs = [
44        'always',  'hourly', 'daily', 'weekly',
45        'monthly', 'yearly', 'never'
46    ];
47
48    /**
49     * Validates if a string is valid as a sitemap changefreq
50     *
51     * @link http://www.sitemaps.org/protocol.php#changefreqdef <changefreq>
52     *
53     * @param  string  $value  value to validate
54     * @return bool
55     */
56    public function isValid($value)
57    {
58        if (! is_string($value)) {
59            $this->error(self::INVALID);
60            return false;
61        }
62
63        $this->setValue($value);
64        if (! is_string($value)) {
65            return false;
66        }
67
68        if (! in_array($value, $this->changeFreqs, true)) {
69            $this->error(self::NOT_VALID);
70            return false;
71        }
72
73        return true;
74    }
75}
76