~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.1.0RC/Nette/Database/Reflection/ConventionalReflection.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * This file is part of the Nette Framework (http://nette.org)
 
5
 *
 
6
 * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
 
7
 *
 
8
 * For the full copyright and license information, please view
 
9
 * the file license.txt that was distributed with this source code.
 
10
 */
 
11
 
 
12
namespace Nette\Database\Reflection;
 
13
 
 
14
use Nette;
 
15
 
 
16
 
 
17
/**
 
18
 * Reflection metadata class for a database.
 
19
 *
 
20
 * @author     Jakub Vrana
 
21
 * @author     Jan Skrasek
 
22
 */
 
23
class ConventionalReflection extends Nette\Object implements Nette\Database\IReflection
 
24
{
 
25
        /** @var string */
 
26
        protected $primary;
 
27
 
 
28
        /** @var string */
 
29
        protected $foreign;
 
30
 
 
31
        /** @var string */
 
32
        protected $table;
 
33
 
 
34
 
 
35
        /**
 
36
         * Create conventional structure.
 
37
         * @param  string %s stands for table name
 
38
         * @param  string %1$s stands for key used after ->, %2$s for table name
 
39
         * @param  string %1$s stands for key used after ->, %2$s for table name
 
40
         */
 
41
        public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
 
42
        {
 
43
                $this->primary = $primary;
 
44
                $this->foreign = $foreign;
 
45
                $this->table = $table;
 
46
        }
 
47
 
 
48
 
 
49
        public function getPrimary($table)
 
50
        {
 
51
                return sprintf($this->primary, $this->getColumnFromTable($table));
 
52
        }
 
53
 
 
54
 
 
55
        public function getHasManyReference($table, $key)
 
56
        {
 
57
                $table = $this->getColumnFromTable($table);
 
58
                return array(
 
59
                        sprintf($this->table, $key, $table),
 
60
                        sprintf($this->foreign, $table, $key),
 
61
                );
 
62
        }
 
63
 
 
64
 
 
65
        public function getBelongsToReference($table, $key)
 
66
        {
 
67
                $table = $this->getColumnFromTable($table);
 
68
                return array(
 
69
                        sprintf($this->table, $key, $table),
 
70
                        sprintf($this->foreign, $key, $table),
 
71
                );
 
72
        }
 
73
 
 
74
 
 
75
        protected function getColumnFromTable($name)
 
76
        {
 
77
                if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
 
78
                        return $match[1];
 
79
                }
 
80
 
 
81
                return $name;
 
82
        }
 
83
 
 
84
}