Skip to content
Permalink
Browse files

ImageItem is ready

  • Loading branch information...
nilportugues committed Dec 10, 2014
1 parent ea2b463 commit 88bbddb71244a9de04d11a9f0e848ef5d0bfc1f9
@@ -6,7 +6,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true"
stopOnFailure="false"
syntaxCheck="true"
bootstrap="vendor/autoload.php"
>
@@ -42,7 +42,7 @@ public function build()
$xml = array_filter($this->xml);
$data = implode("\n", $xml);
return $data;
return $data."\n";
}
/**
@@ -63,11 +63,6 @@ public function getHeaderSize()
return mb_strlen($this->getHeader(), 'UTF-8');
}
/**
* @return string
*/
abstract public function getHeader();
/**
* @return int
*/
@@ -76,11 +71,6 @@ public function getFooterSize()
return mb_strlen($this->getFooter(), 'UTF-8');
}
/**
* @return string
*/
abstract public function getFooter();
/**
* Resets the data structure used to represent the item as XML.
*
@@ -7,126 +7,163 @@
*/
namespace NilPortugues\Sitemap\Item\Image;
use NilPortugues\Sitemap\Item\AbstractItem;
/**
* Class ImageItem
* @package NilPortugues\Sitemap\Items
* @package NilPortugues\Sitemap\Item\Image
*/
class ImageItem extends AbstractItem implements ItemInterface
class ImageItem extends AbstractItem
{
/**
* @var \NilPortugues\Sitemap\Validators\ImageValidator
* @var ImageItemValidator
*/
protected $validator;
/**
*
* @param $loc
*/
public function __construct()
public function __construct($loc)
{
$this->validator = ImageValidator::getInstance();
$this->validator = ImageItemValidator::getInstance();
$this->xml = $this->reset();
$this->setLoc($loc);
}
/**
* @return string
* Resets the data structure used to represent the item as XML.
*
* @return array
*/
public function getHeader()
protected function reset()
{
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '.
'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
return [
"\t<image:image>",
'loc' => '',
'title' => '',
'caption' => '',
'geolocation' => '',
'license' => '',
"\t</image:image>"
];
}
/**
* @return string
* @param $loc
*
* @throws ImageItemException
* @return $this
*/
public function getFooter()
protected function setLoc($loc)
{
return "</urlset>";
$loc = $this->validator->validateLoc($loc);
if (false === $loc) {
throw new ImageItemException(
sprintf('Provided URL \'%s\' is not a valid value.', $loc)
);
}
$this->xml['loc'] = "\t\t<loc>".$loc."</loc>";
return $this;
}
/**
* @return string
*/
public function getLoc()
public static function getHeader()
{
return (!empty($this->data['loc'])) ? $this->data['loc'] : '';
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '.
'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">'."\n";
}
/**
* @param $loc
*
* @return $this
* @return string
*/
public function setLoc($loc)
public static function getFooter()
{
return $this->setField('loc', $loc);
return "</urlset>";
}
/**
* @param $title
*
* @return $this
* @throws ImageItemException
*/
public function setTitle($title)
{
return $this->setField('title', $title);
$title = $this->validator->validateTitle($title);
if (false === $title) {
throw new ImageItemException(
sprintf('Provided title \'%s\' is not a valid value.', $title)
);
}
$this->xml['title'] = "\t\t\t".'<image:title><![CDATA['.$title.']]></image:title>';
return $this;
}
/**
* @param $caption
*
* @throws ImageItemException
* @return $this
*/
public function setCaption($caption)
{
return $this->setField('caption', $caption);
$caption = $this->validator->validateCaption($caption);
if (false === $caption) {
throw new ImageItemException(
sprintf('Provided caption \'%s\' is not a valid value.', $caption)
);
}
$this->xml['caption'] = "\t\t\t".'<image:caption><![CDATA['.$caption.']]></image:caption>';
return $this;
}
/**
* @param $geolocation
*
* @throws ImageItemException
* @return $this
*/
public function setGeolocation($geolocation)
{
return $this->setField('geolocation', $geolocation);
$geolocation = $this->validator->validateGeolocation($geolocation);
if (false === $geolocation) {
throw new ImageItemException(
sprintf('Provided geolocation \'%s\' is not a valid value.', $geolocation)
);
}
$this->xml['geolocation'] = "\t\t\t".'<image:geolocation><![CDATA['.$geolocation.']]></image:geolocation>';
return $this;
}
/**
* @param $license
*
* @throws ImageItemException
* @return $this
*/
public function setLicense($license)
{
return $this->setField('license', $license);
}
$license = $this->validator->validateLicense($license);
/**
* Collapses the item to its string XML representation.
*
* @return string
*/
public function build()
{
$data = '';
//Create item ONLY if all mandatory data is present.
if (!empty($this->data['loc'])) {
$xml = array();
$xml[] = "\t\t".'<image:image>';
$xml[] = (!empty($this->data['loc'])) ? "\t\t\t".'<image:loc><![CDATA['.$this->data['loc'].']]></image:loc>' : '';
$xml[] = (!empty($this->data['title'])) ? "\t\t\t".'<image:title><![CDATA['.$this->data['title'].']]></image:title>' : '';
$xml[] = (!empty($this->data['caption'])) ? "\t\t\t".'<image:caption><![CDATA['.$this->data['caption'].']]></image:caption>' : '';
$xml[] = (!empty($this->data['geolocation'])) ? "\t\t\t".'<image:geolocation><![CDATA['.$this->data['geolocation'].']]></image:geolocation>' : '';
$xml[] = (!empty($this->data['license'])) ? "\t\t\t".'<image:license><![CDATA['.$this->data['license'].']]></image:license>' : '';
$xml[] = "\t\t".'</image:image>';
$xml = array_filter($xml);
$data = implode("\n", $xml);
if (false === $license) {
throw new ImageItemException(
sprintf('Provided license \'%s\' is not a valid value.', $license)
);
}
return $data;
$this->xml['license'] = "\t\t\t".'<image:license><![CDATA['.$license.']]></image:license>';
return $this;
}
}
@@ -24,57 +24,57 @@ class ImageItemValidator
/**
* @param $title
* @return string
*
* @return string|bool
*/
public function validateTitle($title)
{
$data = '';
if (is_string($title)) {
$data = $title;
return $title;
}
return $data;
return false;
}
/**
* @param $caption
* @return string
*
* @return string|bool
*/
public function validateCaption($caption)
{
$data = '';
if (is_string($caption)) {
$data = $caption;
return $caption;
}
return $data;
return false;
}
/**
* @param $geolocation
* @return string
*
* @return string|bool
*/
public function validateGeolocation($geolocation)
{
$data = '';
if (is_string($geolocation)) {
$data = $geolocation;
return $geolocation;
}
return $data;
return false;
}
/**
* @param $license
* @return string
*
* @return string|bool
*/
public function validateLicense($license)
{
$data = '';
if (is_string($license)) {
$data = $license;
return $license;
}
return $data;
return false;
}
}
@@ -7,13 +7,11 @@
*/
namespace NilPortugues\Sitemap\Item;
use NilPortugues\Sitemap\Validators\IndexValidator;
/**
* Class IndexItem
* @package NilPortugues\Sitemap\Items
*/
class IndexItem extends AbstractItem implements ItemInterface
class IndexItem extends AbstractItem
{
/**
* @var \NilPortugues\Sitemap\Validators\IndexValidator
@@ -31,7 +29,7 @@ public function __construct()
/**
* @return string
*/
public function getHeader()
public static function getHeader()
{
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
@@ -40,7 +38,7 @@ public function getHeader()
/**
* @return string
*/
public function getFooter()
public static function getFooter()
{
return "</sitemapindex>";
}
@@ -31,7 +31,7 @@ public function getItemSize();
/**
* @return string
*/
public function getHeader();
public static function getHeader();
/**
* @return integer
@@ -41,7 +41,7 @@ public function getHeaderSize();
/**
* @return string
*/
public function getFooter();
public static function getFooter();
/**
* @return integer

0 comments on commit 88bbddb

Please sign in to comment.
You can’t perform that action at this time.