Skip to content

Commit 8df2906

Browse files
committed
Initial implementation
0 parents  commit 8df2906

File tree

10 files changed

+523
-0
lines changed

10 files changed

+523
-0
lines changed

db/zend_tutorial.db

2 KB
Binary file not shown.

lib/Album.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace ZendTutorialInCougar;
4+
5+
use Cougar\Security\iSecurity;
6+
7+
# Initialize the Cougar framework and add the application path
8+
require_once("cougar.php");
9+
\Cougar\Autoload\FlexAutoload::addPath(__DIR__, 1);
10+
11+
/**
12+
* Provides the Album API, which allows for the querying, creating, reading,
13+
* updating and deleting of Albums.
14+
*/
15+
class Album
16+
{
17+
/**
18+
* Stores the references to the security context and model factory.
19+
*
20+
* @param \Cougar\Security\iSecurity $security
21+
* Security context
22+
* @param AlbumModelFactory $factory
23+
* Album model factory
24+
*/
25+
public function __construct(iSecurity $security, AlbumModelFactory $factory)
26+
{
27+
// Store the references
28+
$this->security = $security;
29+
$this->factory = $factory;
30+
}
31+
32+
/**
33+
* Returns a list of albums. You may optionally pass a list of query
34+
* parameters to limit your search.
35+
*
36+
* @Path /album
37+
* @Methods GET
38+
* @GetQuery query
39+
* @XmlRootElement albums
40+
* @XmlObjectName album
41+
*
42+
* @var array $query
43+
* Optional query parameters
44+
* @return array Album list
45+
*/
46+
public function getAlbumList(array $query = array())
47+
{
48+
$pdo_model = $this->factory->AlbumPdoModel();
49+
return $pdo_model->query($query);
50+
}
51+
52+
/**
53+
* Creates a new album with the given Artist and Title.
54+
*
55+
* @Path /album
56+
* @Methods POST
57+
* @Accepts json
58+
* @Body album object
59+
* @XmlRootElement album
60+
*
61+
* @var mixed $album
62+
* Album model, object or assoc. array with album information
63+
* @return AlbumModel New album as a model
64+
*/
65+
public function createAlbum($album)
66+
{
67+
$new_album = $this->factory->AlbumPdoModel();
68+
$new_album->__import($album);
69+
$new_album->save();
70+
71+
# Detach from database
72+
return $this->factory->AlbumModel($new_album);
73+
}
74+
75+
/**
76+
* Gets the album associated with the given Album ID
77+
*
78+
* @Path /album/:id
79+
* @Methods GET
80+
* @XmlRootElement album
81+
*
82+
* @var int $id
83+
* Album ID
84+
* @return AlbumModel Album
85+
* @throws \Cougar\Exceptions\RecordNotFoundException
86+
*/
87+
public function getAlbum($id)
88+
{
89+
$album = $this->factory->AlbumPdoModel(array("id" => $id));
90+
91+
# Detach from the database
92+
return $this->factory->AlbumModel($album);
93+
}
94+
95+
/**
96+
* Updates the given album with the new information (ID cannot be changed)
97+
*
98+
* @Path /album/:id
99+
* @Methods PUT
100+
* @Accepts json
101+
* @Body album object
102+
* @XmlRootElement album
103+
*
104+
* @var mixed $album
105+
* Album model, object or assoc. array with ID and updated information
106+
* @return AlbumModel updated album
107+
*/
108+
public function updateAlbum($album)
109+
{
110+
$album = $this->factory->AlbumPdoModel($album);
111+
$album->save();
112+
113+
# Detach from database
114+
return $this->factory->AlbumModel($album);
115+
}
116+
117+
/**
118+
* Deletes the album associated with the given Album ID
119+
*
120+
* @Path /album/:id
121+
* @Methods DELETE
122+
*
123+
* @var int $id
124+
* Album ID
125+
*/
126+
public function deleteAlbum($id)
127+
{
128+
$album = $this->factory->AlbumPdoModel(array("id" => $id));
129+
$album->delete();
130+
}
131+
132+
/**
133+
* @var \Cougar\Security\iSecurity Security context
134+
*/
135+
protected $security;
136+
137+
/**
138+
* @var AlbumModelFactory
139+
*/
140+
protected $factory;
141+
}
142+
?>

lib/AlbumModelFactory.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace ZendTutorialInCougar;
4+
5+
use PDO;
6+
use Cougar\Security\iSecurity;
7+
use Cougar\Cache\iCache;
8+
9+
# Initialize the Cougar framework and add the application path
10+
require_once("cougar.php");
11+
\Cougar\Autoload\FlexAutoload::addPath(__DIR__, 1);
12+
13+
/**
14+
* Stores the dependencies for the AlbumModel classes and returns new instances
15+
* when needed.
16+
*/
17+
class AlbumModelFactory
18+
{
19+
/**
20+
* Stores the references to the security context, cache object and database
21+
* connection (PDO).
22+
*
23+
* @param iSecurity $security
24+
* Security context
25+
* @param iCache $cache
26+
* Cache object
27+
* @param PDO $pdo
28+
* Database connection
29+
*/
30+
public function __construct(iSecurity $security, iCache $cache, PDO $pdo)
31+
{
32+
// Store the object references
33+
$this->security = $security;
34+
$this->cache = $cache;
35+
$this->pdo = $pdo;
36+
}
37+
38+
/**
39+
* Returns a new instance of the AlbumModel object
40+
*
41+
* @var mixed $object
42+
* Object or assoc. array to import from
43+
* @var string $view
44+
* Set the initial view
45+
* @var bool strict
46+
* Whether to perform strict property checks
47+
* @return AlbumModel
48+
*/
49+
public function AlbumModel($object = null, $view = null, $strict = true)
50+
{
51+
return new AlbumModel($object, $view, $strict);
52+
}
53+
54+
/**
55+
* Returns a new instance of the AlbumModel object
56+
*
57+
* @var mixed $object
58+
* Object or assoc. array to import from
59+
* @var string $view
60+
* Set the initial view
61+
* @var bool strict
62+
* Whether to perform strict property checks
63+
* @return AlbumModel
64+
*/
65+
public function AlbumPdoModel($object = null, $view = null, $strict = true)
66+
{
67+
return new AlbumPdoModel($this->security, $this->cache, $this->pdo,
68+
$object, $view, $strict);
69+
}
70+
71+
/**
72+
* @var iSecurity Security context
73+
*/
74+
protected $security;
75+
76+
/**
77+
* @var iCache Cache object
78+
*/
79+
protected $cache;
80+
81+
/**
82+
* @var PDO Database connection
83+
*/
84+
protected $pdo;
85+
}
86+
?>

lib/Models/AlbumBaseModel.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace ZendTutorialInCougar;
4+
5+
/**
6+
* Defines the properties in the Album database table
7+
*
8+
* @CaseInsensitive
9+
*/
10+
abstract class AlbumBaseModel
11+
{
12+
/**
13+
* @var int Album ID
14+
*/
15+
public $id;
16+
17+
/**
18+
* @NotNull
19+
* @var string Album artist
20+
*/
21+
public $artist;
22+
23+
/**
24+
* @NotNull
25+
* @var string Album title
26+
*/
27+
public $title;
28+
}

lib/Models/AlbumModel.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ZendTutorialInCougar;
4+
5+
use Cougar\Model\iModel;
6+
use Cougar\Model\tModel;
7+
8+
# Initialize the Cougar framework and add the application path
9+
require_once("cougar.php");
10+
\Cougar\Autoload\FlexAutoload::addPath(__DIR__, 2);
11+
12+
/**
13+
* Creates the Album model from the base model
14+
*/
15+
class AlbumModel extends AlbumBaseModel implements iModel
16+
{
17+
use tModel;
18+
}

lib/Models/AlbumPdoModel.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace ZendTutorialInCougar;
4+
5+
use Cougar\Model\iStoredModel;
6+
use Cougar\Model\tPdoModel;
7+
8+
# Initialize the Cougar framework and add the application path
9+
require_once("cougar.php");
10+
\Cougar\Autoload\FlexAutoload::addPath(__DIR__, 2);
11+
12+
/**
13+
* Creates the Album PDO model from the base model
14+
*
15+
* @Table album
16+
* @Allow CREATE READ UPDATE DELETE QUERY
17+
* @PrimaryKey id
18+
*/
19+
class AlbumPdoModel extends AlbumBaseModel implements iStoredModel
20+
{
21+
use tPdoModel;
22+
}

0 commit comments

Comments
 (0)