Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More cleaning #12

Merged
merged 1 commit into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions resources/views/sitemap/rss.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@
?>
@foreach($sitemap->getUrls() as $url)
<item>
@if ($url->has('loc'))
@if ($url->has('title'))
<title>{{ $url->getTitle() }}</title>
@endif

@if ($url->has('loc'))
<link>{{ $url->loc() }}</link>
<link>{{ $url->getLoc() }}</link>
@endif

@if ($url->has('lastmod'))
<ror:updated>{{ $url->formatLastMod() }}</ror:updated>
@endif

@if ($url->has('changefreq'))
<ror:updatePeriod>{{ $url->changeFreq() }}</ror:updatePeriod>
<ror:updatePeriod>{{ $url->getChangeFreq() }}</ror:updatePeriod>
@endif

@if ($url->has('priority'))
<ror:sortOrder>{{ $url->priority() }}</ror:sortOrder>
<ror:sortOrder>{{ $url->getPriority() }}</ror:sortOrder>
@endif

<ror:resourceOf>sitemap</ror:resourceOf>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/sitemap/txt.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
*/
?>
@foreach($sitemap->getUrls() as $url)
{{ $url->loc() }}
{{ $url->getLoc() }}
@endforeach
6 changes: 3 additions & 3 deletions resources/views/sitemap/xml.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
@foreach($sitemap->getUrls() as $url)
<url>
@if ($url->has('loc'))
<loc>{{ $url->loc() }}</loc>
<loc>{{ $url->get('loc') }}</loc>
@endif

@if ($url->has('lastmod'))
<lastmod>{{ $url->formatLastMod() }}</lastmod>
@endif

@if ($url->has('changefreq'))
<changefreq>{{ $url->changeFreq() }}</changefreq>
<changefreq>{{ $url->get('changefreq') }}</changefreq>
@endif

@if ($url->has('priority'))
<priority>{{ $url->priority() }}</priority>
<priority>{{ $url->get('priority') }}</priority>
@endif
</url>
@endforeach
Expand Down
45 changes: 9 additions & 36 deletions src/Contracts/Entities/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ interface Url extends Arrayable, Jsonable, JsonSerializable
*/
public function getLoc();

/**
* Get the url location (alias).
*
* @see getLoc()
*
* @return string
*/
public function loc();

/**
* Set the url location.
*
Expand All @@ -50,15 +41,6 @@ public function setLoc($loc);
*/
public function getLastMod();

/**
* Get the last modification date (alias).
*
* @see getLastMod()
*
* @return \DateTimeInterface
*/
public function lastMod();

/**
* Format the url last modification.
*
Expand All @@ -85,15 +67,6 @@ public function setLastMod($lastModDate, $format = 'Y-m-d H:i:s');
*/
public function getChangeFreq();

/**
* Get the change frequency (alias).
*
* @see getChangeFreq()
*
* @return string
*/
public function changeFreq();

/**
* Set the change frequency.
*
Expand All @@ -110,15 +83,6 @@ public function setChangeFreq($changeFreq);
*/
public function getPriority();

/**
* Get the priority (alias).
*
* @see getPriority()
*
* @return float
*/
public function priority();

/**
* Set the priority.
*
Expand Down Expand Up @@ -166,4 +130,13 @@ public static function make($loc);
* @return \Arcanedev\LaravelSitemap\Entities\Url
*/
public static function makeFromArray(array $attributes);

/**
* Check if has an attribute.
*
* @param string $key
*
* @return bool
*/
public function has($key);
}
72 changes: 12 additions & 60 deletions src/Entities/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@ public function getLoc()
return $this->escape($this->get('loc'));
}

/**
* Get the url location (alias).
*
* @see getLoc()
*
* @return string
*/
public function loc()
{
return $this->getLoc();
}

/**
* Set the url location.
*
Expand All @@ -89,18 +77,6 @@ public function getLastMod()
return $this->get('lastmod');
}

/**
* Get the last modification date (alias).
*
* @see getLastMod()
*
* @return \DateTimeInterface
*/
public function lastMod()
{
return $this->getLastMod();
}

/**
* Format the url last modification.
*
Expand Down Expand Up @@ -139,18 +115,6 @@ public function getChangeFreq()
return $this->get('changefreq');
}

/**
* Get the change frequency (alias).
*
* @see getChangeFreq()
*
* @return string
*/
public function changeFreq()
{
return $this->getChangeFreq();
}

/**
* Set the change frequency.
*
Expand All @@ -173,18 +137,6 @@ public function getPriority()
return $this->get('priority');
}

/**
* Get the priority (alias).
*
* @see getPriority()
*
* @return float
*/
public function priority()
{
return $this->getPriority();
}

/**
* Set the priority.
*
Expand Down Expand Up @@ -252,6 +204,18 @@ public static function makeFromArray(array $attributes)
return new static($attributes);
}

/**
* Check if has an attribute.
*
* @param string $key
*
* @return bool
*/
public function has($key)
{
return ! is_null($this->get($key));
}

/**
* Convert the Fluent instance to an array.
*
Expand Down Expand Up @@ -284,18 +248,6 @@ protected function set($key, $value)
return $this;
}

/**
* Check if has an attribute.
*
* @param string $key
*
* @return bool
*/
protected function has($key)
{
return ! is_null($this->get($key));
}

/**
* Escape the given value.
*
Expand Down
58 changes: 57 additions & 1 deletion tests/Entities/SitemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ public function it_can_check_if_number_of_urls_is_exceeded()
/** @test */
public function it_can_set_urls()
{
$this->assertSame(0, $this->sitemap->count());
$this->sitemap->add($this->createUrlSample());

$this->assertSame(1, $this->sitemap->count());

$urls = collect([
['loc' => 'http://example.com'],
Expand Down Expand Up @@ -243,6 +245,60 @@ public function it_can_set_urls()
$this->assertSame($expected, $this->sitemap->toArray());
}

/** @test */
public function it_can_add_many_urls_to_the_collection()
{
$this->sitemap->add($this->createUrlSample());

$this->assertSame(1, $this->sitemap->count());

$urls = collect([
['loc' => 'http://example.com/news'],
['loc' => 'http://example.com/about-us'],
['loc' => 'http://example.com/contact'],
]);

$now = new \DateTime;

$this->sitemap->addMany($urls->transform(function (array $item) use ($now) {
return Url::makeFromArray($item)->setLastMod($now);
}));

$this->assertSame(4, $this->sitemap->count());

$formattedDate = $now->format(\DateTime::ATOM);

$expected = [
[
'loc' => 'http://example.com',
'lastmod' => '2017-01-01T00:00:00+00:00',
'changefreq' => 'always',
'priority' => 1.0,
'title' => 'Example - Homepage',
],[
'loc' => 'http://example.com/news',
'lastmod' => $formattedDate,
'changefreq' => 'daily',
'priority' => 0.8,
'title' => null,
],[
'loc' => 'http://example.com/about-us',
'lastmod' => $formattedDate,
'changefreq' => 'daily',
'priority' => 0.8,
'title' => null,
],[
'loc' => 'http://example.com/contact',
'lastmod' => $formattedDate,
'changefreq' => 'daily',
'priority' => 0.8,
'title' => null,
],
];

$this->assertSame($expected, $this->sitemap->toArray());
}

/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@
<title>XML Sitemaps RSS Feed</title>
<link>http://example.com/sitemap-pages.xml</link>
<item>
<title/>
<link>http://example.com/</link>
<ror:updated>2017-01-01T00:00:00+00:00</ror:updated>
<ror:updatePeriod>daily</ror:updatePeriod>
<ror:sortOrder>0.8</ror:sortOrder>
<ror:resourceOf>sitemap</ror:resourceOf>
</item>
<item>
<title/>
<link>http://example.com/about-us</link>
<ror:updated>2017-01-01T00:00:00+00:00</ror:updated>
<ror:updatePeriod>daily</ror:updatePeriod>
<ror:sortOrder>0.8</ror:sortOrder>
<ror:resourceOf>sitemap</ror:resourceOf>
</item>
<item>
<title/>
<link>http://example.com/contact</link>
<ror:updated>2017-01-01T00:00:00+00:00</ror:updated>
<ror:updatePeriod>daily</ror:updatePeriod>
Expand Down